forked from deepset-ai/haystack-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti-provider-setup.ts
More file actions
108 lines (90 loc) · 3.3 KB
/
Copy pathmulti-provider-setup.ts
File metadata and controls
108 lines (90 loc) · 3.3 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
/**
* Multi-Provider Setup Example
*
* Demonstrates setting up and using TealMultiProvider with multiple LLM providers
*/
import { TealMultiProvider, TealOpenAI, TealAnthropic, TealGemini, TealMistral } from 'tealtiger';
async function main() {
console.log('🌐 TealTiger Multi-Provider Setup Example\n');
// Initialize individual provider clients
const openai = new TealOpenAI({
apiKey: process.env.OPENAI_API_KEY || 'your-openai-key',
});
const anthropic = new TealAnthropic({
apiKey: process.env.ANTHROPIC_API_KEY || 'your-anthropic-key',
});
const gemini = new TealGemini({
apiKey: process.env.GOOGLE_API_KEY || 'your-google-key',
});
const mistral = new TealMistral({
apiKey: process.env.MISTRAL_API_KEY || 'your-mistral-key',
});
// Create multi-provider orchestrator with priority strategy
const multiProvider = new TealMultiProvider({
strategy: 'priority', // Use highest priority provider first
enableFailover: true, // Enable automatic failover
maxFailoverAttempts: 3, // Try up to 3 providers
});
// Register providers with priorities
console.log('📝 Registering providers...');
multiProvider.registerProvider({
type: 'openai',
name: 'openai-primary',
client: openai,
priority: 1, // Highest priority
enabled: true,
});
multiProvider.registerProvider({
type: 'anthropic',
name: 'anthropic-backup',
client: anthropic,
priority: 2, // Second priority
enabled: true,
});
multiProvider.registerProvider({
type: 'gemini',
name: 'gemini-backup',
client: gemini,
priority: 3, // Third priority
enabled: true,
});
multiProvider.registerProvider({
type: 'mistral',
name: 'mistral-eu',
client: mistral,
priority: 4, // Lowest priority
enabled: true,
useCases: ['european-data'], // Specific use case
});
// List registered providers
const providers = multiProvider.getProviders();
console.log(`\n✅ Registered ${providers.length} providers:`);
providers.forEach(p => {
console.log(` - ${p.name} (${p.type}) - Priority: ${p.priority}`);
});
// Display routing strategies
console.log('\n🔀 Available Routing Strategies:');
console.log(' 1. priority - Use highest priority provider');
console.log(' 2. round-robin - Rotate through providers');
console.log(' 3. cost - Use cheapest provider');
console.log(' 4. use-case - Route by specific use case');
console.log(' 5. custom - Custom routing function');
// Display failover configuration
const config = multiProvider.getConfig();
console.log('\n🔄 Failover Configuration:');
console.log(` Enabled: ${config.enableFailover}`);
console.log(` Max Attempts: ${config.maxFailoverAttempts}`);
console.log(` Strategy: ${config.strategy}`);
// Display metrics
const metrics = multiProvider.getMetrics();
console.log('\n📊 Metrics:');
console.log(` Total Requests: ${metrics.totalRequests}`);
console.log(` Total Cost: $${metrics.totalCost.toFixed(6)}`);
console.log('\n✨ Multi-Provider Benefits:');
console.log(' ✓ Automatic failover for high availability');
console.log(' ✓ Load balancing across providers');
console.log(' ✓ Cost optimization');
console.log(' ✓ Use-case specific routing');
console.log(' ✓ Unified metrics and monitoring');
}
main();