流式输出
流式输出(Server-Sent Events)允许实时接收模型的生成内容,适用于需要逐字显示的场景。
启用流式
在请求中设置 stream: true 即可启用流式输出
curl -X POST https://api.aitiktak.com/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-plus",
"messages": [{"role": "user", "content": "Hello!"}],
"stream": true
}'流式响应格式
流式响应使用 SSE(Server-Sent Events)格式,每条消息以 data: 开头
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"role":"assistant"},"finish_reason":null}]}
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"你好"},"finish_reason":null}]}
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"!"},"finish_reason":null}]}
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"有什么"},"finish_reason":null}]}
data: {"id":"chatcmpl-xxx","choices":[{"delta":{},"finish_reason":"stop"}]}
data: [DONE]Python SDK 示例
from aitiktak import AItiktak
client = AItiktak(api_key="YOUR_API_KEY")
stream = client.chat.completions.create(
model="qwen-plus",
messages=[{"role": "user", "content": "Hello!"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)