流式输出

请求中加上 "stream": true,模型边生成边返回,用户能更快看到第一个字。适合聊天界面和长文本生成。

示例

curl -N https://shutong.ai/api/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $SHUTONG_API_KEY" \
  -d '{
    "model": "deepseek-chat",
    "stream": true,
    "messages": [{"role": "user", "content": "写一段 100 字的产品介绍"}]
  }'

数据格式

响应为 Server-Sent Events,每行 data: 后是一个 JSON 数据块,最后以 data: [DONE] 结束。

data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"你"}}]}

data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"好"}}]}

data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[],"usage":{"prompt_tokens":14,"completion_tokens":28,"total_tokens":42}}

data: [DONE]