Skip to main content

Utilities for AI functions

Project description

Functools for OpenAI GPT Functions

OpenAI just released GPT Functions: https://openai.com/blog/function-calling-and-other-api-updates

But it's really cumbersome to make the actual function signature. And manually parse results and chain the calls:

curl https://api.openai.com/v1/chat/completions -u :$OPENAI_API_KEY -H 'Content-Type: application/json' -d '{
  "model": "gpt-3.5-turbo-0613",
  "messages": [
    {"role": "user", "content": "What is the weather like in Boston?"}
  ],
  "functions": [
    {
      "name": "get_current_weather",
      "description": "Get the current weather in a given location",
      "parameters": {
        "type": "object",
        "properties": {
          "location": {
            "type": "string",
            "description": "The city and state, e.g. San Francisco, CA"
          },
          "unit": {
            "type": "string",
            "enum": ["celsius", "fahrenheit"]
          }
        },
        "required": ["location"]
      }
    }
  ]
}'

Use aifunctools instead

Instead, aifunctools automatically parses available python type annotations and docstrings so you don't have to do all of the manual work:

from aifunctools.openai_funcs import complete_with_functions

def get_current_weather(location: str, unit: str) -> dict:
    """
    Get the current weather in a given location

    :param location: The city and state, e.g. San Francisco, CA
    :param unit: Either "celsius" or "fahrenheit"
    :return: the current temperature, unit, and a description
    """
    return {"temperature": 22, "unit": "celsius", "description": "Sunny"}


resp = complete_with_functions("What is the weather like in Boston?",
                               get_current_weather)
# The response should contain: "The weather in Boston is currently sunny with a temperature of 22 degrees Celsius."

What's next

OpenAPI Specs

Python functions are great, but what would really be great is to parse openapi specs from remote services. Imagine where every service provider makes their api spec available and agents can just crawl these APIs and use aifunctools to chain them together.

Richer types

For this hack we only support the basic string / number / boolean types. By making this more flexible we can get much richer behavior.

Error handling

I'm sure there are lots of cases where incomplete docstrings and type signatures will cause errors. We can make these more robust (or use some reasonable defaults).

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

aifunctools-0.1.1.tar.gz (15.2 kB view details)

Uploaded Source

File details

Details for the file aifunctools-0.1.1.tar.gz.

File metadata

  • Download URL: aifunctools-0.1.1.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for aifunctools-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c5f7ffafb16faa646c99c6f835fa184039d0e4df3a38e264b74695e8575be736
MD5 97c9613abf9e95ee49b30b1840395c9d
BLAKE2b-256 46bc4ff716dee4c573bb585e9b9cfa520b79b082e8a5de41c262a6af5231ba76

See more details on using hashes here.

Provenance

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