串流訊息
在建立訊息時,您可以設定 "stream": true
來使用伺服器發送事件 (SSE) 進行增量式串流回應。
使用 SDK 進行串流
我們的 Python 和 TypeScript SDK 提供多種串流方式。Python SDK 同時支援同步和非同步串流。詳情請參閱各 SDK 的文件。
事件類型
每個伺服器發送事件都包含一個命名的事件類型和相關的 JSON 資料。每個事件都會使用 SSE 事件名稱(例如 event: message_stop
),並在其資料中包含相對應的事件 type
。
每個串流使用以下事件流程:
message_start
:包含一個帶有空content
的Message
物件。- 一系列內容區塊,每個區塊都有一個
content_block_start
、一個或多個content_block_delta
事件,以及一個content_block_stop
事件。每個內容區塊都有一個index
,對應於其在最終 Messagecontent
陣列中的索引。 - 一個或多個
message_delta
事件,表示最終Message
物件的頂層變更。 - 最後的
message_stop
事件。
Ping 事件
事件串流中可能還包含任意數量的 ping
事件。
錯誤事件
我們可能偶爾會在事件串流中發送錯誤。例如,在高使用量期間,您可能會收到 overloaded_error
,這在非串流環境中通常對應於 HTTP 529:
event: error
data: {"type": "error", "error": {"type": "overloaded_error", "message": "Overloaded"}}
其他事件
根據我們的版本控制政策,我們可能會添加新的事件類型,您的程式碼應該能夠優雅地處理未知的事件類型。
Delta 類型
每個 content_block_delta
事件都包含一個 delta
,用於更新給定 index
的 content
區塊。
文字 delta
文字內容區塊 delta 看起來像這樣:
event: content_block_delta
data: {"type": "content_block_delta","index": 0,"delta": {"type": "text_delta", "text": "ello frien"}}
輸入 JSON delta
tool_use
內容區塊的 delta 對應於區塊的 input
欄位的更新。為了支援最大的粒度,delta 是_部分 JSON 字串_,而最終的 tool_use.input
始終是一個_物件_。
您可以累積字串 delta,並在收到 content_block_stop
事件後解析 JSON,可以使用像 Pydantic 這樣的函式庫進行部分 JSON 解析,或使用我們的 SDK,它們提供了訪問已解析增量值的輔助工具。
tool_use
內容區塊 delta 看起來像這樣:
event: content_block_delta
data: {"type": "content_block_delta","index": 1,"delta": {"type": "input_json_delta","partial_json": "{\"location\": \"San Fra"}}}
注意:我們目前的模型只支援一次從 input
中發出一個完整的鍵值屬性。因此,在使用工具時,模型工作時可能會在串流事件之間出現延遲。一旦累積了一個 input
鍵值,我們會將它們作為多個帶有分塊部分 json 的 content_block_delta
事件發出,這樣格式就可以自動支援未來模型中的更細粒度。
原始 HTTP 串流回應
我們強烈建議在使用串流模式時使用我們的客戶端 SDK。但是,如果您正在建立直接的 API 整合,則需要自行處理這些事件。
串流回應由以下部分組成:
- 一個
message_start
事件 - 可能有多個內容區塊,每個區塊包含:
a. 一個
content_block_start
事件 b. 可能有多個content_block_delta
事件 c. 一個content_block_stop
事件 - 一個
message_delta
事件 - 一個
message_stop
事件
回應中可能還會分散著 ping
事件。有關格式的更多詳細信息,請參見事件類型。
基本串流請求
curl https://api.anthropic.com/v1/messages \
--header "anthropic-version: 2023-06-01" \
--header "content-type: application/json" \
--header "x-api-key: $ANTHROPIC_API_KEY" \
--data \
'{
"model": "claude-3-5-sonnet-20241022",
"messages": [{"role": "user", "content": "Hello"}],
"max_tokens": 256,
"stream": true
}'
event: message_start
data: {"type": "message_start", "message": {"id": "msg_1nZdL29xx5MUA1yADyHTEsnR8uuvGzszyY", "type": "message", "role": "assistant", "content": [], "model": "claude-3-5-sonnet-20241022", "stop_reason": null, "stop_sequence": null, "usage": {"input_tokens": 25, "output_tokens": 1}}}
event: content_block_start
data: {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}}
event: ping
data: {"type": "ping"}
event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "Hello"}}
event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "!"}}
event: content_block_stop
data: {"type": "content_block_stop", "index": 0}
event: message_delta
data: {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence":null}, "usage": {"output_tokens": 15}}
event: message_stop
data: {"type": "message_stop"}
使用工具的串流請求
在這個請求中,我們要求 Claude 使用工具來告訴我們天氣。
curl https://api.anthropic.com/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-3-5-sonnet-20241022",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
}
},
"required": ["location"]
}
}
],
"tool_choice": {"type": "any"},
"messages": [
{
"role": "user",
"content": "What is the weather like in San Francisco?"
}
],
"stream": true
}'
event: message_start
data: {"type":"message_start","message":{"id":"msg_014p7gG3wDgGV9EUtLvnow3U","type":"message","role":"assistant","model":"claude-3-haiku-20240307","stop_sequence":null,"usage":{"input_tokens":472,"output_tokens":2},"content":[],"stop_reason":null}}
event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}
event: ping
data: {"type": "ping"}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Okay"}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":","}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" let"}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"'s"}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" check"}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" the"}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" weather"}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" for"}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" San"}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" Francisco"}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":","}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" CA"}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":":"}}
event: content_block_stop
data: {"type":"content_block_stop","index":0}
event: content_block_start
data: {"type":"content_block_start","index":1,"content_block":{"type":"tool_use","id":"toolu_01T1x1fJ34qAmk2tNTrN7Up6","name":"get_weather","input":{}}}
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":""}}
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\"location\":"}}
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" \"San"}}
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" Francisc"}}
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"o,"}}
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" CA\""}}
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":", "}}
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\"unit\": \"fah"}}
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"renheit\"}"}}
event: content_block_stop
data: {"type":"content_block_stop","index":1}
event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"tool_use","stop_sequence":null},"usage":{"output_tokens":89}}
event: message_stop
data: {"type":"message_stop"}