Skip to main content

Helper functions to generate OpenAI GPT function calling requests.

Project description

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

from openai_function_calling import Function, FunctionDict, Parameter

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

# Define the function parameters.
location_parameter = Parameter(
    name="location",
    type="string",
    description="The city and state, e.g. San Francisco, CA"
)
unit_parameter = Parameter(
    name="unit",
    type="string",
    description="The temperature unit to use.",
    enum=["celsius", "fahrenheit"]
)

# Define the function.
get_current_weather_function = Function(
    "get_current_weather",
    "Get the current weather",
    [location_parameter, unit_parameter],
)

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

# 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.

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

openai_function_calling-0.6.0.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

openai_function_calling-0.6.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file openai_function_calling-0.6.0.tar.gz.

File metadata

File hashes

Hashes for openai_function_calling-0.6.0.tar.gz
Algorithm Hash digest
SHA256 931b488ea55b81ff7b9ffc18a731c406755024875cfbcff9d8da91014260711a
MD5 6eb646099d8daba86a0eeb16b3965920
BLAKE2b-256 eac871db5e6eaf18344433403b19a21a7fb704ee7c1c3254d3ac5f8632fff032

See more details on using hashes here.

File details

Details for the file openai_function_calling-0.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for openai_function_calling-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c1e1e02d04ce65a69a0bb880e6eb1c2d7644d4b812a6e3f15e2b914570fe69c9
MD5 5316d1786bf55f1fe6396821c45a1888
BLAKE2b-256 3ecd92f7ba4f5d6b58b422de911771ecbcb9cb3b558ba376a5f20a151d827e56

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