import type {
ResponseSchema,
ResponseSchemaStructure,
ResponseSchemaProperty
} from '@adaline/api';
// Define properties
const properties: Record<string, ResponseSchemaProperty> = {
name: {
type: 'string',
description: 'Full name',
minLength: 1,
maxLength: 100
},
email: {
type: 'string',
description: 'Email address',
pattern: '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$'
},
age: {
type: 'number',
description: 'Age in years',
minimum: 0,
maximum: 120
},
interests: {
type: 'array',
items: { type: 'string' },
description: 'User interests',
maxItems: 10
},
isPremium: {
type: 'boolean',
description: 'Premium account status',
default: false
}
};
// Build structure
const structure: ResponseSchemaStructure = {
type: 'object',
required: ['name', 'email'],
properties,
additionalProperties: false
};
// Complete schema
const schema: ResponseSchema = {
name: 'user_profile',
description: 'Extract user profile from conversation',
strict: true,
schema: structure
};
// Use with LLM (OpenAI example)
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'system', content: 'Extract user info as JSON' },
{ role: 'user', content: 'I am John Doe, email [email protected], 35 years old' }
],
response_format: {
type: 'json_schema',
json_schema: {
name: schema.name,
description: schema.description,
strict: schema.strict,
schema: schema.schema
}
}
});
const extracted = JSON.parse(response.choices[0].message.content || '{}');
// { name: 'John Doe', email: '[email protected]', age: 35 }