Eine Wendung hält nichts. Was das Modell weiß, ist genau das, was in diesem Array ist – also ist das Array das Gespräch, und es richtig zu wachsen ist die Schleife.
Die drei Rollen
| Rolle | Inhalt | Geschrieben von |
|---|---|---|
| Benutzer | Inhalt und optionale Bilder | Sie |
| Assistent | Inhalt und ToolCalls, wenn es beschlossen hat, zu handeln | Von der vorherigen Kurve zurückkopiert |
| Werkzeug | toolCallId, Name, Inhalt — das Ergebnis, das Sie produziert haben | Du, nach dem Betrieb des Werkzeugs |
[
{ 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"},
]Die Schleife, in voller Höhe
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")Erneut bauen, ein Feld, das Sie nicht erkannt haben, oder nur den Text zu halten, wird den nächsten Turn brechen. Einige Modelle tragen eine Signatur auf einem Werkzeugaufruf, der genau so zurück kommen muss, wie er übrig ist.
Antworten Sie auf jeden Anruf, auch die Fehler
Eine Wendung kann mehrere Werkzeuge gleichzeitig verlangen. Jede braucht eine eigene tool Nachricht, die mit toolCallId übereinstimmt – lassen Sie eine unbeantwortete und die nächste Wendung ist inkonsistent.
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})Dem Modell zu sagen, dass ein Werkzeug fehlgeschlagen ist und warum, ist in der Regel mehr wert als es zu verstecken: es kann einen anderen Weg versuchen.
Bilder und Anhänge
Eine Benutzernachricht kann Bilder tragen, und ein Werkzeugergebnis kann Anhänge tragen — eine Seite, ein Dokument, eine Aufzeichnung — so sieht das Modell, was das Werkzeug produziert anstatt eine Beschreibung davon.
{ 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}]}| Angenommen | Typen |
|---|---|
| Bilder | png · jpeg · webp · gif · heic · heif |
| Dokumente | |
| Audio | mp3 · mp4 · aac · ogg · flac · opus |
| Video | mp4 · mpeg · mov · avi · webm · flv · 3gpp |
Vorher wird die Wendung verweigert, bevor etwas in Rechnung gestellt wird. Auch ein nicht erkannter Typ wird abgelehnt, in der Nachricht genannt – konvertiert oder im Text beschrieben.
Die Geschichte ist, wofür Sie zahlen
Alles im Array wird auf jeder Umdrehung gesendet, so dass eine Schleife, die zwanzig Schritte läuft, die erste Nachricht zwanzig Mal sendet. Zwei Dinge halten das in der Hand.
- Entfernen Sie, was verbraucht wurde. Ein großes Werkzeugergebnis, das bereits begründet wurde, kann durch eine kurze Notiz ersetzt werden, dass es passiert ist.
- Zusammenfassen Sie die alte Hälfte und starten Sie das Array aus der Zusammenfassung – das ist, was die Agent-Engine für Sie tut.
Fehler
| Code | Status | Wann |
|---|---|---|
| messages_required | 400 | Das Array fehlte oder war leer. |
| context_too_large | 400 | Mehr als das Fenster des Modells. Vor jeder Belastung zurückgenommen. |
| turn_failed | 502 | Die Nachricht ist die des Anbieters, reaktiviert. |