Helper functions to generate OpenAI GPT function calling requests.
Project description
OpenAI Function Calling
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, JsonSchemaType
def get_current_weather(location: str, unit: str) -> str:
# Do some stuff here...
# Define the function parameters.
location_parameter = Parameter(
name="location",
type=JsonSchemaType.STRING,
description="The city and state, e.g. San Francisco, CA"
)
unit_parameter = Parameter(
name="unit",
type=JsonSchemaType.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_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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file openai_function_calling-1.0.1.tar.gz
.
File metadata
- Download URL: openai_function_calling-1.0.1.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.2 CPython/3.10.9 Darwin/22.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | de5b0b5d2e5d99752e87fa428639a8cd36319e31b4b86ca67f002f6bbc2d3c47 |
|
MD5 | ffe70553e1b684a2d7d1e9a18fd53d1f |
|
BLAKE2b-256 | a3329973c18d352d80c792233dc3bc07d3f135300115ce28be3da9efdc0a0c02 |
File details
Details for the file openai_function_calling-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: openai_function_calling-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.2 CPython/3.10.9 Darwin/22.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b7499aa668104742269c2f5687dcef48d29ebc97253001b9ae29bd1da5a434d |
|
MD5 | d283a98025e1e07f849d39c6fe4071ba |
|
BLAKE2b-256 | 6283d9288fa7562558b2a617a59d4a025b03269682305f477ea54c71cac7ce32 |