-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path03-structured-output.ts
More file actions
75 lines (63 loc) · 2.47 KB
/
Copy path03-structured-output.ts
File metadata and controls
75 lines (63 loc) · 2.47 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
// Copyright (c) 2025 Agentspan
// Licensed under the MIT License. See LICENSE file in the project root for details.
/**
* OpenAI Agent with Structured Output -- enforced JSON schema response.
*
* Demonstrates:
* - Using outputType with a zod schema for structured responses
* - The agent is forced to return data matching the schema
* - Model settings (temperature) for deterministic output
*
* Requirements:
* - AGENTSPAN_SERVER_URL for the Agentspan path
*/
import { Agent, setTracingDisabled } from '@openai/agents';
import { z } from 'zod';
import { AgentRuntime } from '@io-orkes/conductor-javascript/agents';
setTracingDisabled(true);
// ── Structured output schema ────────────────────────────────────────
const MovieRecommendation = z.object({
title: z.string(),
year: z.number(),
genre: z.string(),
reason: z.string(),
});
const MovieList = z.object({
recommendations: z.array(MovieRecommendation),
theme: z.string(),
});
// ── Agent ───────────────────────────────────────────────────────────
export const agent = new Agent({
name: 'movie_recommender',
instructions:
'You are a movie recommendation expert. When asked for movie suggestions, ' +
'return a structured list of recommendations with title, year, genre, ' +
'and a brief reason for each recommendation. Identify the overall theme.',
model: 'gpt-4o-mini',
outputType: MovieList,
modelSettings: {
temperature: 0.3,
maxTokens: 1000,
},
});
const prompt = 'Recommend 3 sci-fi movies that explore the concept of artificial intelligence.';
// ── Run on agentspan ──────────────────────────────────────────────
async function main() {
const runtime = new AgentRuntime();
try {
const result = await runtime.run(agent, prompt);
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/openai --agents movie_recommender
//
// 2. In a separate long-lived worker process:
// await runtime.serve(agent);
} finally {
await runtime.shutdown();
}
}
main().catch(console.error);