Skip to main content

Streaming Responses

Get real-time AI responses using Server-Sent Events (SSE) for a more interactive user experience.

Endpoint

PUT /api/v1/assistant/get-answer-stream

Authentication

Requires valid session authentication via middleware.

Request Format

Same as the Get Answer endpoint - sent as multipart/form-data.

Response Format

The response is streamed using Server-Sent Events with the following headers:

Content-Type: text/event-stream
Cache-Control: no-cache
Connection: keep-alive

Stream Format

The response streams text content in real-time as the AI generates it:

Hello! I'd be happy to explain quantum computing.

Quantum computing is a revolutionary approach to computation that harnesses the principles of quantum mechanics...

The key difference from classical computers is that quantum computers use quantum bits or "qubits"...

Example Usage

JavaScript (Fetch API)

const formData = new FormData();
formData.append(
'inputs',
JSON.stringify([
{
role: 'user',
value: 'Explain machine learning',
},
])
);
formData.append(
'toolKitSettings',
JSON.stringify({
model: 'gpt-4o',
temperature: 0.7,
chatLang: 'english',
})
);

const response = await fetch('/api/v1/assistant/get-answer-stream', {
method: 'PUT',
body: formData,
});

const reader = response.body.getReader();
const decoder = new TextDecoder();

while (true) {
const { done, value } = await reader.read();
if (done) break;

const chunk = decoder.decode(value);
console.log('Received:', chunk);
// Update UI with streaming text
updateUI(chunk);
}

JavaScript (EventSource Alternative)

Since EventSource doesn't support POST requests, use fetch with ReadableStream:

async function streamResponse(formData) {
const response = await fetch('/api/v1/assistant/get-answer-stream', {
method: 'PUT',
body: formData,
});

if (!response.ok) {
throw new Error('Stream request failed');
}

const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';

try {
while (true) {
const { done, value } = await reader.read();

if (done) {
if (buffer) {
handleStreamChunk(buffer);
}
break;
}

buffer += decoder.decode(value, { stream: true });

// Process complete chunks
const chunks = buffer.split('\n');
buffer = chunks.pop() || ''; // Keep incomplete chunk

chunks.forEach((chunk) => {
if (chunk.trim()) {
handleStreamChunk(chunk);
}
});
}
} finally {
reader.releaseLock();
}
}

function handleStreamChunk(chunk) {
// Update your UI with the streaming text
document.getElementById('response').textContent += chunk;
}