Skip to content
xsAI

Generate text ​

Send chat messages and collect the final assistant text.

This page describes the v0 API from v0.5.1. Use v0 package builds with these examples. Make sure that the service accepts the model ID and request protocol shown below.

sh
npm i @xsai/generate-text@0.5.1

Examples ​

Basic ​

ts
const { text } = await generateText({
  apiKey: env.OPENAI_API_KEY!,
  baseURL: 'https://api.openai.com/v1/',
  messages: [
    {
      content: 'You\'re a helpful assistant.',
      role: 'system'
    },
    {
      content: 'Why is the sky blue?',
      role: 'user'
    }
  ],
  model: 'gpt-4o',
})

Usage ​

usage contains token usage for the final step. totalUsage sums token usage across all steps, including requests after tool calls.

ts
const { text, totalUsage, usage } = await generateText({
  apiKey: env.OPENAI_API_KEY!,
  baseURL: 'https://api.openai.com/v1/',
  messages: [{
    content: 'Why is the sky blue?',
    role: 'user',
  }],
  model: 'gpt-4o',
})

console.log(text)
console.log(usage)
console.log(totalUsage)

Image input ​

Make sure that the model accepts the image or audio input before you send it. xsAI does not detect this capability.

ts
const { text } = await generateText({
  apiKey: env.OPENAI_API_KEY!,
  baseURL: 'https://api.openai.com/v1/',
  messages: [{
    content: [
      { text: 'What\'s in this image?', type: 'text' },
      { 
        image_url: { 
          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', 
        }, 
        type: 'image_url', 
      }, 
    ],
    role: 'user',
  }],
  model: 'gpt-4o',
})

Audio input ​

Make sure that the model accepts the image or audio input before you send it. xsAI does not detect this capability.

ts
const data = await fetch('https://cdn.openai.com/API/docs/audio/alloy.wav')
  .then(res => res.arrayBuffer())
  .then(buffer => Buffer.from(buffer).toString('base64'))

const { text } = await generateText({
  apiKey: env.OPENAI_API_KEY!,
  baseURL: 'https://api.openai.com/v1/',
  messages: [{
    content: [
      { text: 'What is in this recording?', type: 'text' },
      { input_audio: { data, format: 'wav' }, type: 'input_audio' } 
    ],
    role: 'user',
  }],
  modalities: ['text', 'audio'], 
  model: 'gpt-4o-audio-preview', 
})

Result ​

The basic example prints the requested sentence. Model output can vary.