Skip to main content

OpenAI Function Calling

GitHub Actions Build Status

Helper functions to generate JSON schema dicts for OpenAI ChatGPT function calling requests. See the official Function Calling reference for more information.

Installation

Install from PyPi with:

pip install openai-function-calling

The openai-function-calling package does come with the openai package. It must be installed separately with pip install openai

Usage

Auto-Infer the Function Definition (Beta)

Automatically infer your function name, description, and parameters given a reference to the function. A Function instance is returned which can be converted to JSON schema with .to_json_schema() and then passed to the OpenAI chat completion API:

def get_current_weather(location: str, unit: str) -> str:
    """Get the current weather and return a summary."""
    return f"It is currently sunny in {location} and 75 degrees {unit}."

get_current_weather_json_schema = Function.from_function(get_current_weather).to_json_schema()

# Get the function to call from ChatGPT (you would normally have more than one).
response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-0613",
    messages=[
        {
            "role": "user",
            "content": "What will the weather be like in Boston, MA today?",
        }
    ],
    functions=[get_current_weather_json_schema],
)

Define Functions with Objects

Define your function definitions using typed classes Function and Parameter which automatically convert to JSON schema with .to_json_schema methods. See an example below:

from openai_function_calling import Function, FunctionDict, Parameter, JsonSchemaType


def get_current_weather(location: str, unit: str) -> str:
    """Do some stuff in here."""


# Define the function.
get_current_weather_function = Function(
    "get_current_weather",
    "Get the current weather",
    [
        Parameter(
            name="location",
            type=JsonSchemaType.STRING,
            description="The city and state, e.g. San Francisco, CA",
        ),
        Parameter(
            name="unit",
            type=JsonSchemaType.STRING,
            description="The temperature unit to use.",
            enum=["celsius", "fahrenheit"],
        ),
    ],
)

# Convert to a JSON schema dict to send to OpenAI.
get_current_weather_function_dict = get_current_weather_function.to_json_schema()

# Get the function to call from ChatGPT.
response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-0613",
    messages=[
        {
            "role": "user",
            "content": "What will the weather be like in Boston, MA tomorrow?",
        }
    ],
    functions=[get_current_weather_function_dict],
)

Examples

To run the examples, set the environment variable OPENAI_API_KEY to your OpenAI API key. For example:

export OPENAI_API_KEY=SOME_KEY_VALUE

# or when running an example

OPENAI_API_KEY=SOME_KEY_VALUE python examples/weather_functions.py

Make sure to also follow all instructions in the Installation section.

See complete examples in the ./examples folder.

Release files for openai-function-calling 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openai-function-calling 1.1.0
File Size Uploaded
openai_function_calling-1.1.0.tar.gz 6.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openai-function-calling 1.1.0
File Interpreter ABI Platform
openai_function_calling-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.0 kB

Release files / openai_function_calling-1.1.0.tar.gz

Download URL openai_function_calling-1.1.0.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
228893435deb98220d783f119eae6737dc9ad6b882cec68ca3bd66d20a88e5e2
BLAKE2b-256 checksum
How to use checksums
5108074fd05943262024669529c444c8eabeb2163f8b0dafd20452c6d4b4ed1a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.10.9 Darwin/22.6.0

Release files / openai_function_calling-1.1.0-py3-none-any.whl

Download URL openai_function_calling-1.1.0-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
246743598164e7bbde19a0652b5b544ba8f6d2e7e39f8739858572767388f11e
BLAKE2b-256 checksum
How to use checksums
6bf6cc72abf029b65948fdfe404263f27103a539732d39539236cdd7fcd4b4ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.10.9 Darwin/22.6.0

Release history Release notifications | RSS feed

2.6.0

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.2

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.2.0

2 release files

1.1.1

2 release files

This release

1.1.0 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page