/** * Structured Outputs Example - Type-Safe Responses with Zod * * This example shows how to get type-safe responses from agents using Zod schemas. */ import { z } from 'zod'; import { Studio } from '../src'; // Define a Zod schema for the response const SentimentAnalysisSchema = z.object({ sentiment: z.enum(['positive', 'negative', 'neutral']), confidence: z.number().min(0).max(1), summary: z.string(), keywords: z.array(z.string()).optional() }); type SentimentAnalysis = z.infer; async function main() { const studio = new Studio({ apiKey: process.env.LYZR_API_KEY }); try { // Create agent with structured output model console.log('Creating sentiment analyzer...'); const agent = await studio.createAgent({ name: 'Sentiment Analyzer', provider: 'gpt-4o', role: 'Sentiment analysis expert', goal: 'Analyze text sentiment accurately', instructions: 'Return analysis as JSON with sentiment, confidence, and summary', responseModel: SentimentAnalysisSchema }); console.log('✓ Sentiment analyzer created\n'); // Analyze sentiment const text = 'I absolutely love this product! It works perfectly and exceeded my expectations.'; console.log(`Analyzing: "${text}"\n`); const result = await agent.run(text) as SentimentAnalysis; // Result is fully typed! console.log('Analysis Result:'); console.log(` Sentiment: ${result.sentiment}`); console.log(` Confidence: ${(result.confidence * 100).toFixed(1)}%`); console.log(` Summary: ${result.summary}`); if (result.keywords) { console.log(` Keywords: ${result.keywords.join(', ')}`); } // Clean up await agent.delete(); } catch (error) { console.error('Error:', error); process.exit(1); } } main();