-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathmulti-provider.ts
More file actions
160 lines (136 loc) · 4.63 KB
/
multi-provider.ts
File metadata and controls
160 lines (136 loc) · 4.63 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
/**
* cascadeflow - Multi-Provider Example (TypeScript/Node.js)
*
* Demonstrates using multiple AI providers in a cascade.
*
* This example shows:
* - Mixing different providers (OpenAI, Anthropic, Groq)
* - Provider-specific configurations
* - Cross-provider cascading
* - Cost optimization across providers
*
* Requirements:
* - Node.js 18+
* - @cascadeflow/core
* - openai, @anthropic-ai/sdk, groq-sdk (peer dependencies)
* - API keys for each provider
*
* Setup:
* npm install @cascadeflow/core openai @anthropic-ai/sdk groq-sdk
* export OPENAI_API_KEY="your-key"
* export ANTHROPIC_API_KEY="your-key"
* export GROQ_API_KEY="your-key"
* npx tsx multi-provider.ts
*/
import { CascadeAgent, ModelConfig } from '@cascadeflow/core';
async function main() {
console.log('='.repeat(80));
console.log('🌐 CASCADEFLOW - MULTI-PROVIDER EXAMPLE (TypeScript)');
console.log('='.repeat(80));
console.log();
// Check for API keys
const providers: { name: string; key: string }[] = [
{ name: 'OpenAI', key: 'OPENAI_API_KEY' },
{ name: 'Anthropic', key: 'ANTHROPIC_API_KEY' },
{ name: 'Groq', key: 'GROQ_API_KEY' },
];
const available: string[] = [];
const missing: string[] = [];
for (const provider of providers) {
if (process.env[provider.key]) {
available.push(provider.name);
} else {
missing.push(provider.name);
}
}
console.log('🔑 API Keys Status:');
available.forEach((p) => console.log(` ✅ ${p}`));
missing.forEach((p) => console.log(` ⏭️ ${p} (skipped - no API key)`));
console.log();
if (available.length === 0) {
console.error('❌ At least one API key is required');
process.exit(1);
}
// ========================================================================
// STEP 1: Configure Multi-Provider Cascade
// ========================================================================
console.log('📋 Step 1: Configuring multi-provider cascade...\n');
const models: ModelConfig[] = [];
// Groq: Ultra-fast, free tier available
if (process.env.GROQ_API_KEY) {
models.push({
name: 'llama-3.1-8b-instant',
provider: 'groq',
cost: 0.0, // FREE
apiKey: process.env.GROQ_API_KEY,
});
}
// OpenAI: Cheap model
if (process.env.OPENAI_API_KEY) {
models.push({
name: 'gpt-4o-mini',
provider: 'openai',
cost: 0.00015,
apiKey: process.env.OPENAI_API_KEY,
});
}
// Anthropic: Mid-tier
if (process.env.ANTHROPIC_API_KEY) {
models.push({
name: 'claude-haiku-4-5',
provider: 'anthropic',
cost: 0.001,
apiKey: process.env.ANTHROPIC_API_KEY,
});
}
// OpenAI: Premium model
if (process.env.OPENAI_API_KEY) {
models.push({
name: 'gpt-4o',
provider: 'openai',
cost: 0.00625,
apiKey: process.env.OPENAI_API_KEY,
});
}
const agent = new CascadeAgent({
models,
// Using default quality config (matches Python approach)
});
console.log(` ✅ Configured ${models.length}-tier cascade:`);
models.forEach((m, i) => {
console.log(` Tier ${i + 1}: ${m.name} (${m.provider}) - $${m.cost}/1K tokens`);
});
console.log();
// ========================================================================
// STEP 2: Test Queries
// ========================================================================
console.log('📝 Step 2: Testing cross-provider routing...\n');
const queries = [
'What is the capital of France?',
'Explain quantum computing in simple terms',
];
for (const query of queries) {
console.log('-'.repeat(80));
console.log(`❓ Question: ${query}`);
console.log();
const result = await agent.run(query);
console.log('✅ Result:');
console.log(` 🤖 Model: ${result.modelUsed}`);
console.log(` 🏢 Provider: ${models.find((m) => result.modelUsed.includes(m.name) || m.name.includes(result.modelUsed))?.provider || 'unknown'}`);
console.log(` 💰 Cost: $${result.totalCost.toFixed(6)}`);
console.log(` ⚡ Latency: ${result.latencyMs}ms`);
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. cascadeflow works seamlessly across providers');
console.log(' 2. You can mix OpenAI, Anthropic, Groq, and more');
console.log(' 3. Each provider has its own cost and quality profile');
console.log(' 4. TypeScript provides full type safety across all providers');
console.log();
}
main().catch(console.error);