流式输出
在请求中设置 "stream": true,响应将以 Server-Sent Events(SSE) 逐段返回,
适合聊天界面的打字机效果与长回复场景。
请求
curl https://api.4allapi.com/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $AIPROXY_KEY" \ -d '{ "model": "gpt-5.6", "messages": [{"role": "user", "content": "写一首关于星空的短诗"}], "stream": true, "stream_options": {"include_usage": true} }'响应流
每个事件是一行 data: 前缀的 JSON,增量内容在
choices[0].delta.content 中;流结束时发送 data: [DONE]:
data: {"choices":[{"delta":{"content":"夜"}}], ...}data: {"choices":[{"delta":{"content":"空"}}], ...}...data: {"choices":[],"usage":{"prompt_tokens":18,"completion_tokens":56,"total_tokens":74}}data: [DONE]SDK 示例(Python)
stream = client.chat.completions.create( model="gpt-5.6", messages=[{"role": "user", "content": "写一首关于星空的短诗"}], stream=True,)for chunk in stream: delta = chunk.choices[0].delta.content if chunk.choices else None if delta: print(delta, end="", flush=True)注意事项
- 流式连接的读超时建议设置在 60 秒以上,长回复的推理模型可适当加大;
- 客户端中断连接后,已生成部分仍会按实际用量计费;
- 代理/网关转发时需关闭响应缓冲(如 nginx 的
proxy_buffering off),否则会失去流式效果。