On June 13th, OpenAI made an exciting announcement: they introduced a new feature called function calling to two of their models. This feature is similar to popular addons on the ChatGPT UI, such as the Wolfram Alpha addon, which is used for performing mathematical calculations. With the function calling API, GPT-4 can call your functions with arguments and essentially run code. Check out some of the coolest implementations I've seen: this, this, and this.
Now, let's dive into a simple example. Imagine we have two implemented functions: get_current_weather
and get_n_day_weather_forecast
. To use these functions, we structure our new parameter in the API call as follows:
functions = [ { "name": "get_current_weather", "description": "Get the current weather", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA", }, "format": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": "The temperature unit to use. Infer this from the users location.", }, }, "required": ["location", "format"], }, }, { "name": "get_n_day_weather_forecast", "description": "Get an N-day weather forecast", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA", }, "format": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": "The temperature unit to use. Infer this from the users location.", }, "num_days": { "type": "integer", "description": "The number of days to forecast", } }, "required": ["location", "format", "num_days"] }, }, ]
Behind the scenes, the chat message combined with the function text above is tokenized and passed to the model as input. So, if you were to ask, "What's the weather like today?", the model can intelligently utilize your function called get_current_weather
from your code and respond with an answer. This functionality works impressively well. GPT-4 demonstrates a remarkable understanding of which functions to invoke in order to fulfill your request.
Function calling opens up endless possibilities for integrating business logic through code. For example, you can empower ChatGPT to execute any query against your database, providing greater flexibility and control. In an example from OpenAI's cookbook, a user queries directly against a SQL database with their chat message:
system: Answer user questions by generating SQL queries against the Chinook Music Database.
user: Hi, who are the top 5 artists by number of tracks?
assistant: {'name': 'ask_database', 'arguments': '{\n "query": "SELECT ar.Name, COUNT(t.TrackId) AS NumTracks FROM Artist ar INNER JOIN Album al ON ar.ArtistId = al.ArtistId INNER JOIN Track t ON al.AlbumId = t.AlbumId GROUP BY ar.ArtistId ORDER BY NumTracks DESC LIMIT 5"\n}'}
function (ask_database): [('Iron Maiden', 213), ('U2', 135), ('Led Zeppelin', 114), ('Metallica', 112), ('Lost', 92)]
Schema Extraction
However, function calling isn't limited to powering proprietary toolsets or enabling code execution. It also offers a unique parsing functionality. This aspect of the new API allows for intriguing applications. Here's an example from Jason Liu on Twitter that uses clever function definitions to force GPT to parse text into a series of rows that fit into a DataFrame.
In fact there’s already a GitHub repo that enables this. You can specify any data schema, and GPT-4 will parse through your text and populate your table with the structured results.
With this new feature, OpenAI continues to push the boundaries of what AI models can achieve, providing a more engaging and dynamic conversational experience.