Skip to main content

Run gptscripts from Python apps

Project description

GPTScript Python Module

Introduction

The GPTScript Python module is a library that provides a simple interface to create and run gptscripts within Python applications, and Jupyter notebooks. It allows you to define tools, execute them, and process the responses.

Installation

You can install the GPTScript Python module using pip.

pip install gptscript

On MacOS, Windows X6

SDIST and none-any wheel installations

When installing from the sdist or the none-any wheel, the binary is not packaged by default. You must run the install_gptscript command to install the binary.

install_gptscript

The script is added to the same bin directory as the python executable, so it should be in your path.

Or you can install the gptscript cli from your code by running:

from gptscript.install import install

install()

Using an existing gptscript cli

If you already have the gptscript cli installed, you can use it by setting the envvar:

export GPTSCRIPT_BIN="/path/to/gptscript"

GPTScript

The GPTScript instance allows the caller to run gptscript files, tools, and other operations (see below). Note that the intention is that a single GPTScript instance is all you need for the life of your application, you should call close() on the instance when you are done.

Global Options

When creating a GTPScript instance, you can pass the following global options. These options are also available as run Options. Anything specified as a run option will take precedence over the global option.

  • APIKey: Specify an OpenAI API key for authenticating requests. Defaults to OPENAI_API_KEY environment variable
  • BaseURL: A base URL for an OpenAI compatible API (the default is https://api.openai.com/v1)
  • DefaultModel: The default model to use for OpenAI requests
  • Env: Supply the environment variables. Supplying anything here means that nothing from the environment is used. The default is os.environ(). Supplying Env at the run/evaluate level will be treated as "additional."

Run Options

These are optional options that can be passed to the run and evaluate functions. None of the options is required, and the defaults will reduce the number of calls made to the Model API. As noted above, the Global Options are also available to specify here. These options would take precedence.

  • disableCache: Enable or disable caching. Default (False).
  • subTool: Use tool of this name, not the first tool
  • input: Input arguments for the tool run
  • workspace: Directory to use for the workspace, if specified it will not be deleted on exit
  • chatState: The chat state to continue, or null to start a new chat and return the state
  • confirm: Prompt before running potentially dangerous commands
  • prompt: Allow prompting of the user

Tools

The Tool class represents a gptscript tool. The fields align with what you would be able to define in a normal gptscript .gpt file.

Fields

  • name: The name of the tool.
  • description: A description of the tool.
  • tools: Additional tools associated with the main tool.
  • maxTokens: The maximum number of tokens to generate.
  • model: The GPT model to use.
  • cache: Whether to use caching for responses.
  • temperature: The temperature parameter for response generation.
  • arguments: Additional arguments for the tool.
  • internalPrompt: Optional boolean defaults to None.
  • instructions: Instructions or additional information about the tool.
  • jsonResponse: Whether the response should be in JSON format.(If you set this to True, you must say 'json' in the instructions as well.)

Primary Functions

Aside from the list methods there are exec and exec_file methods that allow you to execute a tool and get the responses. Those functions also provide a streaming version of execution if you want to process the output streams in your code as the tool is running.

list_tools()

This function lists the available tools.

from gptscript.gptscript import GPTScript


async def list_tools():
    gptscript = GPTScript()
    tools = await gptscript.list_tools()
    print(tools)
    gptscript.close()

list_models()

This function lists the available GPT models.

from gptscript.gptscript import GPTScript


async def list_models():
    gptscript = GPTScript()
    tools = await gptscript.list_models()
    print(tools)
    gptscript.close()

parse()

Parse a file into a Tool data structure.

from gptscript.gptscript import GPTScript


async def parse_example():
    gptscript = GPTScript()
    tools = await gptscript.parse("/path/to/file")
    print(tools)
    gptscript.close()

parse_tool()

Parse the contents that represents a GPTScript file into a Tool data structure.

from gptscript.gptscript import GPTScript


async def parse_tool_example():
    gptscript = GPTScript()
    tools = await gptscript.parse_tool("Instructions: Say hello!")
    print(tools)
    gptscript.close()

fmt()

Parse convert a tool data structure into a GPTScript file.

from gptscript.gptscript import GPTScript


async def fmt_example():
    gptscript = GPTScript()
    tools = await gptscript.parse_tool("Instructions: Say hello!")
    print(tools)

    contents = gptscript.fmt(tools)
    print(contents)  # This would print "Instructions: Say hello!"
    gptscript.close()

evaluate()

Executes a tool with optional arguments.

from gptscript.gptscript import GPTScript
from gptscript.tool import ToolDef


async def evaluate_example():
    tool = ToolDef(instructions="Who was the president of the United States in 1928?")
    gptscript = GPTScript()

    run = gptscript.evaluate(tool)
    output = await run.text()

    print(output)

    gptscript.close()

run()

Executes a GPT script file with optional input and arguments. The script is relative to the callers source directory.

from gptscript.gptscript import GPTScript


async def evaluate_example():
    gptscript = GPTScript()

    run = gptscript.run("/path/to/file")
    output = await run.text()

    print(output)

    gptscript.close()

Streaming events

GPTScript provides events for the various steps it takes. You can get those events and process them with event_handlers. The evaluate method is used here, but the same functionality exists for the run method.

from gptscript.gptscript import GPTScript
from gptscript.frame import RunFrame, CallFrame, PromptFrame
from gptscript.run import Run


async def process_event(run: Run, event: RunFrame | CallFrame | PromptFrame):
    print(event.__dict__)


async def evaluate_example():
    gptscript = GPTScript()

    run = gptscript.run("/path/to/file", event_handlers=[process_event])
    output = await run.text()

    print(output)

    gptscript.close()

Confirm

Using the confirm: true option allows a user to inspect potentially dangerous commands before they are run. The caller has the ability to allow or disallow their running. In order to do this, a caller should look for the CallConfirm event.

from gptscript.gptscript import GPTScript
from gptscript.frame import RunFrame, CallFrame, PromptFrame
from gptscript.run import Run, RunEventType
from gptscript.confirm import AuthResponse

gptscript = GPTScript()


async def confirm(run: Run, event: RunFrame | CallFrame | PromptFrame):
    if event.type == RunEventType.callConfirm:
        # AuthResponse also has a "message" field to specify why the confirm was denied.
        await gptscript.confirm(AuthResponse(accept=True))


async def evaluate_example():
    run = gptscript.run("/path/to/file", event_handlers=[confirm])
    output = await run.text()

    print(output)

    gptscript.close()

Prompt

Using the prompt: true option allows a script to prompt a user for input. In order to do this, a caller should look for the Prompt event. Note that if a Prompt event occurs when it has not explicitly been allowed, then the run will error.

from gptscript.gptscript import GPTScript
from gptscript.frame import RunFrame, CallFrame, PromptFrame
from gptscript.run import Run
from gptscript.opts import Options
from gptscript.prompt import PromptResponse

gptscript = GPTScript()


async def prompt(run: Run, event: RunFrame | CallFrame | PromptFrame):
    if isinstance(event, PromptFrame):
        # The responses field here is a dictionary of prompt fields to values.
        await gptscript.prompt(PromptResponse(id=event.id, responses={event.fields[0]: "Some value"}))


async def evaluate_example():
    run = gptscript.run("/path/to/file", opts=Options(prompt=True), event_handlers=[prompt])
    output = await run.text()

    print(output)

    gptscript.close()

Example Usage

from gptscript.gptscript import GPTScript
from gptscript.tool import ToolDef

# Create the GPTScript object
gptscript = GPTScript()

# Define a tool
complex_tool = ToolDef(
    tools=["sys.write"],
    jsonResponse=True,
    cache=False,
    instructions="""
    Create three short graphic artist descriptions and their muses.
    These should be descriptive and explain their point of view.
    Also come up with a made-up name, they each should be from different
    backgrounds and approach art differently.
    the JSON response format should be:
    {
        artists: [{
            name: "name"
            description: "description"
        }]
    }
    """
)

# Execute the complex tool
run = gptscript.evaluate(complex_tool)
print(await run.text())

gptscript.close()

Example 2 multiple tools

In this example, multiple tool are provided to the exec function. The first tool is the only one that can exclude the name field. These will be joined and passed into the gptscript as a single gptscript.

from gptscript.gptscript import GPTScript
from gptscript.tool import ToolDef

gptscript = GPTScript()

tools = [
    ToolDef(tools=["echo"], instructions="echo hello times"),
    ToolDef(
        name="echo",
        tools=["sys.exec"],
        description="Echo's the input",
        args={"input": "the string input to echo"},
        instructions="""
        #!/bin/bash
        echo ${input}
        """,
    ),
]

run = gptscript.evaluate(tools)

print(await run.text())

gptscript.close()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gptscript-0.8.5.tar.gz (28.1 kB view details)

Uploaded Source

Built Distributions

gptscript-0.8.5-py3-none-win_amd64.whl (8.7 MB view details)

Uploaded Python 3 Windows x86-64

gptscript-0.8.5-py3-none-macosx_10_9_universal2.whl (16.7 MB view details)

Uploaded Python 3 macOS 10.9+ universal2 (ARM64, x86-64)

gptscript-0.8.5-py3-none-any.whl (23.3 kB view details)

Uploaded Python 3

File details

Details for the file gptscript-0.8.5.tar.gz.

File metadata

  • Download URL: gptscript-0.8.5.tar.gz
  • Upload date:
  • Size: 28.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.4

File hashes

Hashes for gptscript-0.8.5.tar.gz
Algorithm Hash digest
SHA256 fdae36b9704a9f218e9b45ae5f86edb8cb536befa7ae7bd5c7d96d6a8da3c4c9
MD5 33c16d39bbbef0712c3cf15139c19369
BLAKE2b-256 d859e8db6bcf8d15c7c362f23017fc3825455ceccf8d4fbd896ce7901bdef2a7

See more details on using hashes here.

File details

Details for the file gptscript-0.8.5-py3-none-win_amd64.whl.

File metadata

  • Download URL: gptscript-0.8.5-py3-none-win_amd64.whl
  • Upload date:
  • Size: 8.7 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.4

File hashes

Hashes for gptscript-0.8.5-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 31d94653de4aa10bf77f8cec83a6f5d31c421116ebf36997014d84a2f1bca496
MD5 0c97f381c945b6d661dcc74506c838f8
BLAKE2b-256 7d412c1176d9910e6a43d191251463ae7f4df3f2f89a738fd1659302918f4e72

See more details on using hashes here.

File details

Details for the file gptscript-0.8.5-py3-none-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gptscript-0.8.5-py3-none-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5d7df75fcb254779a6cc16d8687af1bd02b6d8b2aaecc88f4ca3ae42dc35a6e5
MD5 694f436a508939e939550d9aad102910
BLAKE2b-256 fd7d1c5a18f64c130cce248b3ac512bb9b8aac6e1e20926d37bff4608ac43300

See more details on using hashes here.

File details

Details for the file gptscript-0.8.5-py3-none-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for gptscript-0.8.5-py3-none-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ba52484ceb6f9eed6d2639e5e390070bf1d71a0cdf0b21a649c02d70be533e2d
MD5 ceb455068958b7655fab088e31a48df5
BLAKE2b-256 0d852c278d96fb67e0d72ee4a0df3796321989ef3c02a5e07923cfbc8af8108b

See more details on using hashes here.

File details

Details for the file gptscript-0.8.5-py3-none-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for gptscript-0.8.5-py3-none-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 b1f2abbeb39a704e9d624de487fd61e1e8ce2c2063fc8a240ea1b82b8514ae8b
MD5 671ff794e55476f55520a8e4da768195
BLAKE2b-256 cd944db342870c53fa04b41dfd680fda51e3286ee5ec1fe2b72f316e6c55d5fa

See more details on using hashes here.

File details

Details for the file gptscript-0.8.5-py3-none-any.whl.

File metadata

  • Download URL: gptscript-0.8.5-py3-none-any.whl
  • Upload date:
  • Size: 23.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.4

File hashes

Hashes for gptscript-0.8.5-py3-none-any.whl
Algorithm Hash digest
SHA256 3ad7b5156dc5aa6faaae95249adad082d0edb04b9b1212a1971bcac58aa814f6
MD5 2c075b4de27f4068fd775942238ff9dd
BLAKE2b-256 dcd3a9c5fbe9705f32ab3679e4f44c7342dcae211064b61872484315f6519968

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page