跳转到内容
返回主页

流式响应

设置 stream: true 后,接口以 SSE 格式返回增量事件。

Terminal window
curl -N "${BASE_URL}/v1/chat/completions" \
-H "Authorization: Bearer ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4.1",
"messages": [
{
"role": "user",
"content": "连续输出三条排查建议"
}
],
"stream": true,
"stream_options": {
"include_usage": true
}
}'
data: {"id":"chatcmpl_xxx","object":"chat.completion.chunk","choices":[{"delta":{"content":"第一"}}]}
data: {"id":"chatcmpl_xxx","object":"chat.completion.chunk","choices":[{"delta":{"content":"条建议"}}]}
data: {"id":"chatcmpl_xxx","object":"chat.completion.chunk","choices":[{"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":20,"completion_tokens":30,"total_tokens":50}}
data: [DONE]
  • 按 SSE 的 data: 行逐条解析
  • 收到 [DONE] 后关闭本次流
  • 如果需要在客户端展示用量,传入 stream_options.include_usage=true
  • 客户端断开后,本次请求可能仍会完成计费
stream = client.chat.completions.create(
model="gpt-4.1",
messages=[{"role": "user", "content": "连续输出三条排查建议"}],
stream=True,
stream_options={"include_usage": True},
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")