工具调用

最后更新

三种协议的工具调用都按各自官方格式书写:OpenAI 的 tools 与 tool_calls、Anthropic 的 tools 与 tool_use / tool_result、Gemini 的 functionDeclarations 与 functionCall / functionResponse。

curl https://modelportal.ai/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "MODEL_ID",
    "messages": [{"role": "user", "content": "上海今天天气怎么样?"}],
    "tools": [{
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "查询城市天气",
        "parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}
      }
    }]
  }'

模型决定调用工具时,响应里的 choices[0].message.tool_calls 给出函数名和参数。你执行函数后,把结果作为 role: "tool" 的消息(带上 tool_call_id)追加到 messages 再请求一次。

平台怎么处理

  • 请求带了工具定义时,只路由到声明支持工具调用的报价;一个都没有时返回 400 unsupported_parameter。模型详情页的能力标签和报价列表里能看到谁支持。
  • 承接的报价用另一种协议时,平台做协议转换,你收到的仍是你所用协议格式的工具调用。
  • 用 Anthropic 协议做多轮工具调用、并回传带签名的 thinking 块时,后续请求只会路由到产生签名的那个报价;它不可用时返回 422,原因代码 session,提示你开启新会话。