Đang truyền phát
Đang truyền phát
Đặt stream: true bật hoàn tất trò chuyện, phản hồi hoặc cả hai vendor SDK và phản hồi đến dưới dạng server-sent events. Mọi SDK chính thức đều xử lý việc đóng gói này cho bạn; cấu trúc bên dưới là những gì bạn nhận được qua HTTP thuần túy.
import os from openai import OpenAI client = OpenAI( api_key=os.environ["OXYY_API_KEY"], base_url="https://api.oxyy.ai/v1" ) stream = client.chat.completions.create( model="gpt-5.5", messages=[{"role": "user", "content": "Write a haiku about caching."}], stream=True, stream_options={"include_usage": True}, # final chunk carries usage + cost ) for chunk in stream: if chunk.choices and chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="", flush=True) elif chunk.usage: print(f"\n\ncost: {chunk.usage.cost}")
import OpenAI from 'openai'; const client = new OpenAI({ apiKey: process.env.OXYY_API_KEY, baseURL: 'https://api.oxyy.ai/v1' }); const stream = await client.chat.completions.create({ model: 'gpt-5.5', messages: [{ role: 'user', content: 'Write a haiku about caching.' }], stream: true, stream_options: { include_usage: true }, }); for await (const chunk of stream) { const delta = chunk.choices?.[0]?.delta?.content; if (delta) process.stdout.write(delta); if (chunk.usage) console.log('\ncost:', chunk.usage.cost); }
curl -N https://api.oxyy.ai/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OXYY_API_KEY" \ -d '{ "model": "gpt-5.5", "messages": [{"role": "user", "content": "Hello!"}], "stream": true, "stream_options": {"include_usage": true} }'
Trên đường truyền
text/event-stream
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1700000000,"model":"gpt-5.5","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1700000000,"model":"gpt-5.5","choices":[{"index":0,"delta":{"content":"Cold"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1700000000,"model":"gpt-5.5","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
// Only when stream_options.include_usage is true — choices is empty here.
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1700000000,"model":"gpt-5.5","choices":[],"usage":{"prompt_tokens":12,"completion_tokens":17,"total_tokens":29,"cost":0.000106}}
data: [DONE]| Tham số | Loại | Bắt buộc | Mô tả |
|---|---|---|---|
| stream | boolean | Không bắt buộc | Truyền phát câu trả lời dưới dạng sự kiện do máy chủ gửi. Mặc định false |
| stream_options.include_usage | boolean | Không bắt buộc | Phát ra một khối cuối cùng chứa usage — số lượng token và cost — với một mảng choices trống, ngay trước [DONE]. Mặc định false |
Những điều cần biết. Stream luôn kết thúc bằng
data: [DONE]. Việc đóng kết nối sẽ hủy quá trình tạo ở thượng nguồn và bạn chỉ bị tính phí cho những gì đã được tạo. Sự cố xảy ra sau khi byte đầu tiên đến sẽ dưới dạng khung lỗi bên trong luồng thay vì trạng thái HTTP, vì trạng thái đã được gửi đi rồi.Idempotency-Key bị bỏ qua đối với các yêu cầu streaming — stream không thể phát lại.