Die Ottili AI API unterstützt Streaming-Antworten, sodass Tokens beim Erzeugen eintreffen. Streaming wird pro Anfrage mit stream: true aktiviert.
Anfrage
curl -X POST "https://api.ottilione.com/api/v1/ai/chat" \
-H "Authorization: Bearer ott_your_api_key_here" \
-H "X-Platform-Company: your-company-slug" \
-H "Content-Type: application/json" \
-d '{
"model": "auto",
"stream": true,
"messages": [{ "role": "user", "content": "Entwirf eine Folge-E-Mail." }]
}'Antwortformat
Die Antwort verwendet text/event-stream. Jedes Event ist eine data:-Zeile mit einem JSON-Delta, abgeschlossen durch data: [DONE]:
data: {"delta": {"content": "Hallo"}, "finish_reason": null}
data: {"delta": {"content": " zusammen"}, "finish_reason": null}
data: {"usage": {"prompt_tokens": 12, "completion_tokens": 8, "total_tokens": 20, "credits": 0.01}, "finish_reason": "stop"}
data: [DONE]Konsumieren im Code
const res = await fetch("https://api.ottilione.com/api/v1/ai/chat", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.OTTILI_API_KEY}`,
"X-Platform-Company": "your-company-slug",
"Content-Type": "application/json",
},
body: JSON.stringify({ model: "auto", stream: true, messages }),
});
const reader = res.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { value, done } = await reader.read();
if (done) break;
for (const line of decoder.decode(value).split("\n")) {
if (!line.startsWith("data:") || line.includes("[DONE]")) continue;
const chunk = JSON.parse(line.slice(5));
if (chunk.delta?.content) process.stdout.write(chunk.delta.content);
}
}Tool-Calls und strukturierte Ausgaben können ebenfalls gestreamt werden; siehe deren Referenzen. Unterstützt das gewählte Modell kein Streaming, gibt die API einen klaren Fehler zurück, anstatt einen unvollständigen Körper zu liefern.
War dieser Artikel hilfreich?
