api.models

turn · 消息

api.models.turn({ messages })

整个对话,每次。三个角色, 以及决定工具呼叫环是否有效的一条规则。

必填never emptyyou own the history
Using Python?

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

转弯一无所获。模型知道的正是这个阵列中的内容,所以阵列就是对话,而增加的正确就是循环。

三个作用

role携带内容撰 写 人
用户用户内容和可选图像你,你,你
助理助理助理助理助理其决定采取行动时的 内容和工具卡从上一个转弯处找回
工具工具工具工具 CallId、 名称、 内容 - 您生成的结果你,在操作工具后
[
  { role: "user", content: "What is the weather in Tunis?" },

  { role: "assistant", content: "",
    toolCalls: [{ id: "call_0", name: "weather",
                  args: { city: "Tunis" }, signature: "…" }] },

  { role: "tool", toolCallId: "call_0", name: "weather",
    content: "22°C, clear" },
]
[
    {"role": "user", "content": "What is the weather in Tunis?"},

    {"role": "assistant", "content": "",
     "toolCalls": [{"id": "call_0", "name": "weather",
                    "args": {"city": "Tunis"}, "signature": "…"}]},

    {"role": "tool", "toolCallId": "call_0", "name": "weather",
     "content": "22°C, clear"},
]

循环, 完整

const messages = [{ role: "user", content: question }];

for (let step = 0; step < 10; step += 1) {
  const turn = await api.models.turn({ model, messages, tools });

  // 1. The assistant's move goes back in, UNCHANGED.
  messages.push(turn.message);

  if (turn.stopReason !== "tool_use") return turn.message.content;

  // 2. Run what it asked for, and answer every call it made.
  for (const call of turn.message.toolCalls) {
    const result = await run(call.name, call.args);
    messages.push({ role: "tool", toolCallId: call.id, name: call.name,
                    content: String(result) });
  }
}
throw new Error("too many steps");
def answer(question):
    messages = [{"role": "user", "content": question}]

    for _ in range(10):
        turn = api.models.turn(model=model, messages=messages, tools=tools)

        # 1. The assistant's move goes back in, UNCHANGED.
        messages.append(turn["message"])

        if turn["stopReason"] != "tool_use":
            return turn["message"]["content"]

        # 2. Run what it asked for, and answer every call it made.
        for call in turn["message"]["toolCalls"]:
            result = run(call["name"], call["args"])
            messages.append({"role": "tool", "toolCallId": call["id"], "name": call["name"],
                             "content": str(result)})
    raise RuntimeError("too many steps")
将助手信息往回按原样

重建它, 丢弃一个您不认识的字段, 或者只保留文本会打破下一转。 有些模型在工具电话上挂上签名, 工具电话必须一转而回, 并且必须精确返回 。

每一次的呼唤,甚至失败都回答

转转可以同时要求若干工具。 tool 信息, 匹配 toolCallId - 留下一个没有答案的,下一转弯前后不一致。

for (const call of turn.message.toolCalls) {
  let content;
  try {
    content = String(await run(call.name, call.args));
  } catch (err) {
    // A failure is a result. Say what went wrong — it is what lets the model recover.
    content = `Error: ${err.message}`;
  }
  messages.push({ role: "tool", toolCallId: call.id, name: call.name, content });
}
for call in turn["message"]["toolCalls"]:
    try:
        content = str(run(call["name"], call["args"]))
    except Exception as error:
        # A failure is a result. Say what went wrong — it is what lets the model recover.
        content = f"Error: {error}"
    messages.append({"role": "tool", "toolCallId": call["id"], "name": call["name"], "content": content})

告诉模型工具失败和为什么通常比隐藏它更值得:它可以尝试另一种方式。沉默只是让它重复同样的号召。

图像和附件

用户信息可以携带图像,工具结果可以携带附件——一页、一份文件、记录——以便模型看到工具制作的是什么而不是描述。

{ role: "user", content: "What is wrong with this screenshot?",
  images: [{ mime: "image/png", data: base64 }] }

{ role: "tool", toolCallId: "call_0", name: "read", content: "report.pdf, 12 pages",
  attachments: [{ mime: "application/pdf", data: base64 }] }
{"role": "user", "content": "What is wrong with this screenshot?",
 "images": [{"mime": "image/png", "data": base64_png}]}

{"role": "tool", "toolCallId": "call_0", "name": "read", "content": "report.pdf, 12 pages",
 "attachments": [{"mime": "application/pdf", "data": base64_pdf}]}
接受接受类型类型类型
图像图像ng / jpeg / wwwp / gif / hic / heif / hif / hic / hic / hif / hic / hic / hig / ig / ig / ig / gif / gif / gif hic / heif / hic / heif yg / hic / heif / heif / heif / shef / sheg / shef / shef / shc / shic / hic /heif /hec /hec shef / shef sh.
Documentspdf pdf 开花
音音频wav · mp3 · mp4 · aac · ogg · flac · opus
视频视频视频视频mp4 / mpeg / mov / avi / webm / flv 3gpp
转一转最多可携带18兆吨附加物

过去, 任何帐单都未开单之前, 转折被拒绝。 拒绝的是一个未知的类型, 信件中注明了这个类型, 转换它, 或者用文字描述它 。

历史就是你付出的代价

数组中的所有东西都被发送到每个转弯, 因此一个运行二十步的循环 发出第一个信息二十次。 两件事将它保存在手头。

  • 将消耗的都放下 已经说明理由的大型工具成果可以用其发生的简短说明来取代。
  • 总结一下旧的半半 并从摘要中开始矩阵,这就是代理引擎为您做的。

错误

代码状态触发条件
messages_required400数组丢失或空 。
context_too_large400比模型的窗口还多 任何借方之前都拒绝
turn_failed502供应商拒绝 信息是供应商的 被编辑