diff --git a/ai/ai-samples/src/features/structured-output/index.tsx b/ai/ai-samples/src/features/structured-output/index.tsx index 7542981dc..453e8ba95 100644 --- a/ai/ai-samples/src/features/structured-output/index.tsx +++ b/ai/ai-samples/src/features/structured-output/index.tsx @@ -1,9 +1,121 @@ -import React from 'react'; +import { useState, useEffect } from 'react'; +import { generateWithSDKSchema, generateWithEnumValues } from './service'; + +export default function StructuredOutputView() { + const [activeTab, setActiveTab] = useState<'json' | 'enum'>('json'); + const [prompt, setPrompt] = useState(''); + const [result, setResult] = useState(''); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); + useEffect(() => { + if (activeTab === 'json') { + setPrompt('Generate 3 animal-based characters for a card game.'); + } else { + setPrompt('The film aims to educate and inform viewers about real-life subjects, events, or people. It offers a factual record of a particular topic by combining interviews, historical footage, and narration.'); + } + setResult(''); + setError(null); + }, [activeTab]); + + const handleGenerate = async () => { + const cleanedPrompt = prompt.trim(); + if (!cleanedPrompt) return; + + setLoading(true); + setError(null); + setResult(''); + + try { + let output = ''; + if (activeTab === 'json') { + output = await generateWithSDKSchema(cleanedPrompt); + } else { + output = await generateWithEnumValues(cleanedPrompt); + } + setResult(output); + } catch (err: unknown) { + if (err instanceof Error) { + setError(err.message); + } else { + setError('An error occurred while generating structured output.'); + } + } finally { + setLoading(false); + } + }; -export default function Feature() { return ( -
-

structured-output

+
+

Structured Output Generation

+

+ Force Gemini to return strictly formatted data by passing a schema into the generation configuration. +

+ +
+ + +
+ +