api.models

turn · 工具

api.models.turn({ tools })

模型可以要求什么。你宣布它们,它决定,你执行——我们这边没有任何东西。

可选sent on every turntwo schema forms
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

声明是一个名称、描述和一个参数图案。 模型从不运行任何功能:它通过决定回答,运行它是你的——这也是它能够安全地揭露事物的原因,把它交给别人是危险的。

一项宣言

{
  name: "weather",
  description: "Current weather for a city.",
  parameters: {
    city: { type: "string", description: "City name", required: true },
    unit: { type: "string", enum: ["c", "f"] },
  },
}
{
    "name": "weather",
    "description": "Current weather for a city.",
    "parameters": {
        "city": {"type": "string", "description": "City name", "required": True},
        "unit": {"type": "string", "enum": ["c", "f"]},
    },
}

写参数的两个方法

简短的表格是一张字段图,每个字段都有 required 完整表格是标的物 JSON 两者都得到接受;短一个是因为三个参数的手工写成一个图案不成比例。

// Short
parameters: {
  city: { type: "string", required: true },
  unit: { type: "string", enum: ["c", "f"] },
}

// Full — reach for this when it gets complicated
parameters: {
  type: "object",
  properties: {
    city: { type: "string" },
    unit: { type: "string", enum: ["c", "f"] },
  },
  required: ["city"],
}
# Short
parameters = {
    "city": {"type": "string", "required": True},
    "unit": {"type": "string", "enum": ["c", "f"]},
}

# Full — reach for this when it gets complicated
parameters = {
    "type": "object",
    "properties": {
        "city": {"type": "string"},
        "unit": {"type": "string", "enum": ["c", "f"]},
    },
    "required": ["city"],
}
接受类型
□ 数 □ 整数 □飞毛腿
数项 · 对象随心所欲,随心所欲。

每份申报单一转一付

工具随每次呼叫发送, 所以它们都是您所收帐的输入的一部分。 模型在每次提供时, 从未选择过仍然成本的标记。 在一个环形的二十个转弯中, 一个工具可以支付二十次 。

发送此转弯可能叫什么, 而不是你所支持的一切

缩小列表的 实际对话是 最便宜的优化 在工具呼叫循环, 并且它也使模型选择更好。

const ALL = { weather: weatherTool, invoices: invoiceTool, calendar: calendarTool };

// Pick per turn rather than declaring the catalogue
const tools = Object.entries(ALL)
  .filter(([name]) => relevant(name, conversation))
  .map(([, tool]) => tool);
ALL = {"weather": weather_tool, "invoices": invoice_tool, "calendar": calendar_tool}

# Pick per turn rather than declaring the catalogue
tools = [tool for name, tool in ALL.items() if relevant(name, conversation)]

说明是指示

说它做什么,需要什么,什么时候 否 使用它。

弱更好
获取数据城市目前的天气,今天只使用——没有预报。
搜索“按姓名搜索我们的产品目录,而不是查阅客户记录。”
发送电子邮件“发电子邮件,仅在用户确认收件人和文本后。”

将签名背回

工具电话可以回来时携带 signature。在下一转弯上将其原样地发回,否则转弯被拒绝。保留整个助手信息,然后按原样将其推回,不考虑它,就可以处理。

// Right: the message goes back whole
messages.push(turn.message);

// Wrong: rebuilt, and the signature is gone
messages.push({ role: "assistant", content: turn.message.content,
                toolCalls: turn.message.toolCalls.map(({ id, name, args }) => ({ id, name, args })) });
# Right: the message goes back whole
messages.append(turn["message"])

# Wrong: rebuilt, and the signature is gone
messages.append({"role": "assistant", "content": turn["message"]["content"],
                 "toolCalls": [{"id": c["id"], "name": c["name"], "args": c["args"]}
                               for c in turn["message"]["toolCalls"]]})

一次接一次几次

转弯可以同时要求一个以上工具。 任意运行它们 — — 当它们独立时同时运行 — — 并以自己的方式回答每个工具。 tool 信息。

const results = await Promise.all(
  turn.message.toolCalls.map((call) => run(call.name, call.args).catch((e) => `Error: ${e.message}`)),
);

turn.message.toolCalls.forEach((call, i) => {
  messages.push({ role: "tool", toolCallId: call.id, name: call.name, content: String(results[i]) });
});
from concurrent.futures import ThreadPoolExecutor

def safe_run(call):
    try:
        return run(call["name"], call["args"])
    except Exception as error:
        return f"Error: {error}"

calls = turn["message"]["toolCalls"]
with ThreadPoolExecutor() as pool:
    results = list(pool.map(safe_run, calls))

for call, result in zip(calls, results):
    messages.append({"role": "tool", "toolCallId": call["id"], "name": call["name"], "content": str(result)})
自己决定 哪些事情可能无人照顾

模型选择; 它不授权。 发送、 发布、 删除或花费的任何信息都应该在运行之前通过您自己的支票—— 引擎的许可类别是思考哪个方法的一种方式。

错误

代码状态触发条件
invalid_tools400工具存在, 但不是矩阵 。
turn_failed502提供商拒绝声明——通常是它不能接受的一种计划。

将参数图状保持平原。 含有引用或构成的系统图案, 且提供商不接受其设置失败 整个 转弯,不只是那一种工具