文字補全
串流文字補全
在建立文字補全時,您可以設置 "stream": true
來使用 server-sent events (SSE) 逐步串流回應。如果您使用我們的客戶端函式庫,解析這些事件將自動為您處理。但是,如果您正在構建直接的 API 整合,您將需要自行處理這些事件。
範例
Request
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: Hello, world!\n\nAssistant:",
"max_tokens_to_sample": 256,
"stream": true
}
'
Response
event: completion
data: {"type": "completion", "completion": " 你好", "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": " 我", "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": 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"}
事件
每個事件都包含一個命名的事件類型和相關的 JSON 數據。
事件類型:completion
、ping
、error
。
錯誤事件類型
我們可能偶爾會在事件流中發送錯誤。例如,在使用量高的時期,您可能會收到一個 overloaded_error
,這通常對應於非串流上下文中的 HTTP 529:
Example error
event: completion
data: {"completion": " 你好", "stop_reason": null, "model": "claude-2.0"}
event: error
data: {"error": {"type": "overloaded_error", "message": "Overloaded"}}