Streaming
Show the answer while it is being written.
Streaming makes chat feel faster. Your app receives small pieces of the answer as soon as they are ready.
Change one field
Set stream to true. Qabas then sends Server-Sent Events until the answer is complete.
- Your app opens one HTTPS request.
- Qabas sends text chunks as the model creates them.
- The stream ends with
[DONE].
Try it in Terminal
The -N option tells cURL to show each chunk immediately.
curl -N https://api.qabas.ae/v1/chat/completions \
-H "Authorization: Bearer $QABAS_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-2.5-7b",
"messages": [{"role": "user", "content": "Explain Dubai in three sentences."}],
"stream": true
}'Python
This works with the official OpenAI Python package.
from openai import OpenAI
import os
client = OpenAI(
api_key=os.environ["QABAS_API_KEY"],
base_url="https://api.qabas.ae/v1",
)
stream = client.chat.completions.create(
model="qwen-2.5-7b",
messages=[{"role": "user", "content": "Explain Dubai in three sentences."}],
stream=True,
)
for chunk in stream:
text = chunk.choices[0].delta.content or ""
print(text, end="", flush=True)TypeScript or Node.js
This works with the official OpenAI JavaScript package.
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.QABAS_API_KEY,
baseURL: "https://api.qabas.ae/v1",
});
const stream = await client.chat.completions.create({
model: "qwen-2.5-7b",
messages: [{ role: "user", content: "Explain Dubai in three sentences." }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}