Getty Images

Natural language programming using GPTScript

GPTScript enables programmers to use natural language syntax and tap into OpenAI when building apps. Here's a basic GPTScript tutorial with examples for beginners.

As enterprises look for all sorts of ways to embrace AI, software developers must increasingly be able to write programs that work directly with AI models to execute logic and get results.

One of the newer entrants into application development that takes advantage of AI is GPTScript, an open source programming language that lets developers write statements using natural language syntax. That capability is not only interesting and impressive, it's potentially game changing.

This tutorial covers the following areas:

  • What is GPTScript, what are its benefits and how does it work?
  • How to install GPTScript and configure it to use the default OpenAI model.
  • Walk-through examples with simple scripts that use the default capabilities of GPTScript.
  • Demonstrate how to use tool libraries and prepare programmers to take the next step in GPTScript programming.

All the sample code demonstrated in this tutorial is available at a GitHub repository.

What is GPTScript? How does it work with AI?

At a basic level, GPTScript lets developers write code that expresses commands as simple sentences, just as with a prompt in the ChatGPT UI. For example, a developer can write a script in GPTScript with the following statement:

Show me the sum of 2 plus 3.

And get the following output:

OUTPUT:
The sum of 2 plus 3 is 5

In a nutshell, GPTScript turns the statement over to OpenAI, which processes the sentence to figure out the programming logic and return a result. The ability to program in natural language presents capabilities that go well beyond how developers presently write software.

Next we'll describe how to install and configure GPTScript.

Get started with GPTScript

To get up and running with GPTScript, you need an account with OpenAI to obtain an API key and access the OpenAI API. GPTScript interacts with the OpenAI API by default, hence the need for the API key. Once you have that, download and install the GPTScript executable.

Set up and fund an account on OpenAI

To set up an account and get an API, go to the OpenAI platform page and click the Sign up button, as shown in Figure 1 (callout 1). Complete and save your profile information to create an account for yourself. Save the API key; you'll need it later.

Once you create the account, click the Dashboard menu item (callout 2). Then click the API keys menu on the left (callout 3).

Screenshots that show the process to obtain an API key at the OpenAI platform website.
Figure 1. The process to obtain an API key at the OpenAI platform website.

Once you've created the API, you need to give OpenAI some money to fund usage of the OpenAI API. The steps to do this are shown in Figure 2.

Click the Settings icon on the upper left of the page and select Your profile (callout 1), then select the Billing menu (callout 2), then click the button labeled Add to credit balance (callout 3) to fund the account.

Screenshot showing how to add funds for API usage within the Billing page of the user profile.
Figure 2. Add funds for API usage within the Billing page of the user profile.

How to install GPTScript

With the account funded, now you're ready to install and use GPTScript.

GPTScript runs on Mac, Linux and Windows. The following table shows the installation commands for all three OS platforms.

Platform Installation command
macOS brew install gptscript-ai/tap/gptscript
Linux curl https://get.gptscript.ai/install.sh | sh
Windows winget install gptscript-ai.gptscript

Once the GPTScript executable is installed, the last thing to do is add the environmental variable OPENAI_AP_KEY to the runtime environment. Remember, you created the API key earlier when you configured your account on OpenAI.

Setting up the environment variable

To add the environment variable to macOS and Linux, run the following commands: 

echo OPENAI_AP_KEY=<YOUR_API_KEY_HERE> >> ~/.bashrc
source  ~/.bashrc

To add the environment variable in a Windows environment, follow these steps:

  1. Press the Windows key + R to open the Run dialog.
  2. Type sysdm.cpl and press Enter. This opens the System Properties window.
  3. Click on the Advanced tab.
  4. Click on the Environment Variables button.
  5. Under User variables, click New.
  6. In the "Variable name" field, type OPENAI_API_KEY.
  7. In the "Variable value" field, type your actual OpenAI API key.
  8. Click OK on all the open windows to save the changes.

Once the environment variable is set, you're ready to program using GPTScript.

GPTScript scripting basics

At the introductory level, with GPTScript a developer writes a command or set of commands in plain language, saves it all in a file with the extension .gpt, then runs the gptscript executable with the file name as a parameter.

The following sections provide examples of various scripts to run with GPTScript.

Getting information from the internet

First, we'll run a script that returns a list of all the world capitals with the biggest populations.

Run the instructions at the Linux/macOS command line to create a file named capitals.gpt. The file contains instructions to output a list of the five capitals of the world with the largest populations. The following code shows how to inject the GTPScript code into the file capitals.gpt and how to run the code using the GPTScript executable. Following the code is the output.

echo "List the 5 world capitals that have the largest populations." > capitals.gpt

gptscript capitals.gpt

-------

OUTPUT:

1. Beijing, China
2. New Delhi, India
3. Tokyo, Japan
4. Jakarta, Indonesia
5. Manila, Philippines

In this next script, we'll create a script that returns the five world capitals with the smallest populations.

These instructions at the Linux/macOS command line create a file named fewest-capitals.gpt, run the script under GPTScript and generate the resulting output.

List the 5 world capitals that have the smallest populations." > fewest-capitals.gpt

gptscript fewest-capitals.gpt

-------

OUTPUT:

1. Ngerulmud, Palau
2. Vatican City, Vatican City
3. Funafuti, Tuvalu
4. San Marino, San Marino
5. Tarawa, Kiribat

Those two scripts show that GPTScript interacts with OpenAI by default as if the commands were entered as prompts in the ChatGPT UI. However, this is a cloud-based interaction -- GPTScript has no knowledge of or access to the developer's local machine.

In the next section we'll discuss how developers can declare within GPTScript code tools that are built into GPTScript itself, and through those tools apply natural language programming to work with content on the local machine.

Analyze data on a local machine

GPTScript has a tool ecosystem that lets developers add special tool libraries to implement particular behaviors. To view a list of such tools, execute the following command line instruction:

gptscript –list-tools

The following example describes GPTScript code that uses the built-in tools sys.ls and sys.read tool libraries to list directories and read files on a local machine for content that meets certain criteria. Specifically, the script looks in the quotes directory downloaded from the aforementioned GitHub repository, and determines which files contain text not written by William Shakespeare.

Here are the files in the directory:

Quotes
  ├── quote-01.txt
  ├── quote-02.txt
  └── quote-03.txt

And here is the code for the GPTScript:

tools: sys.ls, sys.read 

Print by filename the first line of all the files in ./quotes

Notice that the first line of code invokes the tools attribute, which declares that the script will use the sys.ls and sys.read tools that ship with GPTScript code. These tools enable access to list and read files in the local machine's file system. The second line of code is a natural language instruction that tells GPTScript to list all the files in the ./quotes directory according to their file names and print the first line of text in each file. The final line of code tells GPTScript to inspect each file to determine which text was not written by William Shakespeare.

The resulting output is as follows:

OUTPUT:

- quote-01.txt: Four score and seven years ago our fathers brought forth, on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.
- quote-02.txt: To be, or not to be, that is the question:
- quote-03.txt: Odd that mankind's benefactors should be amusing people. In America at least this is the case. Anyone who wants to govern the country has to entertain it. During the Civil War people complained about Lincoln's funny stories. Perhaps he sensed that strict seriousness was far more dangerous than any joke. But critics said that he was frivolous and his own Secretary of War referred to him as an ape.

Content not written by Shakespeare:
- quote-01.txt
- quote-03.txt

Importantly, that code combines two major aspects of GPTScript programming. First, it uses tools built into GPTScript to access data on the local machine. Second, it taps into the power of OpenAI remotely to analyze the content of each file and make a criteria-based determination about the data in those files.

Those two aspects give developers the best of both worlds: GPTScript interacts with the computer's file system locally, and OpenAI's artificial intelligence remote capabilities analyze the contents in particular files. This, plus the natural language capabilities, takes computer programming to a whole new level.

GPTScript's potential for app development

GPTScript is already helpful to developers at all skill levels, with capabilities well beyond how developers presently write software. There's a lot more to learn about it beyond this very basic overview. For example, developers can create their own custom tools and reuse them among any number of scripts.

GPTScript is still very early in its maturation process, but its potential is tantalizing. Imagine developers using voice recognition to write sophisticated programs with GPTScript -- just saying the commands out loud, without typing out anything. That would fundamentally change the way developers work.

Getting accustomed to working with GPTScript now will get you in on the ground floor for a new way of programming that is just ahead on the horizon.

Bob Reselman is a software developer, system architect and writer. His expertise ranges from software development technologies to techniques and culture.

Dig Deeper on Core Java APIs and programming techniques