Function Calling - OpenAI API
Function Calling - OpenAI API
Function calling
Learn how to connect large language models to external tools.
Introduction
In an API call, you can describe functions and have the model intelligently choose to
output a JSON object containing arguments to call one or many functions. The Chat
Completions API does not call the function; instead, the model generates JSON that you
can use to call the function in your code.
This guide is focused on function calling with the Chat Completions API, for
details on function calling in the Assistants API, please see the Assistants Tools
page.
Create assistants that answer questions by calling external APIs (e.g. like ChatGPT
Plugins)
e.g. define functions like send_email(to: string, body: string) , or
get_current_weather(location: string, unit: 'celsius' |
'fahrenheit')
1 Call the model with the user query and a set of functions defined in the functions
parameter.
2 The model can choose to call one or more functions; if so, the content will be a
stringified JSON object adhering to your custom schema (note: the model may
hallucinate parameters).
3 Parse the string into JSON in your code, and call your function with the provided
arguments if they exist.
4 Call the model again by appending the function response as a new message, and let
the model summarize the results back to the user.
Supported models
Not all model versions are trained with function calling data. Function calling is supported
with the following models:
gpt-4
gpt-4-1106-preview
gpt-4-0613
gpt-3.5-turbo
gpt-3.5-turbo-1106
gpt-3.5-turbo-0613
gpt-4-1106-preview
gpt-3.5-turbo-1106
https://platform.openai.com/docs/guides/function-calling 2/5
11/13/23, 8:57 PM Function calling - OpenAI API
example, the model may call functions to get the weather in 3 different locations at the
same time, which will result in a message with 3 function calls in the tool_calls array,
each with an id . To respond to these function calls, add 3 new messages to the
conversation, each containing the result of one function call, with a tool_call_id
referencing the id from tool_calls .
In this example, we define a single function get_current_weather . The model calls the
function multiple times, and after sending the function response back to the model, we let
it decide the next step. It responded with a user-facing message which was telling the user
the temperature in San Francisco, Tokyo, and Paris. Depending on the query, it may
choose to call a function again.
If you want to force the model to call a specific function you can do so by setting
tool_choice with a specific function name. You can also force the model to generate a
user-facing message by setting tool_choice: "none" . Note that the default behavior
( tool_choice: "auto" ) is for the model to decide on its own whether to call a function
and if so which function to call.
python Copy
https://platform.openai.com/docs/guides/function-calling 3/5
11/13/23, 8:57 PM Function calling - OpenAI API
69 "tool_call_id": tool_call.id,
70 "role": "tool",
71 "name": function_name,
72 "content": function_response,
73 }
74 ) # extend conversation with function response
75 second_response = client.chat.completions.create(
76 model="gpt-3.5-turbo-1106",
77 messages=messages,
78 ) # get a new response from the model where it can see the function
79 return second_response
80 print(run_conversation())
Collapse
You can find more examples of function calling in the OpenAI Cookbook:
Function calling
Learn from more examples demonstrating function calling
Tokens
Under the hood, functions are injected into the system message in a syntax the model has
been trained on. This means functions count against the model's context limit and are
billed as input tokens. If running into context limits, we suggest limiting the number of
functions or the length of documentation you provide for function parameters.
It is also possible to use fine-tuning to reduce the number of tokens used if you have many
functions defined.
https://platform.openai.com/docs/guides/function-calling 5/5