Tổng Quan
Đặtstream: true để nhận phản hồi dưới dạng Server-Sent Events (SSE). Mỗi sự kiện chứa một phần phản hồi, cho phép bạn hiển thị nội dung theo thời gian thực khi nó được tạo.
Định Dạng Luồng
Luồng gửi các dòngdata: chứa các đối tượng JSON:
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]
Đối Tượng Chunk
Mỗi chunk chứa:| Trường | Kiểu | Mô tả |
|---|---|---|
id | string | Chia sẻ giữa tất cả chunk trong một phản hồi |
object | string | Luôn là chat.completion.chunk |
created | integer | Dấu thời gian Unix |
model | string | Mô hình đã tạo phản hồi |
choices[].delta.role | string | Chỉ xuất hiện trong chunk đầu tiên |
choices[].delta.content | string | Nội dung token (có thể rỗng) |
choices[].finish_reason | string | null | stop, length hoặc null khi đang streaming |
Ví Dụ
curl https://api.openmodex.com/v1/chat/completions \
-H "Authorization: Bearer omx_sk_YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "Write a poem about the sea."}],
"stream": true
}'
import OpenModex from '@openmodex/sdk';
const client = new OpenModex({ apiKey: 'omx_sk_YOUR_KEY' });
const stream = await client.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Write a poem about the sea.' }],
stream: true,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) process.stdout.write(content);
}
from openmodex import OpenModex
client = OpenModex(api_key="omx_sk_YOUR_KEY")
stream = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Write a poem about the sea."}],
stream=True,
)
for chunk in stream:
content = chunk.choices[0].delta.content
if content:
print(content, end="", flush=True)
import { useChat } from '@openmodex/react';
function Chat() {
const { messages, input, setInput, sendMessage, isLoading, stop } = useChat({
apiKey: 'omx_sk_YOUR_KEY',
model: 'gpt-4o',
});
return (
<div>
{messages.map((m) => (
<p key={m.id}><b>{m.role}:</b> {m.content}</p>
))}
<input value={input} onChange={(e) => setInput(e.target.value)} />
<button onClick={() => sendMessage()} disabled={isLoading}>Send</button>
{isLoading && <button onClick={stop}>Stop</button>}
</div>
);
}
Hủy Bỏ Luồng
Bạn có thể hủy một luồng đang tiến hành:const stream = await client.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Write a long story...' }],
stream: true,
});
// Abort after 2 seconds
setTimeout(() => stream.abort(), 2000);
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? '');
}
stream = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Write a long story..."}],
stream=True,
)
for chunk in stream:
content = chunk.choices[0].delta.content
if content:
print(content, end="", flush=True)
# Call stream.close() to abort