Streaming Text Completions
Legacy API
Die Text Completions API ist eine Legacy-API. Zukünftige Modelle und Funktionen erfordern die Verwendung der Messages API, und wir empfehlen eine Migration so bald wie möglich.
Beim Erstellen einer Text Completion können Sie "stream": true
setzen, um die Antwort inkrementell über Server-Sent Events (SSE) zu streamen. Wenn Sie unsere Client-Bibliotheken verwenden, wird das Parsen dieser Events automatisch für Sie erledigt. Wenn Sie jedoch eine direkte API-Integration erstellen, müssen Sie diese Events selbst behandeln.
Beispiel
curl https://api.anthropic.com/v1/complete \
--header "anthropic-version: 2023-06-01" \
--header "content-type: application/json" \
--header "x-api-key: $ANTHROPIC_API_KEY" \
--data '
{
"model": "claude-2",
"prompt": "\n\nHuman: Hallo, Welt!\n\nAssistant:",
"max_tokens_to_sample": 256,
"stream": true
}
'
event: completion
data: {"type": "completion", "completion": " Hallo", "stop_reason": null, "model": "claude-2.0"}
event: completion
data: {"type": "completion", "completion": "!", "stop_reason": null, "model": "claude-2.0"}
event: ping
data: {"type": "ping"}
event: completion
data: {"type": "completion", "completion": " Mein", "stop_reason": null, "model": "claude-2.0"}
event: completion
data: {"type": "completion", "completion": " Name", "stop_reason": null, "model": "claude-2.0"}
event: completion
data: {"type": "completion", "completion": " ist", "stop_reason": null, "model": "claude-2.0"}
event: completion
data: {"type": "completion", "completion": " Claude", "stop_reason": null, "model": "claude-2.0"}
event: completion
data: {"type": "completion", "completion": ".", "stop_reason": null, "model": "claude-2.0"}
event: completion
data: {"type": "completion", "completion": "", "stop_reason": "stop_sequence", "model": "claude-2.0"}
Events
Jedes Event enthält einen benannten Eventtyp und zugehörige JSON-Daten.
Eventtypen: completion
, ping
, error
.
Fehler-Eventtypen
Gelegentlich senden wir möglicherweise Fehler im Event-Stream. Beispielsweise können Sie bei hoher Auslastung einen overloaded_error
erhalten, der normalerweise einem HTTP 529 in einem nicht-streamenden Kontext entsprechen würde:
event: completion
data: {"completion": " Hallo", "stop_reason": null, "model": "claude-2.0"}
event: error
data: {"error": {"type": "overloaded_error", "message": "Überlastet"}}
Ältere API-Versionen
Wenn Sie eine API-Version vor 2023-06-01
verwenden, wird das Antwortformat anders sein. Siehe Versionierung für Details.