# OpenAI Function Calling
[![GitHub Actions Build Status](https://github.com/jakecyr/openai-function-calling/actions/workflows/test-application.yml/badge.svg)](https://github.com/jakecyr/openai-function-calling/actions)
[![PyPi Package Version](https://badge.fury.io/py/openai-function-calling.svg)](https://pypi.org/project/openai-function-calling/)
Helper functions to generate JSON schema dicts for OpenAI ChatGPT function calling requests. See the [official Function Calling reference](https://platform.openai.com/docs/guides/gpt/function-calling) for more information.
## Installation
Install from PyPi with:
```bash
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:
```python
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:
```python
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
```python
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:
```bash
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](#installation).
See complete examples in the [./examples](https://github.com/jakecyr/openai-function-calling/tree/master/examples) folder.
Raw data
{
"_id": null,
"home_page": "https://github.com/jakecyr/openai-function-calling",
"name": "openai-function-calling",
"maintainer": null,
"docs_url": null,
"requires_python": "<=3.13,>=3.9",
"maintainer_email": null,
"keywords": null,
"author": "Jake Cyr",
"author_email": "cyrjake@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/9d/95/48cbbe216655dfab8d401c65e7e941e82e9c64d67cfa3fd8d4bde14610ce/openai_function_calling-2.3.0.tar.gz",
"platform": null,
"description": "# OpenAI Function Calling\n\n[![GitHub Actions Build Status](https://github.com/jakecyr/openai-function-calling/actions/workflows/test-application.yml/badge.svg)](https://github.com/jakecyr/openai-function-calling/actions)\n[![PyPi Package Version](https://badge.fury.io/py/openai-function-calling.svg)](https://pypi.org/project/openai-function-calling/)\n\nHelper functions to generate JSON schema dicts for OpenAI ChatGPT function calling requests. See the [official Function Calling reference](https://platform.openai.com/docs/guides/gpt/function-calling) for more information.\n\n## Installation\n\nInstall from PyPi with:\n\n```bash\npip install openai-function-calling\n```\n\n**The openai-function-calling package does come with the openai package. It must be installed separately with `pip install openai`**\n\n## Usage\n\n### Auto-Infer the Function Definition (Beta)\n\nAutomatically 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:\n\n```python\nfrom typing import Any, Callable\nfrom openai_function_calling import FunctionInferrer\nimport openai\nimport json\n\n# Define example functions.\n\ndef get_current_weather(location: str, unit: str = \"fahrenheit\") -> str:\n \"\"\"Get the current weather and return a summary.\"\"\"\n return f\"It is currently sunny in {location} and 75 degrees {unit}.\"\n\n\ndef get_tomorrows_weather(location: str, unit: str = \"fahrenheit\") -> str:\n \"\"\"Get the weather for tomorrow and return a summary.\"\"\"\n return f\"Tomorrow it will be rainy in {location} and 60 degrees {unit}.\"\n\n# Infer the function definitions.\nget_current_weather_function = FunctionInferrer.infer_from_function_reference(\n get_current_weather\n)\n\nget_tomorrows_weather_function = FunctionInferrer.infer_from_function_reference(\n get_tomorrows_weather\n)\n\n# Get the function to call from ChatGPT (you would normally have more than one).\nresponse = openai.ChatCompletion.create(\n model=\"gpt-3.5-turbo-0613\",\n messages=[\n {\n \"role\": \"user\",\n \"content\": \"What will the weather be like in Boston, MA today?\",\n }\n ],\n functions=[\n # Convert the functions to JSON schema.\n get_current_weather_function.to_json_schema(),\n get_tomorrows_weather_function.to_json_schema(),\n ],\n)\n```\n\n### Define Functions with Objects\n\nDefine your function definitions using typed classes `Function` and `Parameter` which automatically convert to JSON schema with `.to_json_schema` methods. See an example below:\n\n```python\nfrom openai_function_calling import Function, FunctionDict, Parameter, JsonSchemaType\n\n\ndef get_current_weather(location: str, unit: str) -> str:\n \"\"\"Do some stuff in here.\"\"\"\n\n\n# Define the function.\nget_current_weather_function = Function(\n \"get_current_weather\",\n \"Get the current weather\",\n [\n Parameter(\n name=\"location\",\n type=JsonSchemaType.STRING,\n description=\"The city and state, e.g. San Francisco, CA\",\n ),\n Parameter(\n name=\"unit\",\n type=JsonSchemaType.STRING,\n description=\"The temperature unit to use.\",\n enum=[\"celsius\", \"fahrenheit\"],\n ),\n ],\n)\n\n# Convert to a JSON schema dict to send to OpenAI.\nget_current_weather_function_schema = get_current_weather_function.to_json_schema()\n```\n\n### Convert Functions to OpenAI Compatible JSON\n\n```python\nfrom openai import OpenAI\nfrom openai.types.chat import (\n ChatCompletion,\n ChatCompletionUserMessageParam,\n)\nfrom openai_function_calling.tool_helpers import ToolHelpers\n\n\n# Define our functions.\ndef get_current_weather(location: str, unit: str) -> str:\n \"\"\"Get the current weather and return a summary.\"\"\"\n return f\"It is currently sunny in {location} and 75 degrees {unit}.\"\n\n\ndef get_tomorrows_weather(location: str, unit: str) -> str:\n \"\"\"Get tomorrow's weather and return a summary.\"\"\"\n return f\"It will be rainy tomorrow in {location} and around 65 degrees {unit}.\"\n\n\nopenai_client = OpenAI()\n\n# Send the query and our function context to OpenAI.\nresponse: ChatCompletion = openai_client.chat.completions.create(\n model=\"gpt-3.5-turbo-1106\",\n messages=[\n ChatCompletionUserMessageParam(\n role=\"user\", content=\"What's the weather in Boston MA?\"\n ),\n ],\n tools=ToolHelpers.infer_from_function_refs(\n [get_current_weather, get_tomorrows_weather]\n ),\n tool_choice=\"auto\",\n)\n```\n\n## Examples\n\nTo run the examples, set the environment variable `OPENAI_API_KEY` to your OpenAI API key. For example:\n\n```bash\nexport OPENAI_API_KEY=SOME_KEY_VALUE\n\n# or when running an example\n\nOPENAI_API_KEY=SOME_KEY_VALUE python examples/weather_functions.py\n```\n\nMake sure to also follow all instructions in the [Installation section](#installation).\n\nSee complete examples in the [./examples](https://github.com/jakecyr/openai-function-calling/tree/master/examples) folder.\n\n",
"bugtrack_url": null,
"license": null,
"summary": "Helper functions to generate OpenAI GPT function calling requests.",
"version": "2.3.0",
"project_urls": {
"Homepage": "https://github.com/jakecyr/openai-function-calling",
"Repository": "https://github.com/jakecyr/openai-function-calling"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8b43ffe88e3c3bc82166e92f17c1330869e25f10477f11a8c4a10f42898e8d33",
"md5": "8f1202b352bf39c4551bd525c72c2e61",
"sha256": "448dc8b8b184c46a4d9e66270929630c77567c0430291ca7cee210e74027ed30"
},
"downloads": -1,
"filename": "openai_function_calling-2.3.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8f1202b352bf39c4551bd525c72c2e61",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<=3.13,>=3.9",
"size": 9951,
"upload_time": "2024-11-11T13:38:07",
"upload_time_iso_8601": "2024-11-11T13:38:07.241265Z",
"url": "https://files.pythonhosted.org/packages/8b/43/ffe88e3c3bc82166e92f17c1330869e25f10477f11a8c4a10f42898e8d33/openai_function_calling-2.3.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9d9548cbbe216655dfab8d401c65e7e941e82e9c64d67cfa3fd8d4bde14610ce",
"md5": "c95bc3403e41b4ab27d5d37b78720835",
"sha256": "af98bf0433dbee5e9846a4c54e594e49c37a33d157f2b6900b5914c4c9c03141"
},
"downloads": -1,
"filename": "openai_function_calling-2.3.0.tar.gz",
"has_sig": false,
"md5_digest": "c95bc3403e41b4ab27d5d37b78720835",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<=3.13,>=3.9",
"size": 8556,
"upload_time": "2024-11-11T13:38:08",
"upload_time_iso_8601": "2024-11-11T13:38:08.133748Z",
"url": "https://files.pythonhosted.org/packages/9d/95/48cbbe216655dfab8d401c65e7e941e82e9c64d67cfa3fd8d4bde14610ce/openai_function_calling-2.3.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-11 13:38:08",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "jakecyr",
"github_project": "openai-function-calling",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "openai-function-calling"
}