-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunction-calling.ts
110 lines (96 loc) · 3.57 KB
/
function-calling.ts
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
import { Agent, AnthropicProvider } from '../';
import { Message } from '../core/types';
import dotenv from 'dotenv';
dotenv.config();
// Example functions that can be called by the agent
const functions = {
getWeather: async (location: string): Promise<string> => {
// Simulate weather API call
return `The weather in ${location} is sunny and 22°C`;
},
setReminder: async (time: string, message: string): Promise<string> => {
// Simulate setting a reminder
return `Reminder set for ${time}: ${message}`;
},
searchDatabase: async (query: string): Promise<string[]> => {
// Simulate database search
return [`Result 1 for "${query}"`, `Result 2 for "${query}"`];
}
};
async function main() {
// Initialize the agent
const agent = new Agent({
domain: 'function-calling',
userId: 'function-agent',
platform: 'cli',
capabilities: ['text-generation', 'function-calling'],
permissions: ['read', 'write', 'execute-functions']
});
// Set up AI provider with function definitions
const aiProvider = new AnthropicProvider({
apiKey: process.env.ANTHROPIC_API_KEY!,
model: 'claude-3-opus-20240229'
});
await agent.setAIProvider(aiProvider);
await agent.initialize();
// Example messages that require function calling
const messages: Message[] = [
{
role: 'user',
content: 'What\'s the weather like in London?',
metadata: {
timestamp: new Date().toISOString(),
platform: 'cli'
}
},
{
role: 'user',
content: 'Set a reminder for tomorrow at 9 AM to check emails',
metadata: {
timestamp: new Date().toISOString(),
platform: 'cli'
}
},
{
role: 'user',
content: 'Search the database for recent transactions',
metadata: {
timestamp: new Date().toISOString(),
platform: 'cli'
}
}
];
// Process messages and handle function calls
for (const message of messages) {
console.log('\nUser:', message.content);
const response = await agent.processMessage(message);
console.log('Assistant:', response.content);
// Handle function calls if present in response
if (response.metadata?.functionCall) {
const { name, arguments: args } = response.metadata.functionCall;
if (name in functions) {
try {
// @ts-ignore - Dynamic function call
const result = await functions[name](...Object.values(args));
console.log('Function Result:', result);
// Let the agent process the function result
const functionResponse = await agent.processMessage({
role: 'system',
content: `Function ${name} returned: ${JSON.stringify(result)}`,
metadata: {
timestamp: new Date().toISOString(),
platform: 'cli',
functionResult: true
}
});
console.log('Assistant:', functionResponse.content);
} catch (error) {
console.error(`Error executing function ${name}:`, error);
}
}
}
}
// Cleanup
await agent.shutdown();
}
main().catch(console.error);