Use stream: true to receive incremental output (when supported by the selected model/provider).
When you set stream to true on /chat/completions, the server may respond using
Server-Sent Events (SSE) depending on the model/provider and request type.
Below is a minimal SSE reader. You’ll want to add robust parsing and timeouts for production.
const res = await fetch('https://api.aitaana.com/api/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
message: 'Write a short story.',
model: 'gpt-4',
stream: true,
}),
});
if (!res.ok || !res.body) throw new Error('Request failed');
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
while (true) {
const { value, done } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
// TODO: parse SSE frames from buffer and render partial output
console.log(buffer);
}