-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
47 lines (40 loc) · 1.41 KB
/
index.js
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
import 'dotenv/config';
import { Swarm } from './src/swarm.js';
import { runDemoLoop } from './src/repl.js';
import Agent from './src/agent.js';
import { Result } from './src/types.js';
// Example usage
const client = new Swarm();
console.log('OPENAI_API_KEY:', process.env.OPENAI_API_KEY);
const salesAgent = new Agent({
name: 'SalesAgent',
instructions: 'You are a knowledgeable sales agent. Help the user with their purchase inquiries.',
functions: []
});
const greetAgent = new Agent({
name: 'GreetAgent',
instructions: 'You are a friendly greeting agent. Greet the user and ask how you can help.',
functions: [
{
name: 'transferToSales',
description: 'Transfer the conversation to the sales agent',
function: () => new Result({ value: 'Transferring to sales...', agent: salesAgent })
}
]
});
async function runDemo() {
try {
const response = await client.run({
agent: greetAgent,
messages: [{ role: 'user', content: 'Hello, I need help with a purchase, transfer me to the sales agent.' }],
contextVariables: { userName: 'John' },
debug: true
});
console.log('Final response:', response.messages[response.messages.length - 1].content);
console.log('Last agent:', response.agent.name);
console.log('Updated context variables:', response.contextVariables);
} catch (error) {
console.error('Error running Swarm:', error);
}
}
runDemo();