-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathagentic-multi-agent.ts
More file actions
200 lines (180 loc) · 6.1 KB
/
agentic-multi-agent.ts
File metadata and controls
200 lines (180 loc) · 6.1 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* Agentic + Multi-Agent Example
*
* Demonstrates:
* - Multi-turn tool loop (persist assistant tool_calls + tool results)
* - Multi-agent orchestration (agent-as-a-tool delegation)
* - Tool-capable model filtering + tool cascade routing
*
* Usage:
* export OPENAI_API_KEY="sk-..."
* npx tsx examples/nodejs/agentic-multi-agent.ts
*/
import {
CascadeAgent,
ToolCall,
ToolConfig,
ToolExecutor,
type Message,
type Tool,
} from '@cascadeflow/core';
import { safeCalculateExpression } from './safe-math';
function safeCalculate(expression: string): { expression: string; result?: number; error?: string } {
try {
const result = safeCalculateExpression(expression);
return { expression, result };
} catch (e) {
return { expression, error: e instanceof Error ? e.message : String(e) };
}
}
async function runToolLoop(params: {
agent: CascadeAgent;
messages: Message[];
tools: Tool[];
executor: ToolExecutor;
maxTurns?: number;
}): Promise<{ final: Message[] }> {
const { agent, tools, executor } = params;
const maxTurns = params.maxTurns ?? 6;
const messages: Message[] = [...params.messages];
for (let turn = 0; turn < maxTurns; turn++) {
const result = await agent.run(messages, { tools, maxTokens: 600 });
console.log(`\n[turn=${turn + 1}] model=${result.modelUsed} cost=$${result.totalCost.toFixed(6)}`);
const assistantMsg: Message = { role: 'assistant', content: result.content ?? '' };
if (result.toolCalls && result.toolCalls.length > 0) {
assistantMsg.tool_calls = result.toolCalls;
console.log(` tool_calls=${result.toolCalls.length}`);
}
messages.push(assistantMsg);
if (!result.toolCalls || result.toolCalls.length === 0) {
console.log('\nFinal answer:\n');
console.log(result.content);
return { final: messages };
}
for (const raw of result.toolCalls) {
const call = ToolCall.fromOpenAI(raw as any);
const toolResult = await executor.execute(call);
messages.push({
role: 'tool',
tool_call_id: call.id,
content: JSON.stringify(toolResult.success ? toolResult.result : { error: toolResult.error }),
});
}
}
throw new Error(`Tool loop exceeded maxTurns=${maxTurns}`);
}
async function main() {
console.log('\n' + '='.repeat(80));
console.log('🤖 CASCADEFLOW - AGENTIC + MULTI-AGENT EXAMPLE (TypeScript)');
console.log('='.repeat(80) + '\n');
if (!process.env.OPENAI_API_KEY) {
console.error('❌ Set OPENAI_API_KEY first: export OPENAI_API_KEY="sk-..."');
process.exit(1);
}
const researchAgent = new CascadeAgent({
models: [
{ name: 'gpt-4o-mini', provider: 'openai', cost: 0.00015 },
{ name: 'gpt-4o', provider: 'openai', cost: 0.00625 },
],
quality: { threshold: 0.7 },
});
const mainAgent = new CascadeAgent({
models: [
{ name: 'gpt-4o-mini', provider: 'openai', cost: 0.00015, supportsTools: true },
{ name: 'gpt-4o', provider: 'openai', cost: 0.00625, supportsTools: true },
],
quality: { threshold: 0.7 },
});
// Tool implementations (what actually runs in your app).
const toolConfigs = [
new ToolConfig({
name: 'calculate',
description: 'Perform a mathematical calculation (supports sqrt(), pow(), abs())',
parameters: {
type: 'object',
properties: { expression: { type: 'string', description: 'Math expression' } },
required: ['expression'],
},
function: async ({ expression }: { expression: string }) => safeCalculate(expression),
}),
new ToolConfig({
name: 'search_web',
description: 'Search the web (stub example that returns fake results)',
parameters: {
type: 'object',
properties: {
query: { type: 'string', description: 'Search query' },
num_results: { type: 'number', description: 'Number of results (1-5)' },
},
required: ['query'],
},
function: async ({ query, num_results }: { query: string; num_results?: number }) => {
const n = Math.max(1, Math.min(5, num_results ?? 3));
return {
query,
results: Array.from({ length: n }).map((_, i) => ({
title: `Result ${i + 1} for "${query}"`,
url: `https://example.com/${i + 1}`,
snippet: `Stub snippet about ${query}`,
})),
};
},
}),
new ToolConfig({
name: 'delegate_to_researcher',
description: 'Ask the research agent for a focused explanation or summary',
parameters: {
type: 'object',
properties: { question: { type: 'string', description: 'Research question' } },
required: ['question'],
},
function: async ({ question }: { question: string }) => {
const res = await researchAgent.run(
[
{ role: 'system', content: 'You are a concise research assistant. Answer in 2-4 sentences.' },
{ role: 'user', content: question },
],
{ maxTokens: 250 }
);
return { answer: res.content, model: res.modelUsed, cost: res.totalCost };
},
}),
];
const executor = new ToolExecutor(toolConfigs);
// Tool schemas (what you send to the model).
const tools: Tool[] = toolConfigs.map((t) => ({
type: 'function',
function: {
name: t.name,
description: t.description,
parameters: t.parameters,
},
}));
const messages: Message[] = [
{
role: 'system',
content:
'You are an agent. Use tools when they help.\n' +
'- Use calculate for any arithmetic.\n' +
'- Use delegate_to_researcher for explanations you are unsure about.\n' +
'When you have enough information, answer clearly and briefly.',
},
{
role: 'user',
content:
'Compute sqrt(144) * 5 using the calculate tool, then ask the researcher to explain why the result is correct. ' +
'Return the final answer with the calculation and the explanation.',
},
];
await runToolLoop({
agent: mainAgent,
messages,
tools,
executor,
maxTurns: 6,
});
}
main().catch((e) => {
console.error(e);
process.exit(1);
});