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"

Using the Module

The module requires the OPENAI_API_KEY environment variable to be set with your OPENAI key. You can set it in your shell or in your code.

export OPENAI_AI_KEY="your-key"

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.
  • max_tokens: 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.
  • args: Additional arguments for the tool.
  • internal_prompt: Internal prompt for the tool.
  • instructions: Instructions or additional information about the tool.
  • json_response: 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.

Opts

You can pass the following options to the exec and exec_file functions:

opts= { "cache": True(default)|False, "cache-dir": "", }

Cache can be set to true or false to enable or disable caching globally or it can be set at the individual tool level. The cache-dir can be set to a directory to use for caching. If not set, the default cache directory will be used.

list_models()

This function lists the available GPT models.

from gptscript.command import list_models

models = list_models()
print(models)

list_tools()

This function lists the available tools.

from gptscript.command import list_tools

tools = list_tools()
print(tools)

exec(tool, opts)

This function executes a tool and returns the response.

from gptscript.command import exec
from gptscript.tool import Tool

tool = Tool(
    json_response=True,
    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 response should be in JSON and match the format:
{
   artists: [{
      name: "name"
      description: "description"
   }]
}
""",
    )


response = exec(tool)
print(response)

exec_file(tool_path, input="", opts)

This function executes a tool from a file and returns the response. The input values are passed to the tool as args.

from gptscript.command import exec_file

response = exec_file("./example.gpt")
print(response)

stream_exec(tool, opts)

This function streams the execution of a tool and returns the output, error, and process wait function. The streams must be read from.

from gptscript.command import stream_exec
from gptscript.tool import Tool

tool = Tool(
    json_response=True,
    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 response should be in JSON and match the format:
{
   artists: [{
      name: "name"
      description: "description"
   }]
}
""",
    )

def print_output(out, err):
    # Error stream has the debug info that is useful to see
    for line in err:
        print(line)

    for line in out:
        print(line)

out, err, wait = stream_exec(tool)
print_output(out, err)
wait()

stream_exec_file(tool_path, input="",opts)

This function streams the execution of a tool from a file and returns the output, error, and process wait function. The input values are passed to the tool as args.

from gptscript.command import stream_exec_file

def print_output(out, err):
    # Error stream has the debug info that is useful to see
    for line in err:
        print(line)

    for line in out:
        print(line)

out, err, wait = stream_exec_file("./init.gpt")
print_output(out, err)
wait()

Example Usage

from gptscript.command import exec
from gptscript.tool import FreeForm, Tool

# Define a simple tool
simple_tool = FreeForm(
    content="""
What is the capital of the United States?
"""
)

# Define a complex tool
complex_tool = Tool(
    tools=["sys.write"],
    json_response=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
response, err = exec(complex_tool)
print(err)
print(response)

# Execute the simple tool
resp, err = exec(simple_tool)
print(err)
print(resp)

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 gpt script.

from gptscript.command import exec
from gptscript.tool import Tool

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

resp, err = exec(tools)
print(err)
print(resp)

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.2.0.tar.gz (17.7 kB view details)

Uploaded Source

Built Distributions

gptscript-0.2.0-py3-none-win_amd64.whl (6.6 MB view details)

Uploaded Python 3 Windows x86-64

gptscript-0.2.0-py3-none-macosx_10_9_universal2.whl (12.8 MB view details)

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

gptscript-0.2.0-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for gptscript-0.2.0.tar.gz
Algorithm Hash digest
SHA256 780f2b332b79797a029eb282b06e9cb78dccc927491848ce124e77177db2e0f7
MD5 77eb8c198611e04e1efa04f21a58b2d0
BLAKE2b-256 5051296441c30fb1a7b5a860932ff2a11c68cdce577cdfe4aaa61607689833b8

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for gptscript-0.2.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 6d69da2d1d34d2bc8240b5267ceb595cf2613f034f7b9a99bd3e72627495600a
MD5 2390e5844a356bab045d43ba123e829d
BLAKE2b-256 63a111209980d049328ccdfb9a56e8d91276908dd67d77faa5ab624bd70fa268

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gptscript-0.2.0-py3-none-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6c532efca37aca1d4c9f9e4b33fda83af97aac1f7a32f26a6c4917949a48aa34
MD5 a8a1252a71521c29a2b88cf02d3259e7
BLAKE2b-256 49e03dc493872cc364e6fa949bb79206efd8c44bf5a95b0229855eee19081b63

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gptscript-0.2.0-py3-none-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 07e9613f0e4c6ac96b67c1e4f9ca129ba7ef20e378bbb2d8e083abb700c91ebb
MD5 52665b14242811de9d6d337c8ae43494
BLAKE2b-256 516055fedb5cfc2db3234daf0534f46d47c24a5722888020910070bd11391b36

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gptscript-0.2.0-py3-none-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 05f07841c11edf68aba65476cdd32874f5c745014ae6ba37bb7bcbaae65fe8ca
MD5 eec405f0af0601839ff1177c2e7f3a70
BLAKE2b-256 1cb82529462d56c05fe748a41704a4433d39a8e545ac716705094cf76b9324a5

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for gptscript-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b8b15db846ce79522c25d78000fdb9c89e0a46731760f8be0833c5778cd37332
MD5 d18c796b5fe70ec7eb3e8c6fad0bb641
BLAKE2b-256 248c8f08abe0d671303ac1e99e73b8ad32d7a40f952593d38c318258cda0afe9

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