xsAI v0.1 is now available! Read Announcement
xsAI0.2.0-beta.1
Generating

Structured Data

Generates structured data for a given prompt and schema.

For convenience, we use valibot as an example.

But this package uses xsschema internally, so it supports any schema library that xsschema supports: e.g. zod, valibot, arktype, etc.

See xsschema for more information.

install sizeminified sizeminzipped size
npm i @xsai/generate-object

Examples

Object

import {  } from '@xsai/generate-object'
import {  } from 'node:process'
import * as  from 'valibot'
 
const {  } = await ({
  : .!,
  : 'https://api.openai.com/v1/',
  : [
    {
      : 'Extract the event information.',
      : 'system'
    },
    {
      : 'Alice and Bob are going to a science fair on Friday.',
      : 'user'
    }
  ],
  : 'gpt-4o',
  : .({
    : .(),
    : .(),
    : .(.()),
  })
})

Array

import {  } from '@xsai/generate-object'
import {  } from 'node:process'
import * as  from 'valibot'
 
const { :  } = await ({
  : .!,
  : 'https://api.openai.com/v1/',
  : [
    {
      : 'Generate 3 hero descriptions for a fantasy role playing game.',
      : 'user'
    }
  ],
  : 'gpt-4o',
  : 'array', 
  : .({
    : .(),
    : .(
      .(),
      .('Character class, e.g. warrior, mage, or thief.'),
    ),
    : .(),
  })
})
 
for (const  of ) { 
  .()
}
Edit on GitHub

Last updated on

On this page