Function Calling
This article describes a specific capability of chat models: function calling, or simply functions. A list of models that support this feature is provided at the end of this page.
Introduction
When using text (chat) models via the API, you can define functions that the model can choose to call, generating a JSON object with the necessary arguments. The text model API itself does not execute these functions; instead, it outputs the JSON, which you can then use to call the function within your code.
The latest models (gpt-4o, gpt-4-turbo, and gpt-3.5-turbo) are designed to detect when a function should be called based on the input and to produce JSON that closely matches the function signature. However, this functionality comes with potential risks. We strongly recommend implementing user confirmation steps before performing actions that could impact the real world (e.g., sending an email, posting online, making a purchase).
This guide focuses on function calling with our text models API.
Common Use Cases
Function calling allows you to obtain structured data reliably from the model. For example, you can:
Create assistants that answer questions by calling external APIs
Example functions:
send_email(to: string, body: string)
,get_current_weather(location: string, unit: 'celsius' | 'fahrenheit')
Convert natural language into API calls
Example conversion: "Who are my top customers?" to
get_customers(min_revenue: int, created_before: string, limit: int)
, then call your internal API
Extract structured data from text
Example functions:
extract_data(name: string, birthday: string)
,sql_query(query: string)
Basic Sequence of Steps for Function Calling
Call the model with the user query and a set of functions defined in the
functions
parameter.Model response: The model may choose to call one or more functions. If so, it will output a stringified JSON object adhering to your custom schema (note: the model may hallucinate parameters).
Parse the JSON: In your code, parse the string into JSON and call the function with the provided arguments if they exist.
Call the model again: Append the function response as a new message and let the model summarize the results back to the user.
Examples
Models That Support Function Calling
Last updated
Was this helpful?