Package: openai-function-calling
OpenAI Function CallingHelper functions to generate JSON schema dicts for OpenAI ChatGPT function calling requests. See theofficial Function Calling referencefor more information.InstallationInstall from PyPi with:pipinstallopenai-function-callingThe openai-function-calling package does come with the openai package. It must be installed separately withpip install openaiUsageAuto-Infer the Function Definition (Beta)Automatically infer your function name, description, and parameters given a reference to the function. AFunctioninstance is returned which can be converted to JSON schema with.to_json_schema()and then passed to the OpenAI chat completion API:fromtypingimportAny,Callablefromopenai_function_callingimportFunctionInferrerimportopenaiimportjson# Define example functions.defget_current_weather(location:str,unit:str="fahrenheit")->str:"""Get the current weather and return a summary."""returnf"It is currently sunny in{location}and 75 degrees{unit}."defget_tomorrows_weather(location:str,unit:str="fahrenheit")->str:"""Get the weather for tomorrow and return a summary."""returnf"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 ObjectsDefine your function definitions using typed classesFunctionandParameterwhich automatically convert to JSON schema with.to_json_schemamethods. See an example below:fromopenai_function_callingimportFunction,FunctionDict,Parameter,JsonSchemaTypedefget_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 JSONfromopenaiimportOpenAIfromopenai.types.chatimport(ChatCompletion,ChatCompletionUserMessageParam,)fromopenai_function_calling.tool_helpersimportToolHelpers# Define our functions.defget_current_weather(location:str,unit:str)->str:"""Get the current weather and return a summary."""returnf"It is currently sunny in{location}and 75 degrees{unit}."defget_tomorrows_weather(location:str,unit:str)->str:"""Get tomorrow's weather and return a summary."""returnf"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",)ExamplesTo run the examples, set the environment variableOPENAI_API_KEYto your OpenAI API key. For example:exportOPENAI_API_KEY=SOME_KEY_VALUE# or when running an exampleOPENAI_API_KEY=SOME_KEY_VALUEpythonexamples/weather_functions.pyMake sure to also follow all instructions in theInstallation section.See complete examples in the./examplesfolder.