跳转到主要内容

Function Calling

POST /v1/chat/completions
截至 2026 年 3 月 23 日,Crazyrouter 生产环境已验证 gpt-5.4 在 Chat Completions 路径下会返回 tool_calls

第一步:定义工具

curl https://crazyrouter.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gpt-5.4",
    "messages": [
      {"role": "user", "content": "What is the weather in Beijing? Use the tool."}
    ],
    "tools": [
      {
        "type": "function",
        "function": {
          "name": "get_weather",
          "description": "Get weather info for a city",
          "parameters": {
            "type": "object",
            "properties": {
              "city": {"type": "string"}
            },
            "required": ["city"],
            "additionalProperties": false
          }
        }
      }
    ]
  }'
生产环境实际返回的关键形态:
{
  "object": "chat.completion",
  "model": "gpt-5.4",
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": null,
        "tool_calls": [
          {
            "id": "call_...",
            "type": "function",
            "function": {
              "name": "get_weather",
              "arguments": "{\"city\":\"Beijing\"}"
            }
          }
        ]
      }
    }
  ]
}

第二步:回传工具结果

拿到 tool_calls 之后,把你的工具执行结果作为 role: "tool" 消息发回:
{
  "model": "gpt-5.4",
  "messages": [
    {"role": "user", "content": "What is the weather in Beijing? Use the tool."},
    {
      "role": "assistant",
      "content": null,
      "tool_calls": [
        {
          "id": "call_123",
          "type": "function",
          "function": {
            "name": "get_weather",
            "arguments": "{\"city\":\"Beijing\"}"
          }
        }
      ]
    },
    {
      "role": "tool",
      "tool_call_id": "call_123",
      "content": "{\"temperature\":22,\"condition\":\"sunny\"}"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "Get weather info for a city",
        "parameters": {
          "type": "object",
          "properties": {
            "city": {"type": "string"}
          },
          "required": ["city"],
          "additionalProperties": false
        }
      }
    }
  ]
}
本轮生产验证确认了 tool_calls 这条主链路,同时也观察到 message.reasoning_content 这个键可能出现,但它不应作为当前工具调用流程的稳定依赖字段。