工具调用导读

先把工具调用循环理解清楚,再决定是否要升级成 Skills。

原始 tool calling 适合你还在控制客户端执行循环、只需要少量函数工具的时候。 TheRouter 的价值在于把不同 provider 的 tool calling 语义统一成 OpenAI-compatible 形态, 但实际的执行循环仍然在你的应用里。

工具定义

tool-call-request.json
{
  "model": "openai/gpt-4.1",
  "messages": [{ "role": "user", "content": "检查东京和首尔的天气。" }],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "按城市获取当前天气",
        "parameters": {
          "type": "object",
          "properties": { "city": { "type": "string" } },
          "required": ["city"]
        }
      }
    }
  ],
  "tool_choice": "auto",
  "parallel_tool_calls": true
}

执行循环

TypeScript
const first = await client.chat.completions.create(request);
const message = first.choices[0]?.message;

if (message?.tool_calls?.length) {
  const toolMessages = await Promise.all(
    message.tool_calls.map(async (call) => {
      const args = JSON.parse(call.function.arguments || "{}");
      const result = await runTool(call.function.name, args);
      return {
        role: "tool" as const,
        tool_call_id: call.id,
        content: JSON.stringify(result),
      };
    })
  );

  const second = await client.chat.completions.create({
    ...request,
    messages: [...request.messages, message, ...toolMessages],
  });

  console.log(second.choices[0]?.message?.content);
}
什么时候该升级成 Skills
如果你已经开始在多个项目里重复同一类工具定义、联网逻辑、响应修复和 provider 差异处理, 原始 tool calling 的维护成本会明显上升,这时可以认真评估是否改用 Skills。

下一步