Function Calling

最后更新:2026-04-24 · 预计阅读 9 分钟

TTToken 原生透传 OpenAI / Claude / Gemini 三家的工具调用协议。大部分情况下你写一份 tools 定义就能跨模型运行。

tools 参数(OpenAI 协议)

{
  "model": "gpt-4o",
  "messages": [{"role":"user","content":"北京今天穿什么?"}],
  "tools": [{
    "type": "function",
    "function": {
      "name": "get_weather",
      "description": "查询指定城市的天气",
      "parameters": {
        "type": "object",
        "properties": {
          "city": {"type": "string", "description": "城市名"},
          "unit": {"type": "string", "enum": ["c","f"]}
        },
        "required": ["city"]
      }
    }
  }],
  "tool_choice": "auto"
}

模型返回工具调用

{
  "choices": [{
    "message": {
      "role": "assistant",
      "content": null,
      "tool_calls": [{
        "id": "call_abc",
        "type": "function",
        "function": {
          "name": "get_weather",
          "arguments": "{\"city\":\"北京\",\"unit\":\"c\"}"
        }
      }]
    },
    "finish_reason": "tool_calls"
  }]
}

把工具结果回传

"messages": [
  {"role":"user","content":"北京今天穿什么?"},
  {"role":"assistant","tool_calls":[{"id":"call_abc","type":"function","function":{"name":"get_weather","arguments":"{...}"}}]},
  {"role":"tool","tool_call_id":"call_abc","content":"{\"temp\":8,\"desc\":\"多云\"}"}
]

JSON 输出 / Structured Output

方式 1:JSON 对象

只要求返回合法 JSON:

{
  "model": "gpt-4o-mini",
  "response_format": {"type": "json_object"},
  "messages": [
    {"role":"system","content":"回复严格的 JSON。"},
    {"role":"user","content":"给我三种颜色的 rgb"}
  ]
}

方式 2:JSON Schema(强约束)

{
  "model": "gpt-4o",
  "response_format": {
    "type": "json_schema",
    "json_schema": {
      "name": "ColorList",
      "strict": true,
      "schema": {
        "type": "object",
        "properties": {
          "colors": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "name": {"type":"string"},
                "rgb":  {"type":"array","items":{"type":"integer"}}
              },
              "required": ["name","rgb"]
            }
          }
        },
        "required": ["colors"]
      }
    }
  },
  "messages": [{"role":"user","content":"给我三种颜色的 rgb"}]
}

Claude 工具调用

Claude 使用 tools + input_schema,响应里出现 tool_use 块,用 tool_result 回传:

"messages": [
  {"role":"user","content":"北京天气"},
  {"role":"assistant","content":[
    {"type":"tool_use","id":"toolu_1","name":"get_weather","input":{"city":"北京"}}
  ]},
  {"role":"user","content":[
    {"type":"tool_result","tool_use_id":"toolu_1","content":"{\"temp\":8}"}
  ]}
]

Gemini 工具调用

{
  "contents": [{"role":"user","parts":[{"text":"北京天气"}]}],
  "tools": [{
    "functionDeclarations": [{
      "name": "get_weather",
      "description": "...",
      "parameters": {"type":"OBJECT","properties":{"city":{"type":"STRING"}},"required":["city"]}
    }]
  }]
}

模型返回 functionCall,客户端执行后以 functionResponse 附加到 contents 再次请求。

渠道兼容性

特性OpenAIClaudeGeminiDeepSeek / Qwen
tools
parallel tool calls部分部分
JSON Mode🟡*
JSON Schema strict🟡*🟡
tool_choice=required✅ (any)✅ (ANY)部分

* Claude 需要配合 system prompt 或单工具强制触发来得到强 JSON。

注意

当把 OpenAI 协议请求路由到 Claude / Gemini 模型时,TTToken 会自动做参数映射(toolstool_useresponse_format ↔ system prompt 等)。个别极端嵌套场景可能需要微调。