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
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:
from typing import Any, Callable
from openai_function_calling import FunctionInferrer
import openai
import json
# Define example functions.
def get_current_weather(location: str, unit: str = "fahrenheit") -> str:
"""Get the current weather and return a summary."""
return f"It is currently sunny in {location} and 75 degrees {unit}."
def get_tomorrows_weather(location: str, unit: str = "fahrenheit") -> str:
"""Get the weather for tomorrow and return a summary."""
return f"Tomorrow it will be rainy in {location} and 60 degrees {unit}."
# Infer the function definitions.
get_current_weather_function = FunctionInferrer.infer_from_function_reference(
get_current_weather
)
get_tomorrows_weather_function = FunctionInferrer.infer_from_function_reference(
get_tomorrows_weather
)
# 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=[
# Convert the functions to JSON schema.
get_current_weather_function.to_json_schema(),
get_tomorrows_weather_function.to_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_schema = get_current_weather_function.to_json_schema()
Convert Functions to OpenAI Compatible JSON
from openai import OpenAI
from openai.types.chat import (
ChatCompletion,
ChatCompletionUserMessageParam,
)
from openai_function_calling.tool_helpers import ToolHelpers
# Define our functions.
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}."
def get_tomorrows_weather(location: str, unit: str) -> str:
"""Get tomorrow's weather and return a summary."""
return f"It will be rainy tomorrow in {location} and around 65 degrees {unit}."
openai_client = OpenAI()
# Send the query and our function context to OpenAI.
response: ChatCompletion = openai_client.chat.completions.create(
model="gpt-3.5-turbo-1106",
messages=[
ChatCompletionUserMessageParam(
role="user", content="What's the weather in Boston MA?"
),
],
tools=ToolHelpers.infer_from_function_refs(
[get_current_weather, get_tomorrows_weather]
),
tool_choice="auto",
)
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.
Metadata
Release files for openai-function-calling 2.6.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| openai_function_calling-2.6.0.tar.gz | 8.6 kB | Without provenance | Details for openai_function_calling-2.6.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| openai_function_calling-2.6.0-py3-none-any.whl | Python 3 | none | any | Details for openai_function_calling-2.6.0-py3-none-any.whl |
Total release size: 18.6 kB
Release files / openai_function_calling-2.6.0.tar.gz
| Download URL | openai_function_calling-2.6.0.tar.gz |
|---|---|
| Size | 8.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
51ef229cc45ac1deee82fe32b66067c52d9cc2366c13e7e9c266010964657bea
|
|
BLAKE2b-256 checksum How to use checksums |
ae72cd055b948d65cd63616f539d85848710353074edc34b3eefddbde88c4b7a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.16
|
Release files / openai_function_calling-2.6.0-py3-none-any.whl
| Download URL | openai_function_calling-2.6.0-py3-none-any.whl |
|---|---|
| Size | 10.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4a640c6c31a834ea8b7743b5293dac6160101c2f6ed90314e19d4edfdce55fbd
|
|
BLAKE2b-256 checksum How to use checksums |
3bcec8532365582ccd1e96460ebf6909197f7f744b9506b565e19c5abd2fde13
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.16
|
Security for openai-function-calling 2.6.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.