Streaming
What is streaming
With "stream": true, the server returns content in chunks as Server-Sent
Events (SSE) instead of waiting for the full generation. Each chunk is a
data: {...} line, ending with data: [DONE].
When to use it
- Chat UIs: render tokens as they arrive for a smoother feel
- Long replies: see the first words sooner, less perceived waiting
curl example
curl https://api.moyuncourse.site/v1/chat/completions \
-H "Authorization: Bearer sk-nodaryx-xxxxxxxxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-flash",
"messages": [{ "role": "user", "content": "Write a short poem" }],
"stream": true
}'
Node.js example
const res = await fetch("https://api.moyuncourse.site/v1/chat/completions", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.NODARYX_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "gemini-flash",
messages: [{ role: "user", content: "Write a short poem" }],
stream: true,
}),
});
const reader = res.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { value, done } = await reader.read();
if (done) break;
process.stdout.write(decoder.decode(value));
}
Python example
from openai import OpenAI
client = OpenAI(
base_url="https://api.moyuncourse.site/v1",
api_key="sk-nodaryx-xxxxxxxxxxxxxxxx",
)
stream = client.chat.completions.create(
model="gemini-flash",
messages=[{"role": "user", "content": "Write a short poem"}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)
Security reminder
Never call the API directly from browser frontend code with your API key (streaming or not) — the key would be exposed to users. Proxy the request through your own backend. See Security best practices.