Skip to content

Streaming, progress and cancellation

askStream() / ask_stream() yield the answer’s text as the LLM writes it. They take the same options as ask().

for await (const delta of doc.askStream("Explain the introduction")) {
process.stdout.write(delta);
}

A stream yields text only. To get citations with a streamed answer, collect the text and call extractCitations(text, hits) (TypeScript) or extract_citations(text, hits) (Python) with the passages from search(), or use ask(). Over HTTP, send "stream": true to /ask; see REST API.

Chunks are embedded in batches of 64. The progress callback runs after each batch with the document’s source, the chunks embedded so far, and the total.

await collection.build({
onProgress: ({ source, embedded, total }) => console.log(`${source}: ${embedded}/${total}`),
});

A document whose saved index is reused reports no progress: it needs no embedding.

A cancelled build stops between embedding batches and keeps the previous index: the old index is replaced only once all chunks are embedded.

Pass an AbortSignal. The build throws an AbortError.

const controller = new AbortController();
setTimeout(() => controller.abort(), 30_000);
try {
await collection.build({ signal: controller.signal });
} catch (err) {
if ((err as Error).name !== "AbortError") throw err;
}

ask() and askStream() take the same signal, which cancels the LLM request:

await doc.ask("Summarize the document", { signal: AbortSignal.timeout(20_000) });