Streaming
Text
Streams text from a given prompt.
npm i @xsai/stream-textExamples
Basic
import { } from '@xsai/stream-text'
import { } from 'node:process'
const { } = ({
: .!,
: 'https://api.openai.com/v1/',
: [
{
: 'You are a helpful assistant.',
: 'system',
},
{
: 'This is a test, so please answer'
+ '\'The quick brown fox jumps over the lazy dog.\''
+ 'and nothing else.',
: 'user',
},
],
: 'gpt-4o',
})
const : string[] = []
for await (const of ) {
.()
}
// "The quick brown fox jumps over the lazy dog."
.()Streams and events
streamText() exposes multiple streams for different levels of detail:
textStream: text deltas onlyreasoningTextStream: reasoning deltas only, when the model emits themeventStream: normalized xsAI eventsfullStream: parsed chat completion chunks from the provider
eventStream emits events for each step. Depending on the response, a step can include:
step.startandstep.done(with optional usage onstep.done)reasoning.start,reasoning.delta, andreasoning.donetext.start,text.delta, andtext.donetool-call.start,tool-call.delta, andtool-call.donetool-result.done
Event names use dot notation. Use eventStream for normalized text, reasoning, and tool events; use fullStream when you need the provider's original chat completion chunks.
import { } from '@xsai/stream-text'
import { } from 'node:process'
const { , } = ({
: .!,
: 'https://api.openai.com/v1/',
: [{
: 'Tell me a short joke.',
: 'user',
}],
: 'gpt-4o',
})
for await (const of ) {
if (. === 'text.delta')
.(.)
if (. === 'step.done')
.('step usage:', .)
}
for await (const of ) {
.(.)
}Image input
xsAI has no way of knowing if your model supports multi-modal, so please check before using it.
import { } from '@xsai/stream-text'
import { } from 'node:process'
const { } = ({
: .!,
: 'https://api.openai.com/v1/',
: [{
: 'user',
: [
{ : 'text', : 'What\'s in this image?' },
{
: 'image_url',
: {
: 'https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg',
},
},
],
}],
: 'gpt-4o',
})Audio input
xsAI has no way of knowing if your model supports multi-modal, so please check before using it.
import { } from '@xsai/stream-text'
import { } from 'node:buffer'
import { } from 'node:process'
const = await ('https://cdn.openai.com/API/docs/audio/alloy.wav')
.( => .())
.( => .().('base64'))
const { } = ({
: .!,
: 'https://api.openai.com/v1/',
: [{
: 'user',
: [
{ : 'text', : 'What is in this recording?' },
{ : 'input_audio', : { , : 'wav' }}
],
}],
: 'gpt-4o-audio-preview',
: ['text', 'audio'],
})