How To Call Functions With Chat Models
How to call functions with chat models
This notebook covers how to use the Chat Completions API in combination with external functions to extend the capabilities of GPT models.
tools is an optional parameter in the Chat Completion API which can be used to provide function specifications. The purpose of this is to enable models to generate function arguments which adhere to the provided specifications. Note that the API will not actually execute any function calls. It is up to developers to execute function calls using model outputs.
Within the tools parameter, if the functions parameter is provided then by default the model will decide when it is appropriate to use one of the functions. The API can be forced to use a specific function by setting the tool_choice parameter to {"type": "function", "function": {"name": "my_function"}}. The API can also be forced to not use any function by setting the tool_choice parameter to "none". If a function is used, the output will contain "finish_reason": "tool_calls" in the response, as well as a tool_calls object that has the name of the function and the generated function arguments.
Overview
This notebook contains the following 2 sections:
- How to generate function arguments: Specify a set of functions and use the API to generate function arguments.
- How to call functions with model generated arguments: Close the loop by actually executing functions with model generated arguments.
How to generate function arguments
Utilities
First let's define a few utilities for making calls to the Chat Completions API and for maintaining and keeping track of the conversation state.
Basic concepts
Let's create some function specifications to interface with a hypothetical weather API. We'll pass these function specification to the Chat Completions API in order to generate function arguments that adhere to the specification.
If we prompt the model about the current weather, it will respond with some clarifying questions.
system: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous. user: What's the weather like today assistant: Sure—what city and state (or country) should I check? Also, do you prefer Celsius or Fahrenheit?
Once we provide the missing information, it will generate the appropriate function arguments for us.
system: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous. user: What's the weather like today assistant: Sure—what city and state (or country) should I check? Also, do you prefer Celsius or Fahrenheit? user: I'm in Glasgow, Scotland. assistant: [{'id': 'call_k2QgGc9GT9WjxD76GvR0Ot8q', 'function': {'arguments': '{"location": "Glasgow, Scotland", "format": "celsius"}', 'name': 'get_current_weather'}, 'type': 'function'}, {'id': 'call_RtnXV5t49lqbWwhvGoEPZ7KY', 'function': {'arguments': '{"location": "Glasgow, Scotland", "format": "celsius", "num_days": 1}', 'name': 'get_n_day_weather_forecast'}, 'type': 'function'}]
By prompting it differently, we can get it to target the other function we've told it about.
system: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous. user: what is the weather going to be like in Glasgow, Scotland over the next x days assistant: How many days would you like the forecast for in Glasgow, Scotland? For example: 3, 5, 7, 10, or 14.
Once again, the model is asking us for clarification because it doesn't have enough information yet. In this case it already knows the location for the forecast, but it needs to know how many days are required in the forecast.
system: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous. user: what is the weather going to be like in Glasgow, Scotland over the next x days assistant: How many days would you like the forecast for in Glasgow, Scotland? For example: 3, 5, 7, 10, or 14. user: 5 days assistant: [{'id': 'call_lNzOVLrNSaSVjL3O3bN110af', 'function': {'arguments': '{"location":"Glasgow, Scotland","format":"celsius","num_days":5}', 'name': 'get_n_day_weather_forecast'}, 'type': 'function'}]
Forcing the use of specific functions or no function
We can force the model to use a specific function, for example get_n_day_weather_forecast by using the function_call argument. By doing so, we force the model to make assumptions about how to use it.
system: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous. user: Give me a weather report for Toronto, Canada. assistant: [{'id': 'call_3hoMjl55OQ7LxfwhFyjxwv1T', 'function': {'arguments': '{"location":"Toronto, Canada","format":"celsius","num_days":5}', 'name': 'get_n_day_weather_forecast'}, 'type': 'function'}]
system: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous. user: Give me a weather report for Toronto, Canada. assistant: [{'id': 'call_wv5mdjEQJnBPuSci3xw09Tom', 'function': {'arguments': '{"location":"Toronto, ON","format":"celsius"}', 'name': 'get_current_weather'}, 'type': 'function'}]
We can also force the model to not use a function at all. By doing so we prevent it from producing a proper function call.
system: Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous. user: Give me the current weather (use Celcius) for Toronto, Canada. assistant: I don’t have live access to pull the current conditions right now. To get Toronto’s current weather in Celsius, check any of these quickly: - Environment Canada: weather.gc.ca (search “Toronto”) - The Weather Network: theweathernetwork.com/ca/weather/ontario/toronto - Google: search “Toronto weather” (shows °C by default in Canada) - AccuWeather or Weather.com (set units to °C) If you paste the current readings here (temperature, feels-like, wind, precipitation), I can interpret them and advise on what to wear or plan for.
Parallel Function Calling
Newer models such as gpt-5, gpt-4.1 or gpt-4o can call multiple functions in one turn.
[ChatCompletionMessageFunctionToolCall(id='call_KlZ3Fqt3SviC6o66dVMYSa2Q', function=Function(arguments='{"location": "San Francisco, CA", "format": "fahrenheit", "num_days": 4}', name='get_n_day_weather_forecast'), type='function'),
, ChatCompletionMessageFunctionToolCall(id='call_YAnH0VRB3oqjqivcGj3Cd8YA', function=Function(arguments='{"location": "Glasgow, UK", "format": "celsius", "num_days": 4}', name='get_n_day_weather_forecast'), type='function')] How to call functions with model generated arguments
In our next example, we'll demonstrate how to execute functions whose inputs are model-generated, and use this to implement an agent that can answer questions for us about a database. For simplicity we'll use the Chinook sample database.
Note: SQL generation can be high-risk in a production environment since models are not perfectly reliable at generating correct SQL.
Specifying a function to execute SQL queries
First let's define some helpful utility functions to extract data from a SQLite database.
Opened database successfully
Now we can use these utility functions to extract a representation of the database schema.
As before, we'll define a function specification for the function we'd like the API to generate arguments for. Notice that we are inserting the database schema into the function specification. This will be important for the model to know about.
Executing SQL queries
Now let's implement the function that will actually excute queries against the database.
Steps to invoke a function call using Chat Completions API:
Step 1: Prompt the model with content that may result in model selecting a tool to use. The description of the tools such as a function name and signature is defined in the 'Tools' list and passed to the model in API call. If selected, the function name and parameters are included in the response.
Step 2: Check programmatically if model wanted to call a function. If true, proceed to step 3.
Step 3: Extract the function name and parameters from response, call the function with parameters. Append the result to messages.
Step 4: Invoke the chat completions API with the message list to get the response.
user: What is the name of the album with the most tracks? assistant: [{'id': 'call_pGRtZZGfd2o41GHlZcEdB9he', 'function': {'arguments': '{"query":"WITH track_counts AS (\\n SELECT a.AlbumId, a.Title, COUNT(t.TrackId) AS track_count\\n FROM Album a\\n JOIN Track t ON t.AlbumId = a.AlbumId\\n GROUP BY a.AlbumId, a.Title\\n)\\nSELECT Title\\nFROM track_counts\\nWHERE track_count = (SELECT MAX(track_count) FROM track_counts);"}', 'name': 'ask_database'}, 'type': 'function'}]
Result found in database: Greatest Hits
Next Steps
See our other notebook that demonstrates how to use the Chat Completions API and functions for knowledge retrieval to interact conversationally with a knowledge base.