流式输出
开启
请求体里 stream: true。
SSE 格式
SSE
data: {"choices":[{"delta":{"content":"你"}}]}
data: {"choices":[{"delta":{"content":"好"}}]}
data: [DONE]Python 示例
Python
stream = client.chat.completions.create(
model="glm-5.3",
messages=[{"role": "user", "content": "写一段 200 字介绍"}],
stream=True
)
for chunk in stream:
d = chunk.choices[0].delta.content
if d:
print(d, end="", flush=True)注意
usage在最后一帧返回- 自己解析 SSE 时记得处理
data: [DONE] - 流式下
first_token_ms会记入用量日志,可看首字延迟