總覽
串流允許您在回應生成時即時接收部分內容,為聊天應用程式提供更好的使用者體驗。啟用串流
在您的請求中設定stream: true:
複製
curl https://api.lemondata.cc/v1/chat/completions \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "Write a short poem"}],
"stream": true
}'
串流回應格式
串流中的每個區塊 (chunk) 都遵循以下格式:複製
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1234567890,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1234567890,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":" world"},"finish_reason":null}]}
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1234567890,"model":"gpt-4o","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]
處理串流結束
串流結束於:finish_reason: "stop"- 正常完成finish_reason: "length"- 達到max_tokens限制finish_reason: "tool_calls"- 模型想要呼叫工具data: [DONE]- 最後一則訊息
收集完整回應
要在串流時收集完整的回應:複製
full_response = ""
for chunk in stream:
if chunk.choices[0].delta.content:
content = chunk.choices[0].delta.content
full_response += content
print(content, end="", flush=True)
print(f"\n\nFull response: {full_response}")
非同步串流 (Async Streaming)
對於非同步應用程式:複製
import asyncio
from openai import AsyncOpenAI
async def main():
client = AsyncOpenAI(
api_key="sk-your-api-key",
base_url="https://api.lemondata.cc/v1"
)
stream = await client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}],
stream=True
)
async for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
asyncio.run(main())
網頁應用程式範例
對於網頁聊天介面:複製
async function streamChat(message) {
const response = await fetch('https://api.lemondata.cc/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': 'Bearer sk-your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'gpt-4o',
messages: [{ role: 'user', content: message }],
stream: true
})
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n').filter(line => line.startsWith('data: '));
for (const line of lines) {
const data = line.slice(6);
if (data === '[DONE]') return;
const parsed = JSON.parse(data);
const content = parsed.choices[0]?.delta?.content;
if (content) {
// Append to your UI
document.getElementById('output').textContent += content;
}
}
}
}