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.
npm i @xsai/generate-objectExamples
These below examples require you to install a standard schema to json schema parser (a separate package). As they are not provided by zod or valibot natively.
Read more about them here
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 ) {
.()
}