I want you to build me a custom object generator for schema.
Here a some examples
Create a few examples, show me all the options
I'll show you how to build a custom object generator using the AI SDK, which provides a standardized way to generate structured data from language models 1. This is perfect for creating interfaces similar to the ones in your screenshots.
generateObject
The AI SDK provides the generateObject
function which allows you to generate typed, structured objects based on a schema 1. This is ideal for:
The AI SDK supports several output strategies 2:
import { generateObject } from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';
const { object } = await generateObject({
model: openai('gpt-4o'),
schema: z.object({
recipe: z.object({
name: z.string(),
ingredients: z.array(z.string()),
steps: z.array(z.string()),
}),
}),
prompt: 'Generate a lasagna recipe.',
});
console.log(JSON.stringify(object, null, 2));
import { generateObject } from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';
const { object } = await generateObject({
model: openai('gpt-4o'),
output: 'array',
schema: z.object({
name: z.string(),
class: z.string().describe('Character class, e.g. warrior, mage, or thief.'),
description: z.string(),
}),
prompt: 'Generate 3 hero descriptions for a fantasy role playing game.',
});
import { generateObject } from 'ai';
import { openai } from '@ai-sdk/openai';
const { object } = await generateObject({
model: openai('gpt-4o'),
output: 'enum',
enum: ['action', 'comedy', 'drama', 'horror', 'sci-fi'],
prompt: 'Classify the genre of this movie plot: "A group of astronauts travel through a wormhole in search of a new habitable planet for humanity."',
});
import { generateObject } from 'ai';
import { openai } from '@ai-sdk/openai';
const { object } = await generateObject({
model: openai('gpt-4o'),
output: 'no-schema',
prompt: 'Generate a lasagna recipe.',
});
Let's create a custom interface for defining schemas and generating structured data:
For real-time UI updates as the model generates data 2:
import { streamObject } from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';
const { partialObjectStream } = streamObject({
model: openai('gpt-4o'),
schema: z.object({
recipe: z.object({
name: z.string(),
ingredients: z.array(z.string()),
steps: z.array(z.string()),
}),
}),
prompt: 'Generate a lasagna recipe.',
});
for await (const partialObject of partialObjectStream) {
console.log(partialObject); // Updates as the object is generated
}
You can include images in your prompts for multimodal models 3:
import { generateObject } from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';
const { object } = await generateObject({
model: openai('gpt-4o'),
schema: z.object({
description: z.string(),
objects: z.array(z.string()),
colors: z.array(z.string()),
}),
messages: [
{
role: 'user',
content: [
{
type: 'text',
text: 'Describe this image in detail',
},
{
type: 'image',
image: new URL('https://example.com/image.jpg'),
},
],
},
],
});
The AI SDK supports different schema definition approaches 2:
When building your custom object generator:
The AI SDK provides a powerful foundation for building structured data generation tools 4, making it easy to create interfaces similar to the ones in your screenshots.