Logo
热心市民王先生

技术原理核心

AI研究 Tool Calling 技术原理

深入分析各主流AI模型的Tool Calling实现原理、JSON Schema格式差异和调用流程

什么是 Tool Calling

Tool Calling(工具调用)是大型语言模型与外部系统交互的核心机制。它允许模型:

  • 识别需要调用外部工具的场景
  • 生成符合预期格式的工具调用请求
  • 将工具执行结果整合到后续推理中

核心概念对比

概念OpenAIAnthropicGoogle GeminiMCP
工具定义字段toolstoolsfunction_declarationstools/list
参数 SchemaJSON SchemaJSON SchemaJSON SchemaJSON Schema
调用触发tool_callstool_usefunctionCalltools/call
结果返回tool_call_idtool_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\"}"
      }
    }
  ]
}

关键特征

  1. arguments 是字符串 —— 注意这是一个 JSON 字符串,不是对象
  2. tool_calls 数组 —— 支持并行调用多个工具
  3. 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"
}

关键差异

  1. input 是对象 —— 直接是 JSON 对象,无需解析字符串
  2. 混合内容类型 —— content 数组可同时包含 texttool_use
  3. input_schema vs parameters —— 字段命名不同

结果返回格式

{
  "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"
              }
            }
          }
        ]
      }
    }
  ]
}

关键特征

  1. args 是对象 —— 类似 Anthropic,直接是 JSON 对象
  2. function_declarations —— 不同的工具列表字段名
  3. 函数调用模式 —— 支持 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"}
          }
        }
      }
    }
  ]
}

关键特征

  1. 完全兼容 OpenAI 格式 —— 降低了迁移成本
  2. 支持 strict 模式 —— 启用严格 Schema 验证
  3. tool_choice 支持 —— noneautorequired

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 的核心优势

  1. 协议级别标准化 —— 不依赖特定模型 API
  2. 服务端工具支持 —— 工具可以独立部署
  3. JSON-RPC 2.0 基础 —— 成熟的通信协议
  4. 结构化输出 —— 支持 outputSchema 定义返回格式

核心差异总结表

维度OpenAIAnthropicGeminiDeepSeekMCP
工具定义字段tools[].functiontools[]function_declarations[]tools[].functiontools/list
参数 Schema 字段parametersinput_schemaparametersparametersinputSchema
调用类型字段tool_callstool_usefunctionCalltool_callstools/call
参数格式字符串对象对象字符串对象
ID 字段tool_call_idtool_use_idtool_call_id自动处理
结果角色role: "tool"role: "user"嵌入对话role: "tool"JSON-RPC

参考资料