技术原理核心
AI研究 Tool Calling 技术原理
深入分析各主流AI模型的Tool Calling实现原理、JSON Schema格式差异和调用流程
什么是 Tool Calling
Tool Calling(工具调用)是大型语言模型与外部系统交互的核心机制。它允许模型:
- 识别需要调用外部工具的场景
- 生成符合预期格式的工具调用请求
- 将工具执行结果整合到后续推理中
核心概念对比
| 概念 | OpenAI | Anthropic | Google Gemini | MCP |
|---|---|---|---|---|
| 工具定义字段 | tools | tools | function_declarations | tools/list |
| 参数 Schema | JSON Schema | JSON Schema | JSON Schema | JSON Schema |
| 调用触发 | tool_calls | tool_use | functionCall | tools/call |
| 结果返回 | tool_call_id | tool_use_id | 不需要 ID | 返回 content |
OpenAI Function Calling 规范
工具定义格式
{
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
}
]
}
模型响应格式
{
"content": null,
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"location\": \"San Francisco, CA\", \"unit\": \"celsius\"}"
}
}
]
}
关键特征
arguments是字符串 —— 注意这是一个 JSON 字符串,不是对象tool_calls数组 —— 支持并行调用多个工具tool_call_id—— 用于匹配工具调用与结果
结果返回格式
{
"role": "tool",
"tool_call_id": "call_abc123",
"content": "15°C, mostly cloudy"
}
Anthropic Claude Tool Use 规范
工具定义格式
{
"tools": [
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
]
}
模型响应格式
{
"content": [
{
"type": "text",
"text": "I'll check the weather for you."
},
{
"type": "tool_use",
"id": "toolu_01A09q90qw90lq917835lq9",
"name": "get_weather",
"input": {
"location": "San Francisco, CA",
"unit": "celsius"
}
}
],
"stop_reason": "tool_use"
}
关键差异
input是对象 —— 直接是 JSON 对象,无需解析字符串- 混合内容类型 ——
content数组可同时包含text和tool_use input_schemavsparameters—— 字段命名不同
结果返回格式
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01A09q90qw90lq917835lq9",
"content": "15°C, mostly cloudy"
}
]
}
Google Gemini Function Calling 规范
工具定义格式
{
"function_declarations": [
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
}
},
"required": ["location"]
}
}
]
}
模型响应格式
{
"candidates": [
{
"content": {
"parts": [
{
"functionCall": {
"name": "get_weather",
"args": {
"location": "San Francisco, CA"
}
}
}
]
}
}
]
}
关键特征
args是对象 —— 类似 Anthropic,直接是 JSON 对象function_declarations—— 不同的工具列表字段名- 函数调用模式 —— 支持 AUTO、ANY、NONE、VALIDATED 模式
DeepSeek Function Calling 规范
DeepSeek 采用与 OpenAI 高度兼容的格式:
工具定义格式
{
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"}
}
}
}
}
]
}
关键特征
- 完全兼容 OpenAI 格式 —— 降低了迁移成本
- 支持
strict模式 —— 启用严格 Schema 验证 tool_choice支持 ——none、auto、required
MCP (Model Context Protocol) 规范
MCP 是 Anthropic 推动的开放协议,旨在标准化 AI 模型与外部工具的交互。
工具发现
// Request
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/list"
}
// Response
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"tools": [
{
"name": "get_weather",
"description": "Get current weather information for a location",
"inputSchema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name or zip code"
}
},
"required": ["location"]
}
}
]
}
}
工具调用
// Request
{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "get_weather",
"arguments": {
"location": "New York"
}
}
}
// Response
{
"jsonrpc": "2.0",
"id": 2,
"result": {
"content": [
{
"type": "text",
"text": "Current weather in New York: 72°F, Partly cloudy"
}
],
"isError": false
}
}
MCP 的核心优势
- 协议级别标准化 —— 不依赖特定模型 API
- 服务端工具支持 —— 工具可以独立部署
- JSON-RPC 2.0 基础 —— 成熟的通信协议
- 结构化输出 —— 支持
outputSchema定义返回格式
核心差异总结表
| 维度 | OpenAI | Anthropic | Gemini | DeepSeek | MCP |
|---|---|---|---|---|---|
| 工具定义字段 | tools[].function | tools[] | function_declarations[] | tools[].function | tools/list |
| 参数 Schema 字段 | parameters | input_schema | parameters | parameters | inputSchema |
| 调用类型字段 | tool_calls | tool_use | functionCall | tool_calls | tools/call |
| 参数格式 | 字符串 | 对象 | 对象 | 字符串 | 对象 |
| ID 字段 | tool_call_id | tool_use_id | 无 | tool_call_id | 自动处理 |
| 结果角色 | role: "tool" | role: "user" | 嵌入对话 | role: "tool" | JSON-RPC |