# turn · 消息

Canonical URL: https://developers.iagenify.com/zh/sdk/ref/models/turn/messages/
Language: zh

`api.models.turn({ messages })`

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

Status: 必填 · never empty · you own the history

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

## 三个作用 {#roles}

| role | 携带内容 | 撰 写 人 |
| --- | --- | --- |
| 用户用户 | 内容和可选图像 | 你,你,你 |
| 助理助理助理助理助理 | 其决定采取行动时的 内容和工具卡 | 从上一个转弯处找回 |
| 工具工具工具 | 工具 CallId、 名称、 内容 - 您生成的结果 | 你,在操作工具后 |

```javascript
[
  { 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" },
]
```

## 循环, 完整 {#loop}

```javascript
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");
```

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

## 每一次的呼唤,甚至失败都回答 {#answer-all}

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

```javascript
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 });
}
```

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

## 图像和附件 {#images}

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

```javascript
{ 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 }] }
```

| 接受接受 | 类型类型类型 |
| --- | --- |
| 图像图像 | 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. |
| Documents | pdf pdf 开花 |
| 音音频 | wav · mp3 · mp4 · aac · ogg · flac · opus |
| 视频视频视频视频 | mp4 / mpeg / mov / avi / webm / flv 3gpp |

> **转一转最多可携带18兆吨附加物**
>
> 过去, 任何帐单都未开单之前, 转折被拒绝。 拒绝的是一个未知的类型, 信件中注明了这个类型, 转换它, 或者用文字描述它 。

## 历史就是你付出的代价 {#growth}

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

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

## 错误 {#errors}

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

- [turn · 工具](/zh/sdk/ref/models/turn/tools): 宣布它可能叫什么, 和代价。
- [turn · 停止原因](/zh/sdk/ref/models/turn/stop-reasons): 知环何时结束
- [智能体引擎](/zh/sdk/agent): 此循环, 已经写入, 并有权限和预算 。

```python
[
    {"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"},
]
```

```python
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")
```

```python
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})
```

```python
{"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}]}
```
