stream: true:{
"model": "deepseek-v3",
"messages": [{"role": "user", "content": "写一首诗"}],
"stream": true
}text/event-stream。data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1713833628,"model":"deepseek-v3","choices":[{"index":0,"delta":{"content":"你"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1713833628,"model":"deepseek-v3","choices":[{"index":0,"delta":{"content":"好"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1713833628,"model":"deepseek-v3","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]data: 开头,后面是 JSONdata: [DONE],表示流结束delta.content 是本次新增的文本片段,拼起来就是完整回复finish_reason 为 stop 表示正常结束stream_options:{
"model": "deepseek-v3",
"messages": [{"role": "user", "content": "你好"}],
"stream": true,
"stream_options": {"include_usage": true}
}{
"id": "chatcmpl-abc123",
"object": "chat.completion.chunk",
"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}],
"usage": {"prompt_tokens": 10, "completion_tokens": 20, "total_tokens": 30}
}stream: true,格式与 Chat 类似,也是 SSE 协议,最后以 data: [DONE] 结束。data: 开头data: [DONE] 就停止读取| 维度 | 非流式 (stream: false) | 流式 (stream: true) |
|---|---|---|
| 响应方式 | 一次性返回完整结果 | 逐块返回文本片段 |
| 用户感知 | 等待时间较长 | 逐字出现,体感更快 |
| 响应格式 | chat.completion | chat.completion.chunk |
| Usage | 默认包含 | 需设置 stream_options |
| 适用场景 | 后端批处理、API 串联 | 前端对话、实时交互 |
| 解析难度 | 简单,直接读 JSON | 需要 SSE 解析 |