-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path03-structured-output.ts
More file actions
84 lines (72 loc) · 2.68 KB
/
Copy path03-structured-output.ts
File metadata and controls
84 lines (72 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Google ADK Agent with Structured Output -- enforced JSON schema response.
*
* Demonstrates:
* - Using outputSchema (Zod converted via zodObjectToSchema) for structured, validated responses
* - Generation config for controlling model behavior
* - The server normalizer maps ADK's outputSchema to AgentConfig.outputType
*
* Requirements:
* - npm install @google/adk zod
* - AGENTSPAN_SERVER_URL for agentspan path
*/
import { LlmAgent, zodObjectToSchema } from '@google/adk';
import { z } from 'zod';
import { AgentRuntime } from '@io-orkes/conductor-javascript/agents';
const model = process.env.AGENTSPAN_LLM_MODEL ?? 'gemini-2.5-flash';
// ── Output schemas ───────────────────────────────────────────────────
const IngredientSchema = z.object({
name: z.string(),
quantity: z.string(),
unit: z.string(),
});
const RecipeStepSchema = z.object({
step_number: z.number(),
instruction: z.string(),
duration_minutes: z.number(),
});
const RecipeSchema = z.object({
name: z.string(),
servings: z.number(),
prep_time_minutes: z.number(),
cook_time_minutes: z.number(),
ingredients: z.array(IngredientSchema),
steps: z.array(RecipeStepSchema),
difficulty: z.string(),
});
// ── Agent ────────────────────────────────────────────────────────────
export const agent = new LlmAgent({
name: 'recipe_generator',
model,
instruction:
'You are a professional chef assistant. When asked for a recipe, ' +
'provide a complete, well-structured recipe with precise measurements, ' +
'clear step-by-step instructions, and accurate timing.',
outputSchema: zodObjectToSchema(RecipeSchema),
generateContentConfig: {
temperature: 0.3,
},
});
// ── Run on agentspan ───────────────────────────────────────────────
async function main() {
const runtime = new AgentRuntime();
try {
const result = await runtime.run(
agent,
'Give me a recipe for classic Italian carbonara pasta.',
);
console.log('Status:', result.status);
result.printResult();
// Production pattern:
// 1. Deploy once during CI/CD:
// await runtime.deploy(agent);
// CLI alternative:
// agentspan deploy --package sdk/typescript/examples/adk --agents recipe_generator
//
// 2. In a separate long-lived worker process:
// await runtime.serve(agent);
} finally {
await runtime.shutdown();
}
}
main().catch(console.error);