OpenAI has announced the release of its brand-new Function Calling Guide, designed to help developers extend the capabilities of OpenAI models by integrating custom tools and functions. Based on extensive user feedback, the guide has been revamped to be 50% shorter and clearer, featuring new best practices, in-doc function generation, and a fully functional example using a weather API. This update reflects OpenAI’s commitment to making AI tools more accessible and developer-friendly, empowering developers to leverage function calling more effectively in their applications.
Function calling allows OpenAI models to interact with developer-defined tools, enabling them to perform tasks beyond generating text or audio. Here’s a simplified breakdown of the process:
The image shows the process of how function calling works between a developer and an AI model. Here’s a step-by-step breakdown:
Also Read: Top 6 LLMs that Support Function Calling
Let’s walk through a real-world example using a get_weather function. This function retrieves the current temperature for a given set of coordinates.
import requests
def get_weather(latitude, longitude):
response = requests.get(f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}¤t=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m")
data = response.json()
return data['current']['temperature_2m']
from openai import OpenAI
import json
client = OpenAI(api_key="sk-api_key”)
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current temperature for provided coordinates in celsius.",
"parameters": {
"type": "object",
"properties": {
"latitude": {"type": "number"},
"longitude": {"type": "number"}
},
"required": ["latitude", "longitude"],
"additionalProperties": False
},
"strict": True
}
}]
messages = [{"role": "user", "content": "What's the weather like in Paris today?"}]
completion = client.chat.completions.create(
model="gpt-4o",
messages=messages,
tools=tools,
)
tool_call = completion.choices[0].message.tool_calls[0]
args = json.loads(tool_call.function.arguments)
result = get_weather(args["latitude"], args["longitude"])
# Append the model's tool call message
messages.append(completion.choices[0].message)
# Append the result message as a string
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps({"temperature": result}) # Convert the result to a JSON string
})
# Create the second chat completion
completion_2 = client.chat.completions.create(
model="gpt-4o",
messages=messages,
tools=tools,
)
print(completion_2.choices[0].message.content)
Output:
The current temperature in Paris is -2.8°C.
To help you get the most out of function calling, here are some pro tips:
OpenAI’s revamped Function Calling Guide empowers developers to integrate custom tools seamlessly, making AI more accessible and practical. By simplifying processes, offering clear examples, and prioritizing user feedback, OpenAI enables developers to innovate and build solutions that harness the full potential of AI, driving real-world impact and creativity.