GPTScript Documentation
website·Indexed 33 days ago
https://docs.gptscript.aiGPTScript is a tool for creating AI integrations and automated workflows using a system of tools. It allows developers to extend LLM capabilities via natural language prompts, code-based tools, OpenAPI definitions, and local CLI integrations. The platform includes a command-line interface for script evaluation, credential management, and formatting, and supports alternative model providers that expose an OpenAI-compatible API.
What's inside GPTScript
- Tools in GPTScript are used to break complex problems into smaller, focused tasks, allowing the AI to perform better with specific instructions. A typical pattern involves a main script that imports a set of tools to accomplish its goal. GPTScripts can utilize three types of tools: built-in system tools, in-script tools, and external tools.
Understand GPTScript tool architecture
GPTScript is composed of tools. A tool can be either a natural language prompt for the LLM or a programmatic implementation (such as a command, script, or program). Tools using natural language prompts can invoke other tools, similar to function calls. The LLM determines when a tool should be invoked and defines the parameters to be passed to it.Share prompt information using the Context directive
GPTScript uses theContextdirective to share prompt information across multiple tools. The content of the context is prepended to the instruction of the calling tool, providing the LLM with additional guidance on how or when to use specific tools. Context can be provided via a static text file or a GPTScript tool. If a GPTScript tool is used, its output is prepended to the calling tool's instruction.Implement a Node.js-based GPTScript tool
Node.js tools can be implemented as inline scripts or external files. For complex tools or those requiring dependencies, reference an external.jsfile using the${GPTSCRIPT_TOOL_DIR}environment variable. To manage dependencies, include apackage.jsonfile in the same directory as thetool.gptoragent.gptfile; GPTScript will automatically install the dependencies before running the tool.# Inline example Name: my-node-tool #!node console.log('hello world') # External file example Name: my-node-tool #!/usr/bin/env node ${GPTSCRIPT_TOOL_DIR}/tool.jsImplement a Python-based GPTScript tool
Python tools can be implemented as simple inline scripts or as external files. For complex tools or those with dependencies, reference an external script using the${GPTSCRIPT_TOOL_DIR}environment variable. To manage external dependencies, include arequirements.txtfile in the same directory as thetool.gptoragent.gptfile; GPTScript will automatically install these dependencies before execution.# Inline example Name: my-python-tool #!python3 print('hello world') # External file example Name: my-python-tool #!/usr/bin/env python3 ${GPTSCRIPT_TOOL_DIR}/tool.pyUse packaged tools from GitHub
Tools can be packaged in GitHub repositories and referenced by their GitHub URL. When run, GPTScript clones the referenced repositories locally to execute the tools.
Constraint: The GitHub URL must NOT be prefixed with
http://orhttps://.tools: github.com/gptscript-ai/dalle-image-generation, github.com/gptscript-ai/gpt4-v-vision, sys.read Generate an image of a city skyline at night and write the resulting image to a file called city_skyline.png. Take this image and write a description of it in the style of pirate.Download an OpenAPI definition for use with GPTScript
While GPTScript can reference OpenAPI definitions via URL, downloading the file locally is often easier for development. You can usecurlto save the specification to your current directory.curl -o openapi.yaml -L https://api-engineering.nyc3.cdn.digitaloceanspaces.com/spec-ci/DigitalOcean-public.v2.yamlImplement Output Filters in GPTScript
Output filters are tools that modify the final output produced by a tool before it is returned to the user. They are defined using the
Output Filter: toolNamesyntax.Output filter tools must:
- Read the tool's output from the
OUTPUTenvironment variable. - Print the modified result to
stdout.
Additionally, output filters can access the following environment variables:
CHAT(boolean): Indicates if the script is running in chat mode.CONTINUATION(boolean): IfCHATis true, indicates if the chat will continue executing or if this is the final message.
Output Filter: applesToOranges Write a poem about apples. --- Name: applesToOranges #!python3 import os replacements = {"apple": "orange", "apples": "oranges"} output_text = os.getenv("OUTPUT", "") # Simple replacement logic for key, value in replacements.items(): output_text = output_text.replace(key, value) print(output_text)- Read the tool's output from the
Define and use in-script tools
In-script tools are defined within the same file as the main script. This is used to break a large script into smaller, manageable pieces. The tool is defined after a separator (---) with a name, description, and the specific instructions for that tool.tools: random-number Select a number at random and, based on the results, write a poem about it. --- name: random-number description: Generate a random number between 1 and 100. Select a number at random between 1 and 100 and return only the number.Create a custom tool for GPTScript
To author a tool in GPTScript, you create a
tool.gptfile in the root directory of your project. This file defines the tool's metadata and the command to execute it. GPTScript supports Python, Node.js, and Go for managed runtime and dependency installation.Key implementation details:
- Parameters: Any parameter defined in the
tool.gptfile is passed to the tool as an environment variable. Your code should read these variables directly rather than using CLI flags. - Execution Path: Use the
${GPTSCRIPT_TOOL_DIR}environment variable in the shebang or execution command to ensure the tool can locate its source files regardless of the user's current working directory.
Description: Returns the contents of a webpage. Param: url: The URL of the webpage. #!/usr/bin/env python3 ${GPTSCRIPT_TOOL_DIR}/tool.py- Parameters: Any parameter defined in the
Install GPTScript via Homebrew
GPTScript can be installed on MacOS and Linux using Homebrew. It is recommended to use the official homebrew tap to ensure you have the latest updates, as the homebrew-core formula may be outdated. After installation, you will need an OpenAI API key to use the framework.brew install gptscriptStructure a GPTScript tool repository for distribution
To distribute a tool via GitHub, the repository must contain either a
tool.gptoragent.gptfile. If both exist,agent.gpttakes precedence. These files should typically reside in the root directory. If multiple tools are hosted in one repo, placing the.gptfile in a subdirectory allows the tool to be referenced asgithub.com/<user>/<repo>/<subdirectory>.It is recommended to include
NameandDescriptiondirectives within the.gptfile to help users and LLMs understand the tool's purpose. Tool parameters are passed to the underlying code as environment variables rather than command-line arguments.