跳转到正文

工具调用

RightNow AIOpen models工具调用 更新于

本页内容

当模型需要对外部世界采取行动时,就使用工具调用:调用 API、查询数据库、执行计算、获取文档。

工具调用是按模型提供的能力。发送 tools 之前,请先查看模型页面上的能力列表。未列出工具调用的模型会以 400 hosted_capability_not_supported 拒绝请求。

一轮的实际样子

一轮工具调用
Text
一轮工具调用的示意图。

你发送消息和工具。模型返回一条带有 tool_calls 的助手消息。每个工具由你自己运行。你把那条完整的助手消息添加进去,再为每次调用添加一条工具消息,然后再次发送变长后的数组。没有 tool_calls 的那一轮就是最终答案。第 1 步发送 user 消息。第 4 步发送 user、assistant 和 tool 消息。

调用一次 get_weather 之后,第 4 步会发送下面这三条消息。模型的 arguments 以 JSON 字符串的形式到达,工具消息通过 tool_call_id 回应对应的调用。

messages
JSON
[
  { "role": "user", "content": "What's the weather in Paris?" },
  {
    "role": "assistant",
    "tool_calls": [{
      "id": "call_example",
      "type": "function",
      "function": { "name": "get_weather", "arguments": "{\"city\":\"Paris\"}" }
    }]
  },
  {
    "role": "tool",
    "tool_call_id": "call_example",
    "content": "{\"city\":\"Paris\",\"temp_c\":21,\"conditions\":\"partly cloudy\"}"
  }
]

最少的代码

下面的循环会重复这一轮,直到模型给出答案。MODEL_ID 代表 Open models 中一个可用的 ID。

TypeScript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.rightnowai.co/v1",
  apiKey: process.env.RIGHTNOW_API_KEY,
});

const tools = [{
  type: "function" as const,
  function: {
    name: "get_weather",
    description: "Get the current weather for a city",
    parameters: {
      type: "object",
      properties: { city: { type: "string" } },
      required: ["city"],
    },
  },
}];

function getWeather(city: string) {
  return { city, temp_c: 21, conditions: "partly cloudy" };
}

const messages: any[] = [
  { role: "user", content: "What's the weather in Paris?" },
];

for (let turnsLeft = 10; turnsLeft > 0; turnsLeft -= 1) {
  const response = await client.chat.completions.create({
    model: "MODEL_ID",
    messages,
    tools,
    max_tokens: 16384,
  });
  const msg = response.choices[0].message;
  messages.push(msg);

  if (!msg.tool_calls?.length) { console.log(msg.content); break; }

  for (const call of msg.tool_calls) {
    if (call.type !== "function") continue;
    const args = JSON.parse(call.function.arguments);
    const result = getWeather(args.city);
    messages.push({
      role: "tool",
      tool_call_id: call.id,
      content: JSON.stringify(result),
    });
  }
}

可以调整的内容

参数作用
tool_choice: "auto"由模型决定何时调用工具。这是默认值。
tool_choice: "required"在支持的模型上,强制每一轮都调用工具,不允许自由回答。部分模型会以 400 拒绝 tool_choice: "required",错误码为 hosted_parameter_not_supported,并带有 param: "tool_choice"。消息会说明应改为发送什么。在这些模型上,请强制指定一个具名工具,或发送 auto。
tool_choice: {type:"function", function:{name:"..."}}强制使用某一个特定工具。
parallel_tool_calls: false要求每次只进行一次调用。是否遵守取决于模型。

在部分模型上,强制或指定某个工具时,还需要在每个工具上设置 function.strict: true。没有它,即使你要求调用,模型也可能不调用就直接回答。更换所选模型之前,请查看完整的工具规则。

常见错误

  • 丢掉助手这一轮。在添加任何工具结果之前,先把完整的助手消息连同 tool_calls 一起添加进去。
  • 返回非字符串的工具结果。tool 角色消息中的 content 必须是字符串。请始终进行序列化。
  • 含糊的 description。写的时候要假设模型从未见过你的 API:工具做什么,每个参数是什么意思,好的输入是什么样子。这是决定工具会不会被调用的最大因素。
  • 没有轮数上限。给循环设定上限。十轮几乎能覆盖所有模式,而没有上限的循环可能花掉真金白银。
  • 轻信参数。模型可能编造字段,或漏掉必填字段。执行之前,请用 Pydantic 或 Zod 进行校验。

相关内容