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

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

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

Uploaded Source

Built Distribution

openai_function_calling-0.4.0-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for openai_function_calling-0.4.0.tar.gz
Algorithm Hash digest
SHA256 97d9aca5fef973cb754f46da7308d774799da41b69495b6b10c10f7c07905f0d
MD5 b65c61067732fb1aaa3bb46fdce46c35
BLAKE2b-256 cc8e56efc7a0d34e193472da22cb28ae1100250a20fb941e5f0d7e2acad36310

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for openai_function_calling-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 11f6bc25b8c38244b27fa1d50fa8172b32ab28a3f38e4b76eb04d1c429ce2e96
MD5 1336744c2b7d3d17c2c644def249beb9
BLAKE2b-256 db97afebfe58fea8b67ae3253ffc75387728926f5d19abe33dac7bad5e221645

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