xsAI v0.2 is now available! Read Announcement
xsAI0.3.0-beta.6
Streaming

Text

Streams text from a given prompt.

install sizeminified sizeminzipped size
npm i @xsai/stream-text

Examples

Basic

import {  } from '@xsai/stream-text'
import {  } from 'node:process'
 
const {  } = await ({
  : .!,
  : '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."
.()

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 {  } = await ({
  : .!,
  : '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 {  } = await ({
  : .!,
  : 'https://api.openai.com/v1/',
  : [{
    : 'user',
    : [
      { : 'text', : 'What is in this recording?' },
      { : 'input_audio', : { , : 'wav' }} 
    ],
  }],
  : 'gpt-4o-audio-preview', 
  : ['text', 'audio'], 
})
Edit on GitHub

Last updated on

On this page