Overview of Function Calling
Function Calling is a technology that enables large language models to recognize user intent and call predefined functions. The model itself does not execute functions; instead, it generates structured function call requests, which are actually executed by developer code, and the results are returned to the model.
Basic Principles
The workflow of Function Calling:
- Define available functions and their parameter schemas
- Send the function definitions and user message to the model
- The model determines whether to call a function and generates the call parameters
- The developer executes the function and obtains the result
- Return the function result to the model, and the model generates the final reply
Practical: Weather Query Agent
import json
import requests
from openai import OpenAI
client = OpenAI()
# Define function
def get_weather(city: str, date: str = "today"):
"""Query weather"""
api_key = "your-api-key"
url = f"https://api.weather.com/v1/forecast"
response = requests.get(url, params={
"city": city, "date": date, "key": api_key
})
return response.json()
# Define function schema
functions = [{
"name": "get_weather",
"description": "Query weather information for a specified city and date",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "City name, e.g., 'Beijing', 'Shanghai'"
},
"date": {
"type": "string",
"description": "Date, format YYYY-MM-DD, default today"
}
},
"required": ["city"]
}
}]
# Conversation flow
messages = [{"role": "user", "content": "What's the weather in Beijing tomorrow?"}]
response = client.chat.completions.create(
model="deepseek-chat",
messages=messages,
functions=functions,
function_call="auto"
)
msg = response.choices[0].message
if msg.function_call:
# Model decides to call function
func_name = msg.function_call.name
func_args = json.loads(msg.function_call.arguments)
# Execute actual function
available_functions = {"get_weather": get_weather}
func_result = available_functions[func_name](**func_args)
# Return result to model
messages.append(msg)
messages.append({
"role": "function",
"name": func_name,
"content": json.dumps(func_result)
})
final_response = client.chat.completions.create(
model="deepseek-chat",
messages=messages
)
print(final_response.choices[0].message.content)Multi-Function Coordinated Calling
In practical applications, Agents often need to call multiple functions. You can define multiple function schemas and let the model autonomously choose which function(s) to call. For example, a customer service Agent might need to call functions such as "Query Order", "Query Logistics", and "Query Refund Policy" simultaneously.
Best Practices
- Detailed function descriptions: Clear descriptions help the model accurately determine when to call
- Parameter validation: Validate parameters inside the function to prevent abnormal values from the model
- Error handling: Return clear error information when function execution fails
- Concise results: The results returned to the model should be concise, containing only necessary information
- Timeout control: Set timeout for function calls to prevent long blocking
Security Considerations
Function Calling gives AI the ability to execute code, so security must be considered: do not directly expose sensitive operations (such as database DELETE, payment transfers) as functions; require user confirmation for dangerous operations; strictly validate function parameters; log all function call logs for auditing.