-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathtool-calling.ts
More file actions
164 lines (142 loc) · 4.65 KB
/
tool-calling.ts
File metadata and controls
164 lines (142 loc) · 4.65 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
/**
* cascadeflow - Tool Calling Example (TypeScript/Node.js)
*
* Demonstrates how to use function/tool calling with cascadeflow.
*
* This example shows:
* - Defining tools with TypeScript types
* - Passing tools to the cascade agent
* - How cascadeflow handles tool calls across tiers
* - Type-safe tool definitions
*
* Requirements:
* - Node.js 18+
* - @cascadeflow/core
* - openai (peer dependency)
* - OpenAI API key
*
* Setup:
* npm install @cascadeflow/core openai
* export OPENAI_API_KEY="your-key-here"
* npx tsx tool-calling.ts
*/
import { CascadeAgent, ModelConfig } from '@cascadeflow/core';
async function main() {
console.log('='.repeat(80));
console.log('🔧 CASCADEFLOW - TOOL CALLING EXAMPLE (TypeScript)');
console.log('='.repeat(80));
console.log();
if (!process.env.OPENAI_API_KEY) {
console.error('❌ Set OPENAI_API_KEY first: export OPENAI_API_KEY="sk-..."');
process.exit(1);
}
// ========================================================================
// STEP 1: Define Tools
// ========================================================================
console.log('🔧 Step 1: Defining tools...\n');
const tools = [
{
type: 'function' as const,
function: {
name: 'get_weather',
description: 'Get the current weather for a location',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'City name, e.g., "San Francisco"',
},
unit: {
type: 'string',
enum: ['celsius', 'fahrenheit'],
description: 'Temperature unit',
},
},
required: ['location'],
},
},
},
{
type: 'function' as const,
function: {
name: 'calculate',
description: 'Perform a mathematical calculation',
parameters: {
type: 'object',
properties: {
expression: {
type: 'string',
description: 'Mathematical expression to evaluate, e.g., "2 + 2"',
},
},
required: ['expression'],
},
},
},
];
console.log(' ✅ Defined 2 tools: get_weather, calculate');
console.log();
// ========================================================================
// STEP 2: Configure Cascade with Tools
// ========================================================================
console.log('📋 Step 2: Configuring cascade with tools support...\n');
const models: ModelConfig[] = [
{
name: 'gpt-4o-mini',
provider: 'openai',
cost: 0.00015,
apiKey: process.env.OPENAI_API_KEY,
supportsTools: true,
},
{
name: 'gpt-4o',
provider: 'openai',
cost: 0.00625,
apiKey: process.env.OPENAI_API_KEY,
supportsTools: true,
},
];
const agent = new CascadeAgent({ models });
console.log(' ✅ Cascade configured with tool calling enabled');
console.log();
// ========================================================================
// STEP 3: Test Tool Calling
// ========================================================================
console.log('📝 Step 3: Testing queries that require tools...\n');
const queries = [
"What's the weather in San Francisco?",
'Calculate 42 * 1337',
"What's the weather in London and Paris?",
];
for (const query of queries) {
console.log('-'.repeat(80));
console.log(`❓ Question: ${query}`);
console.log();
const result = await agent.run(query, { tools });
console.log('✅ Result:');
console.log(` 🤖 Model: ${result.modelUsed}`);
console.log(` 💰 Cost: $${result.totalCost.toFixed(6)}`);
if (result.toolCalls && result.toolCalls.length > 0) {
console.log(` 🔧 Tool Calls: ${result.toolCalls.length}`);
result.toolCalls.forEach((call, i) => {
console.log(` ${i + 1}. ${call.function.name}(${call.function.arguments})`);
});
} else {
console.log(' 🔧 Tool Calls: None (answered directly)');
}
console.log(` 📝 Response: ${result.content.substring(0, 100)}...`);
console.log();
}
console.log('='.repeat(80));
console.log('🎯 KEY TAKEAWAYS');
console.log('='.repeat(80));
console.log();
console.log('✅ What You Learned:');
console.log(' 1. How to define tools with TypeScript types');
console.log(' 2. How to pass tools to cascadeflow');
console.log(' 3. Tool calls are automatically handled across cascade tiers');
console.log(' 4. Full type safety for tool definitions');
console.log();
}
main().catch(console.error);