Introduction to Function Calling
Function calling is a feature provided by OpenAI that allows developers to describe specific functions to models like gpt-4 and gpt-3.5-turbo. The model can then choose to output a JSON object containing the necessary arguments for those functions. It essentially integrates GPT’s capabilities with external tools and APIs in a seamless manner.
OpenAI’s models have been enhanced to determine when to call a function based on the user’s input. Additionally, they respond with JSON formatted according to the function’s signature. This results in:
Creating Chatbots: Developers can build chatbots that use external tools, such as ChatGPT Plugins.
Natural Language Conversion: Convert user queries like “Email Anya about coffee next Friday” into structured function calls. For instance, transforming it to
send_email(to: string, body: string)
. Another example would be converting “What’s the weather in Boston?” toget_current_weather(location: string, unit: 'celsius' | 'fahrenheit')
.API and Database Calls: You can turn user queries into direct API calls or even database requests. For example, “Who are my top customers this month?” could be translated into an API call like
get_customers_by_revenue(start_date: string, end_date: string, limit: int)
.Data Extraction: Extracting structured data from texts is also possible. For instance, you could define a function named
extract_people_data(people: [{name: string, birthday: string, location: string}])
to fetch details of all individuals mentioned in a Wikipedia article.
Practical Example: Weather Information
Step 1: User’s Initial Request to OpenAI
Using Python’s requests
:
import requests |
OpenAI’s response:
{ |
Observe that in the provided response, the “content” key returns a null value. In contrast, the “function_call” key offers structured JSON results. Users can utilize this structured format to make external API calls based on their requirements.
The subsequent steps are independent of OpenAI. We simply harness the structured format provided by OpenAI to proceed further.
Step 2: Use OpenAI’s Response for Your External API Call
An example using the fictional weather API:
weather_response = requests.get("https://weatherapi.com/YOUR_ENDPOINT", params={"location": "Boston, MA"}) |
Resulting in:
{ "temperature": 22, "unit": "celsius", "description": "Sunny" } |
You can stop here, or use OpenAI again to convert this structured data into natural language.
Following Step 2, we could potentially conclude our workflow and present the response to the users.
Yet, there’s an option to progress further by sending the API response from Step 2 back to OpenAI. This allows OpenAI to transform the structured data into more natural language. However, if our primary intention is merely to have OpenAI shape the initial output into a JSON format for our workflow, there’s no need to advance to Step 3.
Step 3: Convert Structured Data into Natural Language with OpenAI
Making another request to OpenAI:
data["messages"].extend([ |
OpenAI’s final response:
{ |
This example demonstrates the flexibility and power of OpenAI’s function calling feature, allowing for seamless integrations with external tools and services.
Remember to replace placeholders such as YOUR_OPENAI_API_KEY with your actual credentials.