Function Calling for AI Agents
Introduction
Have you ever wondered how ChatGPT plugins work behind the scenes? Or how AI assistants like Claude can interact with external tools and data sources? The secret sauce is a powerful capability called “function calling”.
I’ll walk you through how I built a practical weather assistant using OpenAI’s implementation of this technology.
What is Function Calling?
Function calling allows AI models to recognize when they need external data or tools to answer a question. Instead of hallucinating information it doesn’t know (like today’s weather), the AI can:
- Identify the need for external information.
- Call specific functions with the right parameters
- Use the returned data to provide accurate responses
This creates a seamless bridge between AI language capabilities and real-world data or services.
Building a Weather Assistant: A Step-by-Step Guide
Let see how we can built a simple but powerful weather assistant using Python and OpenAI’s function calling feature.
Step 1: Define Your Tool
First, we need to tell the AI model what external functions it can use. Here, we’re creating a weather lookup tool:
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,
},
}
]
This JSON above will tell the model what our function does and what parameters it needs. The beauty here is that the model will automatically determine when to use this function based on our prompt.
Step 2: Let the Model Decide When to Call the Function
When a user asks “What’s the weather like in Kerala today?”, we pass this to the model along with our tool definitions:
system_prompt = "You are a helpful weather assistant."
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": "What's the weather like in Kerala today?"},
]completion = client.chat.completions.create(
model="gpt-4o",
messages=messages,
tools=tools,
)
Step 3: Execute the Function with the Model’s Parameters
The model doesn’t just decide to call our function — it also figures out that “Kerala” needs to be converted to latitude and longitude coordinates:
def call_function(name, args):
if name == "get_weather":
return get_weather(**args)
for tool_call in completion.choices[0].message.tool_calls:
name = tool_call.function.name
args = json.loads(tool_call.function.arguments)
messages.append(completion.choices[0].message) result = call_function(name, args)
messages.append(
{"role": "tool", "tool_call_id": tool_call.id, "content": json.dumps(result)}
)
Our actual weather function makes an API call to fetch real-time data:
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"]
Step 4: Feed the Results Back to the Model
Now we have real weather data, but we want a natural language response. We pass the function results back to the model:
# Previous messages plus the function result
completion_2 = client.beta.chat.completions.parse(
model="gpt-4o",
messages=messages,
tools=tools,
response_format=WeatherResponse,
)
Step 5: Structure the Response with Pydantic
A nice touch is using Pydantic to structure the model’s response:
class WeatherResponse(BaseModel):
temperature: float = Field(
description="The current temperature in celsius for the given location."
)
response: str = Field(
description="A natural language response to the user's question."
)
This gives us both the raw temperature data and a natural language response in a clean, structured format.
Beyond Weather: What Can You Build?
The applications for function calling extend far beyond weather:
- Customer service bots that can look up order information
- Research assistant that query specialized databases
- Personal finance apps that check account balances or stock prices
- Home automation systems that can control smart devices
- Healthcare assistants that access patient records or medical knowledge bases
Getting Started:
Want to try this yourself? You’ll need:
1. An OpenAI API key
2. Basic Python knowledge
3. An idea for what external data or services you want to connect.