Acorn | Introducing GPTScript ...Officially

07.08.2024Ricky Elfner
News Artificial Intelligence Developer Experience Framework Machine Learning Neural Networks scripting

Banner

GPTScript offers an innovative way to work with large language models (LLMs). The first public presentation took place on February 24, 2024, by Darren Shepherd on X (formerly Twitter). Already five days earlier, the initial version v0.0.1 was released on GitHub. One month later, the official announcement followed on the acorn.io website, which is how I became aware of it and chose it as my next Tech-Up topic.

Background

In his blog post, Shepherd explained that GPTScript initially started as an experiment to enable programming using natural language. The idea was to combine natural language with the functionality of traditional scripts. In doing so, the team discovered an elegant and simple model that connects AI with traditional systems, data, and code. GPTScript uses tools that can either be pure prompts or implemented with code. ChatGPT then makes it possible to link these individual tools together to efficiently handle complex tasks.

Main Features of GPTScript

One of the main features of GPTScript is definitely its user-friendliness. The syntax is mainly based on natural language, which makes it accessible to a wide range of users. In addition, GPTScript impresses with its versatility, as it can cover a variety of use cases, such as task automation, data analysis, and integration with external services. Another important feature is the seamless integration of GPTScript with traditional scripts, such as Bash or Python, as well as external HTTP services. This integration significantly expands the capabilities and application possibilities of GPTScript.

Application Possibilities

GPTScript thus offers a wide variety of application possibilities. For example, the automation of complex tasks, such as vacation planning or database management. In addition, intelligent tools can be developed that cover a variety of functions. Even demanding data analyses and their visualizations can be carried out efficiently with GPTScript, which facilitates the processing of complex data sets. Furthermore, GPTScript offers possibilities for developing applications in image, video, and audio processing, whereby powerful multimedia applications can be realized.

Practical Examples

  • Chat with Local CLI:
    • Enables the creation of AI integrations with CLIs or other executables on the local computer.
    • Example: GitHub CLI (gh) or Kubernetes CLI (kubectl).
  • Chat with an API:
    • DevOps engineers often interact with cloud providers via dashboards, APIs, and CLIs. Now you can define a desired task, and ChatGPT generates and executes the necessary API calls.
    • Example: With Digital Ocean’s OpenAPI specification, you can create a chatbot that can launch droplets and databases.
  • Chat with Local Files:
    • You can also work with local files.
    • Example: Query Excel files, CSVs, and PDFs to read, transform, and then use the data.

Structure of a GPTScript

If you want to create a gptscript, you have to create a file with the file extension .gpt. This file can contain several tools by separating the individual areas with three hyphens (---) on one line.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Name: tool1
Description: This is tool1

Do sample tool stuff.

---

Name: tool2
Description: This is tool2

Do more sample tool stuff.

Each tool starts with a description before the actual part of the script comes with the actual content. Of course, there is also the possibility to add comments.

1
2
3
4
5
6
7
8
Name: tool-name
# This is a comment in the preamble.
Description: Tool description
# This tool can invoke tool1 or tool2 if needed
Tools: tool1, tool2
Args: arg1: The description of arg1

Tool instructions go here.

Parameters at a Glance

At the beginning of the script, there are different parameters that can and sometimes must be used. Particularly important are the description and the name so that ChatGPT can use the necessary tools from the context.

  • Name:
    • The name of the tool.
  • Model Name & Global Model Name:
    • The LLM model name to be used. Currently, “gpt-4” is used by default.
  • Description:
    • This describes the specific purpose and functionality of the tool so that the model can use the appropriate tool from the context.
  • Internal Prompt:
    • If set to false, the built-in system prompt for this tool is disabled.
  • Tools & Global Tools:
    • Defines all tools that are available.
  • Credentials:
    • Since it is possible to create credential tools, these can be linked separately here.
  • Args:
    • Arguments that the tool accepts. The format arg-name: Description must be used.
  • Max Tokens:
    • Limits the maximum number of tokens that can be generated by the LLM.
  • JSON Response:
    • If set to true, the LLM responds in JSON format. In this case, instructions must also be included in the tool.
  • Temperature:
    • A floating-point number that determines the creativity level of the model. By default, the temperature is set to 0, which produces a more deterministic and less creative response.
  • Chat:
    • If set to true, an interactive chat session is enabled for the tool.
  • Context:
    • Defines the context for the prompts. It is possible to refer to a text file, whereby different tools can share the same context.

Defining the Interpreter

The interpreter must start with #!, for example:

  • #!/bin/bash
  • #!/python3

Tools in GPTScript

In GPTScript, tools help to extend the capabilities of a script. The idea behind this is that AI works better when it is given very specific instructions for a task. Tools make it possible to break down a problem into smaller, more focused parts, with each tool performing a specific task.

There is already a selection of GPTScript tools that you can use: GPTScript Tools.

In addition, a distinction is made between different types:

  • System Tools:
  • Custom Tools:
    • If you write your own scripts and tools, you have the possibility to define several individual tools within the script.
  • External Tools:
    • Existing tools can be linked and used directly within the script.

Hands-On Example

In the following section, we will take a closer look at the practical application of GPTScript using a detailed example script. We will show how different tools are defined and used within the script to comprehensively demonstrate the described functions and to illustrate the diverse application possibilities of GPTScript.

Installation

First, GPTScript must be installed on the system. In our case, we will use Homebrew, a popular package management system for macOS and Linux, to simplify the installation process. This can be done by running the following command from the terminal to install GPTScript:

1
brew install gptscript-ai/tap/gptscript

Creating an OpenAI API Key

To use the features of GPTScript, you need an API key from OpenAI. To do this, you need to do the following:

  1. Visit the OpenAI platform at OpenAI API Keys.
  2. Log in to an existing account or create a new one.
  3. Create a new API key and copy it.

After the API key has been created, it must be set as an environment variable so that GPTScript can access it. You can set the key via the terminal with the following command:

1
export OPENAI_API_KEY="[API-KEY]"

Pricing

The use of the OpenAI API is subject to charges. The exact costs depend on the amount of usage and the model chosen. OpenAI offers different pricing models that can be selected according to your needs. A detailed price list can be found on the OpenAI website at OpenAI API Pricing. It is important to be aware of the costs to avoid surprises and to plan usage accordingly.

Defining the First GPTScript File - Step 1

After installation and setting up the API key, you can create the first GPTScript file. In this example, a script called techhub-generator_01.gpt is defined. In the following steps, I will create a new file again and again so that you can see how often you have to adjust the prompt.

The first script is used to create a suitable title and description for a Techhub blog article based on a given link. This is achieved by the following code:

1
2
3
4
5
6
description: Creates a Techhub thumbnail and title using a script.
args: link: The web link for which the title and thumbnail will be created.

Do the following steps in ascending order:
1. Develop a suitable title for the Techhub based on the link. Also create a description for a blog article based on the link.
2. Print the title and the description on the screen

By executing the script with the command:

1
 gptscript techhub-generator_01.gpt --link https://docs.gptscript.ai/

a title and description for the specified link will be generated.

The output in the console looked like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
18:05:10 started  [main] [input=--link https://docs.gptscript.ai/]
18:05:11 sent     [main]
18:05:14 ended    [main] [output=**Title:** Exploring GPTScript: The Future of AI-Powered Scripting\n\n**Description:** Dive into the w...]
18:05:14 usage    [total=237] [prompt=146] [completion=91]

INPUT:

--link https://docs.gptscript.ai/

OUTPUT:

**Title:** Exploring GPTScript: The Future of AI-Powered Scripting

**Description:** Dive into the world of GPTScript, an innovative scripting language designed to harness the power of AI. This blog article explores the features, benefits, and potential applications of GPTScript, providing insights into how it can revolutionize the way developers and businesses approach automation and intelligent scripting. Discover how GPTScript can streamline your workflows and enhance productivity with its advanced AI capabilities.

The output is often quite helpful, as you can see the individual calls and what the input and output was. This will be particularly noticeable later when using multiple tools.

Step 2

After creating the first GPTScript file in the previous step, the process is continued in step 2 by adding an extended part with the tool mkdir. This script not only creates a suitable title and description for a Techhub blog article but also generates a folder based on the slug if it does not already exist. In addition, a text file with the description is saved in this folder.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
tools: mkdir, sys.write
description: Creates a Techhub thumbnail and title using a script.
args: link: The web link for which the title and thumbnail will be created.

Do the following steps in ascending order:
1. Develop a suitable title for the Techhub based on the link. Also create a description for a blog article based on the link.
2. Create a slug based on the '$(techup-title) and then create a folder with the slug name if it foes not already

---

name: mkdir 
tools: sys.write
description: Creates a folder in our system. 
args: slug: Path to the folder to be created.

#!bash

mkdir -p "${slug}"

The command to execute the script is as follows:

1
 gptscript techhub-generator_02.gpt --link https://docs.gptscript.ai/

If you are still interested in the output from the terminal, you can have a look at it in the TechHub Repo.

Thus, with this step, you get a more automated process that creates the following folder structure:

1
2
3
4
5
6
.
├── exploring-gptscript-a-comprehensive-guide
│   └── description.txt
├── techhub-generator.gpt
├── techhub-generator_01.gpt
└── techhub-generator_02.gpt

Step 3

In this step, the script is extended so that a thumbnail is also generated. This is done within the tool thumb-generator. I used an existing tool from GptScript myself.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
tools: thumb-generator, mkdir, sys.write, sys.download
description: Generates a Blog Article title and thumbnail based on a link.
args: link: The web link for which the title and thumbnail will be created.

Do the following steps in acsending order:
1. Develop a suitable title for the Techhub based on the link. Also create a description for a blog article based on the link.
2. Create a slug based on the '$(techup-title) and then create a folder with the slug name if it foes not already
3. Call thumb-generator to illustrate it.
4. Download the illustration to a file at `${slug}/thumb.png`.

---
name: mkdir
tools: sys.write
description: Creates a folder in your system
args: slug: Path to the folder to be created.

#!bash

mkdir -p "${slug}"

---
name: thumb-generator
tools: github.com/gptscript-ai/image-generation
description: Generates a YouTube thumbnail.
args: link: The link to generate thumbnail from.

Do the following steps in acsending order:

1. Come up with a background color to represent the $link which can be used as the background color for the thumbnail.
2. Think of a good prompt to generate an image to represent the content of '$(techup-description). Make sure to to include the '${techup-title} in one sentence inside a colored box somewhere in the thumbnail. Only return the URL of the illustration. The thumbnail should be 1792x1024.
3. Use the ${background-color} to make sure the edges of the thumbnails fades out.

Again, the execution of the script.

1
 gptscript techhub-generator_03.gpt --link https://docs.gptscript.ai/

Again, the output is located in our TechHub Repo.

Step 4

Now we come to the last step of our example script. The only thing missing is the possibility to create a Markdown file that already contains an outline with the most important topics. For this, I wrote the article-generator.

An index.md file should be created in the same folder where the thumbnail was generated. The title of the blog article should be added to this file. An outline for the article is created with the descriptions created. This outline can then be used as a guide when writing the article.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
tools: article-generator, thumb-generator, mkdir, sys.write, sys.download
description: Generates a Blog Article title and thumbnail based on a link.
args: link: The web link for which the title and thumbnail will be created.

Do the following steps in acsending order:
1. Develop a suitable title for the Techhub based on the link. Also create a description for a blog article based on the link.
2. Create a slug based on the '$(techup-title) and then create a folder with the slug name if it foes not already
3. Call thumb-generator to illustrate it.
4. Download the illustration to a file at `${slug}/thumb.png`.
5. Call the article-generator to generate the index.md file

---
name: mkdir
tools: sys.write
description: Creates a folder in your system
args: slug: Path to the folder to be created.

#!bash

mkdir -p "${slug}"

---
name: thumb-generator
tools: github.com/gptscript-ai/image-generation
description: Generates a YouTube thumbnail.
args: link: The link to generate thumbnail from.

Do the following steps in acsending order: 

1. Come up with a background color to represent the $link which can be used as the background color for the thumbnail.
2. Think of a good prompt to generate an image to represent the content of '$(techup-description). Make sure to to include the '${techup-title} in one sentence inside a colored box somewhere in the thumbnail. Only return the URL of the illustration. The thumbnail should be 1792x1024.
3. Use the ${background-color} to make sure the edges of the thumbnails fades out.

---
name: article-generator
tools: sys.write
description: Create an index.md file with the basic information.

Do the following steps in acsending order: 
1. create an '${slug}/index.md file 
2. add at the beginning of the file the basic infromation '$(slug). 
3. Create with the correct markdown syntax the '$(techup-title) as heading of this article
4. use the'$(techup-description) to write a outline with most important information for the technical blog article

Now we execute the script one last time:

1
gptscript techhub-generator.gpt --link https://docs.gptscript.ai/                                           

Especially with the length of the console output, it is also located in our Repo.

In the folder structure, you can see that an index.md file and a thumb.png have been created. In this case, it is not just an outline, but some sentences have already been added. Here it becomes clear that the prompt would have to be much more precise in this case in order to obtain the desired result.

And this is the result of the thumb generator:

Conclusion

GPTScript actually offers a very impressive way to easily link traditional scripts with LLMs. This naturally extends the basic functionality enormously. This allows you to easily come up with a wide variety of ideas.

But of course, as with any technology, you have to deal with some challenges. What I would like to emphasize here is the writing of prompts. Anyone who has ever done prompt engineering themselves knows what a change this means. And you can already see this in such small tools that you create with GPTScript.

So far, I have not tried out all the functionalities, but there are some useful functions that you can definitely try out. These include, for example, analyzing local data.

Finally, it can be said that this offers a very simple and good way to automate different things and link them with the possibilities of LLMs. However, you have to get used to this type of development at the beginning and invest a little time to achieve the desired goals.

[!TIP]

All examples are also available in our Techhub Repository

This techup has been translated automatically by Gemini

Ricky Elfner

Ricky Elfner – Denker, Überlebenskünstler, Gadget-Sammler. Dabei ist er immer auf der Suche nach neuen Innovationen, sowie Tech News, um immer über aktuelle Themen schreiben zu können.