-
Notifications
You must be signed in to change notification settings - Fork 945
Expand file tree
/
Copy pathagents-as-tools-structured.ts
More file actions
53 lines (44 loc) · 1.84 KB
/
Copy pathagents-as-tools-structured.ts
File metadata and controls
53 lines (44 loc) · 1.84 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
import { Agent, run } from '@openai/agents';
import { z } from 'zod';
const translator = new Agent({
name: 'translator',
instructions:
'Translate the input text into the target language. If the target is not clear, you can ask the user for clarification.',
});
const orchestrator = new Agent({
name: 'orchestrator',
instructions:
'You are a task dispatcher. Always call the tool with sufficient input for the task. Do not handle any tasks by yourself.',
tools: [
translator.asTool({
toolName: 'translate_text',
toolDescription:
'Translate text between languages. When you call this tool, you must provide the text to translate, the source language, and the target language.',
// Structured input parameters, filled by the model.
parameters: z.object({
text: z.string().describe('Text to translate.'),
source: z.string().describe('Source language code or name.'),
target: z.string().describe('Target language code or name.'),
}),
// By default, the input schema will be included in a simpler format.
// Set includeInputSchema to true to include the full JSON Schema.
// includeInputSchema: true,
// Build a custom prompt from structured input data.
// inputBuilder: async ({ params }) => {
// return `Translate the text "${params.text}" from ${params.source} to ${params.target}.`;
// },
}),
],
modelSettings: { toolChoice: 'required' },
});
async function main() {
const query = 'Translate "Hola" from Spanish to French.';
const response1 = await run(translator, query);
console.log('Translator agent direct run:', response1.finalOutput);
const response2 = await run(orchestrator, query);
console.log('Translator agent as tool:', response2.finalOutput);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});