流式输出
设置 stream: true 启用流式输出,服务端以 SSE(Server-Sent Events)格式逐 token 返回。
Python
from openai import OpenAI
client = OpenAI(
api_key="sk-your-api-key",
base_url="https://api.ogmiao.com/v1"
)
stream = client.chat.completions.create(
model="glm-4.7-flash",
messages=[{"role": "user", "content": "写一首诗"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")SSE 数据格式:
SSE
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","created":1700000000,"model":"glm-4.7-flash","choices":[{"index":0,"delta":{"content":"你"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","created":1700000000,"model":"glm-4.7-flash","choices":[{"index":0,"delta":{"content":"好"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","created":1700000000,"model":"glm-4.7-flash","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]