大模型函数调用(Function Calling)完全指南
函数调用(Function Calling)是大模型从”聊天机器人”进化为”AI Agent”的关键能力。它让模型能够调用你预定义的工具,实现查数据库、调 API、操作文件等实际任务。
工作原理
函数调用的完整流程分五步:
- 在请求中定义可用的工具函数(名称、描述、参数 schema)
- 模型分析用户意图,决定是否需要调用工具
- 模型返回函数名和参数(JSON 格式)
- 你的代码执行函数,拿到结果
- 把结果发回模型,生成最终回复
OpenAI 协议实现
Python
from openai import OpenAI
import json
client = OpenAI(
base_url="https://api.ofox.ai/v1",
api_key="your-api-key"
)
# 定义工具
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的实时天气信息",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称,如:北京、上海"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "温度单位"
}
},
"required": ["city"]
}
}
}]
messages = [{"role": "user", "content": "北京今天天气怎么样?"}]
# 第一轮:模型决定调用工具
response = client.chat.completions.create(
model="openai/gpt-4o",
messages=messages,
tools=tools,
tool_choice="auto"
)
message = response.choices[0].message
# 执行工具调用
if message.tool_calls:
for tool_call in message.tool_calls:
args = json.loads(tool_call.function.arguments)
result = get_weather(args["city"]) # 你的函数实现
messages.append(message)
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result)
})
# 第二轮:模型根据工具结果生成回复
final = client.chat.completions.create(
model="openai/gpt-4o",
messages=messages,
tools=tools
)
print(final.choices[0].message.content)
TypeScript
import OpenAI from 'openai'
const client = new OpenAI({
baseURL: 'https://api.ofox.ai/v1',
apiKey: 'your-api-key'
})
const tools: OpenAI.ChatCompletionTool[] = [{
type: 'function',
function: {
name: 'get_weather',
description: '获取指定城市的实时天气信息',
parameters: {
type: 'object',
properties: {
city: { type: 'string', description: '城市名称' },
unit: { type: 'string', enum: ['celsius', 'fahrenheit'] }
},
required: ['city']
}
}
}]
const response = await client.chat.completions.create({
model: 'openai/gpt-4o',
messages: [{ role: 'user', content: '北京今天天气怎么样?' }],
tools,
tool_choice: 'auto'
})
Anthropic 协议差异
Claude 模型的工具定义格式略有不同,用 input_schema 代替 parameters:
import anthropic
client = anthropic.Anthropic(
base_url="https://api.ofox.ai/anthropic",
api_key="your-api-key"
)
response = client.messages.create(
model="anthropic/claude-sonnet-4.5",
max_tokens=1024,
tools=[{
"name": "get_weather",
"description": "获取指定城市的实时天气信息",
"input_schema": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名称"}
},
"required": ["city"]
}
}],
messages=[{"role": "user", "content": "北京今天天气怎么样?"}]
)
通过 ofox.ai 网关,你可以用同一个 API Key 同时调用两种协议的模型,无需分别管理。
tool_choice 参数
| 值 | 说明 |
|---|---|
"auto" | 模型自动决定是否调用(默认) |
"none" | 禁止调用工具 |
"required" | 强制调用工具 |
{"type": "function", "function": {"name": "xxx"}} | 强制调用指定工具 |
支持函数调用的模型
| 厂商 | 模型 |
|---|---|
| OpenAI | openai/gpt-4o, openai/gpt-4o-mini, openai/gpt-5 |
| Anthropic | anthropic/claude-opus-4.6, anthropic/claude-sonnet-4.5, anthropic/claude-haiku-4.5 |
| google/gemini-2.5-pro, google/gemini-2.5-flash | |
| 国产 | deepseek/deepseek-v3.2, bailian/qwen3-max, z-ai/glm-4.7 |
函数调用是构建 AI Agent 的基石。掌握了这个能力,你的 AI 应用就能从”只会聊天”进化为”能干活”。