GPTScript Documentation

website·Indexed 33 days ago

https://docs.gptscript.ai

GPTScript 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.

Tokens
12.4K
Snippets
77
Records
92
Agent score
63%

What's inside GPTScript

  1. Overview of using tools in 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.
  2. 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.
  3. Share prompt information using the Context directive

    GPTScript uses the Context directive 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.
  4. 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 .js file using the ${GPTSCRIPT_TOOL_DIR} environment variable. To manage dependencies, include a package.json file in the same directory as the tool.gpt or agent.gpt file; 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.js
  5. Implement 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 a requirements.txt file in the same directory as the tool.gpt or agent.gpt file; 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.py
  6. Use 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:// or https://.

    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.
  7. 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 use curl to save the specification to your current directory.
    curl -o openapi.yaml -L https://api-engineering.nyc3.cdn.digitaloceanspaces.com/spec-ci/DigitalOcean-public.v2.yaml
  8. Implement 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: toolName syntax.

    Output filter tools must:

    1. Read the tool's output from the OUTPUT environment variable.
    2. 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): If CHAT is 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)
  9. 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.
  10. Create a custom tool for GPTScript

    To author a tool in GPTScript, you create a tool.gpt file 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.gpt file 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
  11. 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 gptscript
  12. Structure a GPTScript tool repository for distribution

    To distribute a tool via GitHub, the repository must contain either a tool.gpt or agent.gpt file. If both exist, agent.gpt takes precedence. These files should typically reside in the root directory. If multiple tools are hosted in one repo, placing the .gpt file in a subdirectory allows the tool to be referenced as github.com/<user>/<repo>/<subdirectory>.

    It is recommended to include Name and Description directives within the .gpt file 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.