Skip to main content

A library to generate function schemas for use in the OpenAI API.

Project description

tool2schema

Check Code

A library to convert Python functions to schemas supported by the OpenAI API.

Inspired by janekb04/py2gpt and fastai/lm-hackers.

Why tool2schema?

The OpenAI API supports function calling. However, to tell GPT what functions it can call, you must send the functions in a JSON format. With tool2schema, functions can be automatically converted to the correct JSON schema!

Usage

On all functions that you would like to get JSON schema for, simply add the GPTEnabled decorator.

# my_functions.py
from tool2schema import GPTEnabled

@GPTEnabled
def my_function2(a: int, b: str = "Hello"):
    """
    Example function description.

    :param a: First parameter;
    :param b: Second parameter;
    """
    # Function code here...

@GPTEnabled(tags=["tag1"])
def my_function2(a: int, b: str = "Hello"):
    """
    Example function description.

    :param a: First parameter;
    :param b: Second parameter;
    """
    # Function code here...

tool2schema provides some methods to easily retrieve your functions.

import my_functions  # Module containing your functions
from tool2schema import FindGPTEnabled

# Return functions with GPTEnabled decorator
gpt_enable = FindGPTEnabled(my_functions)

# Return all function schemas
schemas = FindGPTEnabledSchemas(my_functions)

# Return function with given name
f = FindGPTEnabledByName(my_functions, "my_function1")

# Returns all functions with given tag
fs = FindGPTEnabledByTag(my_functions, "tag1")

How it Works

tool2schema uses certain features of your function to correctly populate the schema.

  • Parameter type hints
  • Parameter default values
  • Docstring with parameter descriptions

The docstring must be of a specific format. An example function is defined below that utilises all of the above features.

def my_function(a: int, b: str = "Hello"):
    """
    Example function description.

    :param a: First parameter;
    :param b: Second parameter;
    """

Note the ; at the end of each parameter description. This is used to indicate the end of the parameter description.

To get the schema for this function, simply use the GPTEnabled decorator. The decorator will return a class with some additional attributes but can still be called as a function.

The schema of the function be accessed using the schema attribute.

my_function.schema.to_json()

This returns the function schema in JSON format.

Supported Parameter Types

The following parameter types are supported:

  • int
  • float
  • str
  • bool
  • list

Any other parameter types will be listed as object in the schema.

Enumerations

If you want to limit the possible values of a parameter, you can use the enum keyword argument.

@GPTEnabled
def my_function(a: int, b: str,):
    """
    Example function description.

    :param a: First parameter;
    :param b: Second parameter;
    """
    # Function code here...
my_function.schema.add_enum("b", ["yes", "no"])

The schema will then be updated to include the enum keyword.

Tags

The GPTEnabled decorator also supports the tags keyword argument. This allows you to add tags to your function schema.

@GPTEnabled(tags=["tag1", "tag2"])
def my_function(a: int, b: str = "Hello"):
    """
    Example function description.

    :param a: First parameter;
    :param b: Second parameter;
    """
    # Function code here...

The tags can then be accessed using the tags attribute.

my_function.tags  # ["tag1", "tag2"]

You can check if a function has a certain tag using the has_tag method.

my_function.has_tag("tag1")  # True

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

tool2schema-0.4.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

tool2schema-0.4.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file tool2schema-0.4.0.tar.gz.

File metadata

  • Download URL: tool2schema-0.4.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.6 Windows/10

File hashes

Hashes for tool2schema-0.4.0.tar.gz
Algorithm Hash digest
SHA256 5259c98af0e57c3befcbec0f3b6e79fb8a929e54ccf03e9ffd92a08996de3798
MD5 0e92f6809ab0ccf7e86a3ce68007693d
BLAKE2b-256 6cad3708b1cef00106a345eeb56199ae5b8779018e7a3e44e017c849e127c1ba

See more details on using hashes here.

File details

Details for the file tool2schema-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: tool2schema-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.6 Windows/10

File hashes

Hashes for tool2schema-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 456d2884ca8f043574602a88046ca44b60b01907b0a101729b30e22fd7ba6f25
MD5 a15dce9cc40aeba54c7ff11558294aab
BLAKE2b-256 27e0ce01be9001c733cdf22b8c864faf05db7d6153d36d2b912ef9952b9acb2d

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