@sparkleideas/providers 3.5.2-patch.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +574 -0
- package/package.json +70 -0
- package/src/__tests__/provider-integration.test.ts +446 -0
- package/src/__tests__/quick-test.ts +356 -0
- package/src/anthropic-provider.ts +435 -0
- package/src/base-provider.ts +596 -0
- package/src/cohere-provider.ts +423 -0
- package/src/google-provider.ts +429 -0
- package/src/index.ts +40 -0
- package/src/ollama-provider.ts +408 -0
- package/src/openai-provider.ts +490 -0
- package/src/provider-manager.ts +538 -0
- package/src/ruvector-provider.ts +721 -0
- package/src/types.ts +435 -0
package/README.md
ADDED
|
@@ -0,0 +1,574 @@
|
|
|
1
|
+
# @claude-flow/providers
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@claude-flow/providers)
|
|
4
|
+
[](https://www.npmjs.com/package/@claude-flow/providers)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
[](https://www.typescriptlang.org/)
|
|
7
|
+
[](https://github.com/ruvnet/claude-flow)
|
|
8
|
+
|
|
9
|
+
> Multi-LLM Provider System for Claude Flow V3 - unified interface for Anthropic, OpenAI, Google, Cohere, Ollama, and RuVector with intelligent load balancing, automatic failover, and cost optimization.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **6+ LLM Providers** - Anthropic, OpenAI, Google, Cohere, Ollama, RuVector
|
|
14
|
+
- **Load Balancing** - Round-robin, latency-based, least-loaded, cost-based strategies
|
|
15
|
+
- **Automatic Failover** - Seamless provider switching on failures
|
|
16
|
+
- **Request Caching** - LRU cache with configurable TTL
|
|
17
|
+
- **Cost Optimization** - Up to 85%+ savings with intelligent routing
|
|
18
|
+
- **Streaming Support** - Full streaming for all providers
|
|
19
|
+
- **Tool Calling** - Unified tool/function calling interface
|
|
20
|
+
- **Health Monitoring** - Real-time provider health checks
|
|
21
|
+
- **Cost Tracking** - Per-request and aggregate cost metrics
|
|
22
|
+
|
|
23
|
+
## Supported Providers & Models
|
|
24
|
+
|
|
25
|
+
### Anthropic (Claude)
|
|
26
|
+
- `claude-3-5-sonnet-20241022`, `claude-3-5-sonnet-latest`
|
|
27
|
+
- `claude-3-opus-20240229`
|
|
28
|
+
- `claude-3-sonnet-20240229`
|
|
29
|
+
- `claude-3-haiku-20240307`
|
|
30
|
+
|
|
31
|
+
### OpenAI (GPT)
|
|
32
|
+
- `gpt-4o`, `gpt-4o-mini`
|
|
33
|
+
- `gpt-4-turbo`, `gpt-4`
|
|
34
|
+
- `gpt-3.5-turbo`
|
|
35
|
+
- `o1-preview`, `o1-mini`, `o3-mini`
|
|
36
|
+
|
|
37
|
+
### Google (Gemini)
|
|
38
|
+
- `gemini-2.0-flash`
|
|
39
|
+
- `gemini-1.5-pro`, `gemini-1.5-flash`
|
|
40
|
+
- `gemini-pro`
|
|
41
|
+
|
|
42
|
+
### Cohere
|
|
43
|
+
- `command-r-plus`, `command-r`
|
|
44
|
+
- `command-light`, `command`
|
|
45
|
+
|
|
46
|
+
### Ollama (Local)
|
|
47
|
+
- `llama3.2`, `llama3.1`
|
|
48
|
+
- `mistral`, `mixtral`
|
|
49
|
+
- `codellama`, `phi-4`
|
|
50
|
+
- `deepseek-coder`
|
|
51
|
+
|
|
52
|
+
### RuVector
|
|
53
|
+
- Custom models via @ruvector/ruvllm
|
|
54
|
+
|
|
55
|
+
## Installation
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
npm install @claude-flow/providers
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Quick Start
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
import { createProviderManager } from '@claude-flow/providers';
|
|
65
|
+
|
|
66
|
+
// Create provider manager with multiple providers
|
|
67
|
+
const manager = await createProviderManager({
|
|
68
|
+
providers: [
|
|
69
|
+
{
|
|
70
|
+
provider: 'anthropic',
|
|
71
|
+
apiKey: process.env.ANTHROPIC_API_KEY!,
|
|
72
|
+
model: 'claude-3-5-sonnet-latest',
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
provider: 'openai',
|
|
76
|
+
apiKey: process.env.OPENAI_API_KEY!,
|
|
77
|
+
model: 'gpt-4o',
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
loadBalancing: {
|
|
81
|
+
enabled: true,
|
|
82
|
+
strategy: 'cost-based',
|
|
83
|
+
},
|
|
84
|
+
fallback: {
|
|
85
|
+
enabled: true,
|
|
86
|
+
maxAttempts: 2,
|
|
87
|
+
},
|
|
88
|
+
cache: {
|
|
89
|
+
enabled: true,
|
|
90
|
+
ttl: 300000,
|
|
91
|
+
maxSize: 1000,
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// Make a completion request
|
|
96
|
+
const response = await manager.complete({
|
|
97
|
+
messages: [
|
|
98
|
+
{ role: 'user', content: 'Hello, how are you?' }
|
|
99
|
+
],
|
|
100
|
+
maxTokens: 100,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
console.log('Response:', response.content);
|
|
104
|
+
console.log('Provider:', response.provider);
|
|
105
|
+
console.log('Cost:', response.cost?.totalCost);
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## API Reference
|
|
109
|
+
|
|
110
|
+
### ProviderManager
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
import { ProviderManager, createProviderManager } from '@claude-flow/providers';
|
|
114
|
+
|
|
115
|
+
// Create and initialize
|
|
116
|
+
const manager = await createProviderManager(config);
|
|
117
|
+
|
|
118
|
+
// Or manually initialize
|
|
119
|
+
const manager = new ProviderManager(config);
|
|
120
|
+
await manager.initialize();
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
#### Methods
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
// Complete a request
|
|
127
|
+
const response = await manager.complete(request, preferredProvider?);
|
|
128
|
+
|
|
129
|
+
// Stream completion
|
|
130
|
+
for await (const event of manager.streamComplete(request, preferredProvider?)) {
|
|
131
|
+
if (event.type === 'content') {
|
|
132
|
+
process.stdout.write(event.delta?.content || '');
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Health check all providers
|
|
137
|
+
const health = await manager.healthCheck();
|
|
138
|
+
health.forEach((result, provider) => {
|
|
139
|
+
console.log(`${provider}: ${result.healthy ? 'OK' : 'FAIL'}`);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// Estimate costs across providers
|
|
143
|
+
const estimates = await manager.estimateCost(request);
|
|
144
|
+
estimates.forEach((estimate, provider) => {
|
|
145
|
+
console.log(`${provider}: $${estimate.estimatedCost.total.toFixed(4)}`);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// Get aggregated usage
|
|
149
|
+
const usage = await manager.getUsage('day');
|
|
150
|
+
console.log(`Total cost: $${usage.cost.total}`);
|
|
151
|
+
|
|
152
|
+
// Get provider metrics
|
|
153
|
+
const metrics = manager.getMetrics();
|
|
154
|
+
metrics.forEach((m, provider) => {
|
|
155
|
+
console.log(`${provider}: ${m.latency}ms avg, ${m.errorRate * 100}% errors`);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// List available providers
|
|
159
|
+
const providers = manager.listProviders();
|
|
160
|
+
|
|
161
|
+
// Get specific provider
|
|
162
|
+
const anthropic = manager.getProvider('anthropic');
|
|
163
|
+
|
|
164
|
+
// Clear cache
|
|
165
|
+
manager.clearCache();
|
|
166
|
+
|
|
167
|
+
// Shutdown
|
|
168
|
+
manager.destroy();
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Individual Providers
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
import {
|
|
175
|
+
AnthropicProvider,
|
|
176
|
+
OpenAIProvider,
|
|
177
|
+
GoogleProvider,
|
|
178
|
+
CohereProvider,
|
|
179
|
+
OllamaProvider,
|
|
180
|
+
RuVectorProvider,
|
|
181
|
+
} from '@claude-flow/providers';
|
|
182
|
+
|
|
183
|
+
// Create provider directly
|
|
184
|
+
const anthropic = new AnthropicProvider({
|
|
185
|
+
config: {
|
|
186
|
+
provider: 'anthropic',
|
|
187
|
+
apiKey: process.env.ANTHROPIC_API_KEY!,
|
|
188
|
+
model: 'claude-3-5-sonnet-latest',
|
|
189
|
+
temperature: 0.7,
|
|
190
|
+
maxTokens: 1000,
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
await anthropic.initialize();
|
|
195
|
+
|
|
196
|
+
const response = await anthropic.complete({
|
|
197
|
+
messages: [{ role: 'user', content: 'Hello!' }],
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// Stream response
|
|
201
|
+
for await (const event of anthropic.streamComplete({
|
|
202
|
+
messages: [{ role: 'user', content: 'Tell me a story' }],
|
|
203
|
+
})) {
|
|
204
|
+
if (event.type === 'content') {
|
|
205
|
+
process.stdout.write(event.delta?.content || '');
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### Load Balancing Strategies
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
const manager = await createProviderManager({
|
|
214
|
+
providers: [...],
|
|
215
|
+
loadBalancing: {
|
|
216
|
+
enabled: true,
|
|
217
|
+
strategy: 'round-robin', // or 'least-loaded', 'latency-based', 'cost-based'
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
| Strategy | Description | Best For |
|
|
223
|
+
|----------|-------------|----------|
|
|
224
|
+
| `round-robin` | Rotate through providers | Even distribution |
|
|
225
|
+
| `least-loaded` | Use provider with lowest load | High throughput |
|
|
226
|
+
| `latency-based` | Use fastest provider | Low latency |
|
|
227
|
+
| `cost-based` | Use cheapest provider | Cost optimization |
|
|
228
|
+
|
|
229
|
+
### Automatic Failover
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
const manager = await createProviderManager({
|
|
233
|
+
providers: [...],
|
|
234
|
+
fallback: {
|
|
235
|
+
enabled: true,
|
|
236
|
+
maxAttempts: 3, // Try up to 3 providers
|
|
237
|
+
},
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
// Events for monitoring
|
|
241
|
+
manager.on('fallback_success', ({ originalProvider, fallbackProvider, attempts }) => {
|
|
242
|
+
console.log(`Fallback from ${originalProvider} to ${fallbackProvider}`);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
manager.on('fallback_exhausted', ({ originalProvider, attempts }) => {
|
|
246
|
+
console.error(`All ${attempts} fallback attempts failed`);
|
|
247
|
+
});
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### Request Caching
|
|
251
|
+
|
|
252
|
+
```typescript
|
|
253
|
+
const manager = await createProviderManager({
|
|
254
|
+
providers: [...],
|
|
255
|
+
cache: {
|
|
256
|
+
enabled: true,
|
|
257
|
+
ttl: 300000, // 5 minutes
|
|
258
|
+
maxSize: 1000, // Max cached responses
|
|
259
|
+
},
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
// Cache is keyed by: messages + model + temperature + maxTokens
|
|
263
|
+
// Identical requests return cached responses
|
|
264
|
+
|
|
265
|
+
// Clear cache when needed
|
|
266
|
+
manager.clearCache();
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
### Cost Optimization
|
|
270
|
+
|
|
271
|
+
```typescript
|
|
272
|
+
const manager = await createProviderManager({
|
|
273
|
+
providers: [...],
|
|
274
|
+
costOptimization: {
|
|
275
|
+
enabled: true,
|
|
276
|
+
maxCostPerRequest: 0.10, // Max $0.10 per request
|
|
277
|
+
},
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
// Request with cost constraints
|
|
281
|
+
const response = await manager.complete({
|
|
282
|
+
messages: [...],
|
|
283
|
+
costConstraints: {
|
|
284
|
+
maxCost: 0.05,
|
|
285
|
+
preferredModels: ['gpt-4o-mini', 'claude-3-haiku-20240307'],
|
|
286
|
+
},
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
// Get cost breakdown
|
|
290
|
+
console.log('Cost:', {
|
|
291
|
+
prompt: response.cost?.promptCost,
|
|
292
|
+
completion: response.cost?.completionCost,
|
|
293
|
+
total: response.cost?.totalCost,
|
|
294
|
+
currency: response.cost?.currency,
|
|
295
|
+
});
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
### Tool Calling
|
|
299
|
+
|
|
300
|
+
```typescript
|
|
301
|
+
const response = await manager.complete({
|
|
302
|
+
messages: [
|
|
303
|
+
{ role: 'user', content: 'What is the weather in San Francisco?' }
|
|
304
|
+
],
|
|
305
|
+
tools: [
|
|
306
|
+
{
|
|
307
|
+
type: 'function',
|
|
308
|
+
function: {
|
|
309
|
+
name: 'get_weather',
|
|
310
|
+
description: 'Get weather for a location',
|
|
311
|
+
parameters: {
|
|
312
|
+
type: 'object',
|
|
313
|
+
properties: {
|
|
314
|
+
location: { type: 'string', description: 'City name' },
|
|
315
|
+
unit: { type: 'string', enum: ['celsius', 'fahrenheit'] },
|
|
316
|
+
},
|
|
317
|
+
required: ['location'],
|
|
318
|
+
},
|
|
319
|
+
},
|
|
320
|
+
},
|
|
321
|
+
],
|
|
322
|
+
toolChoice: 'auto',
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
// Check for tool calls
|
|
326
|
+
if (response.toolCalls) {
|
|
327
|
+
for (const call of response.toolCalls) {
|
|
328
|
+
console.log('Tool:', call.function.name);
|
|
329
|
+
console.log('Args:', JSON.parse(call.function.arguments));
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
### Streaming
|
|
335
|
+
|
|
336
|
+
```typescript
|
|
337
|
+
// Async iterator streaming
|
|
338
|
+
for await (const event of manager.streamComplete(request)) {
|
|
339
|
+
switch (event.type) {
|
|
340
|
+
case 'content':
|
|
341
|
+
process.stdout.write(event.delta?.content || '');
|
|
342
|
+
break;
|
|
343
|
+
case 'tool_call':
|
|
344
|
+
console.log('Tool call:', event.delta?.toolCall);
|
|
345
|
+
break;
|
|
346
|
+
case 'error':
|
|
347
|
+
console.error('Error:', event.error);
|
|
348
|
+
break;
|
|
349
|
+
case 'done':
|
|
350
|
+
console.log('\nUsage:', event.usage);
|
|
351
|
+
console.log('Cost:', event.cost);
|
|
352
|
+
break;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
### Multimodal (Vision/Audio)
|
|
358
|
+
|
|
359
|
+
```typescript
|
|
360
|
+
// Image input (OpenAI, Anthropic, Google)
|
|
361
|
+
const response = await manager.complete({
|
|
362
|
+
messages: [
|
|
363
|
+
{
|
|
364
|
+
role: 'user',
|
|
365
|
+
content: [
|
|
366
|
+
{ type: 'text', text: 'What is in this image?' },
|
|
367
|
+
{ type: 'image', imageUrl: 'https://example.com/image.jpg' },
|
|
368
|
+
// or imageBase64: 'base64-encoded-image'
|
|
369
|
+
],
|
|
370
|
+
},
|
|
371
|
+
],
|
|
372
|
+
});
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
## TypeScript Types
|
|
376
|
+
|
|
377
|
+
```typescript
|
|
378
|
+
import type {
|
|
379
|
+
// Provider types
|
|
380
|
+
LLMProvider,
|
|
381
|
+
LLMModel,
|
|
382
|
+
LLMProviderConfig,
|
|
383
|
+
ILLMProvider,
|
|
384
|
+
ProviderCapabilities,
|
|
385
|
+
|
|
386
|
+
// Message types
|
|
387
|
+
LLMMessage,
|
|
388
|
+
LLMContentPart,
|
|
389
|
+
LLMToolCall,
|
|
390
|
+
LLMTool,
|
|
391
|
+
|
|
392
|
+
// Request/Response
|
|
393
|
+
LLMRequest,
|
|
394
|
+
LLMResponse,
|
|
395
|
+
LLMStreamEvent,
|
|
396
|
+
|
|
397
|
+
// Manager types
|
|
398
|
+
ProviderManagerConfig,
|
|
399
|
+
LoadBalancingStrategy,
|
|
400
|
+
|
|
401
|
+
// Status types
|
|
402
|
+
HealthCheckResult,
|
|
403
|
+
ProviderStatus,
|
|
404
|
+
CostEstimate,
|
|
405
|
+
UsageStats,
|
|
406
|
+
|
|
407
|
+
// Error types
|
|
408
|
+
LLMProviderError,
|
|
409
|
+
RateLimitError,
|
|
410
|
+
AuthenticationError,
|
|
411
|
+
ModelNotFoundError,
|
|
412
|
+
ProviderUnavailableError,
|
|
413
|
+
} from '@claude-flow/providers';
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
## Error Handling
|
|
417
|
+
|
|
418
|
+
```typescript
|
|
419
|
+
import {
|
|
420
|
+
LLMProviderError,
|
|
421
|
+
RateLimitError,
|
|
422
|
+
AuthenticationError,
|
|
423
|
+
isLLMProviderError,
|
|
424
|
+
isRateLimitError,
|
|
425
|
+
} from '@claude-flow/providers';
|
|
426
|
+
|
|
427
|
+
try {
|
|
428
|
+
const response = await manager.complete(request);
|
|
429
|
+
} catch (error) {
|
|
430
|
+
if (isRateLimitError(error)) {
|
|
431
|
+
console.log(`Rate limited. Retry after: ${error.retryAfter}ms`);
|
|
432
|
+
} else if (isLLMProviderError(error)) {
|
|
433
|
+
console.log(`Provider error: ${error.code}`);
|
|
434
|
+
console.log(`Retryable: ${error.retryable}`);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
### Error Types
|
|
440
|
+
|
|
441
|
+
| Error | Code | HTTP | Retryable |
|
|
442
|
+
|-------|------|------|-----------|
|
|
443
|
+
| `RateLimitError` | `RATE_LIMIT` | 429 | Yes |
|
|
444
|
+
| `AuthenticationError` | `AUTHENTICATION` | 401 | No |
|
|
445
|
+
| `ModelNotFoundError` | `MODEL_NOT_FOUND` | 404 | No |
|
|
446
|
+
| `ProviderUnavailableError` | `PROVIDER_UNAVAILABLE` | 503 | Yes |
|
|
447
|
+
|
|
448
|
+
## Environment Variables
|
|
449
|
+
|
|
450
|
+
```bash
|
|
451
|
+
# API Keys
|
|
452
|
+
ANTHROPIC_API_KEY=sk-ant-...
|
|
453
|
+
OPENAI_API_KEY=sk-...
|
|
454
|
+
GOOGLE_API_KEY=...
|
|
455
|
+
COHERE_API_KEY=...
|
|
456
|
+
|
|
457
|
+
# Ollama (local)
|
|
458
|
+
OLLAMA_BASE_URL=http://localhost:11434
|
|
459
|
+
|
|
460
|
+
# Optional overrides
|
|
461
|
+
ANTHROPIC_BASE_URL=https://api.anthropic.com
|
|
462
|
+
OPENAI_BASE_URL=https://api.openai.com/v1
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
## Provider Configuration
|
|
466
|
+
|
|
467
|
+
### Anthropic
|
|
468
|
+
|
|
469
|
+
```typescript
|
|
470
|
+
{
|
|
471
|
+
provider: 'anthropic',
|
|
472
|
+
apiKey: process.env.ANTHROPIC_API_KEY!,
|
|
473
|
+
model: 'claude-3-5-sonnet-latest',
|
|
474
|
+
temperature: 0.7,
|
|
475
|
+
maxTokens: 4096,
|
|
476
|
+
timeout: 60000,
|
|
477
|
+
retryAttempts: 3,
|
|
478
|
+
}
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
### OpenAI
|
|
482
|
+
|
|
483
|
+
```typescript
|
|
484
|
+
{
|
|
485
|
+
provider: 'openai',
|
|
486
|
+
apiKey: process.env.OPENAI_API_KEY!,
|
|
487
|
+
model: 'gpt-4o',
|
|
488
|
+
temperature: 0.7,
|
|
489
|
+
maxTokens: 4096,
|
|
490
|
+
frequencyPenalty: 0,
|
|
491
|
+
presencePenalty: 0,
|
|
492
|
+
}
|
|
493
|
+
```
|
|
494
|
+
|
|
495
|
+
### Google
|
|
496
|
+
|
|
497
|
+
```typescript
|
|
498
|
+
{
|
|
499
|
+
provider: 'google',
|
|
500
|
+
apiKey: process.env.GOOGLE_API_KEY!,
|
|
501
|
+
model: 'gemini-1.5-pro',
|
|
502
|
+
temperature: 0.7,
|
|
503
|
+
maxTokens: 8192,
|
|
504
|
+
topK: 40,
|
|
505
|
+
}
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
### Cohere
|
|
509
|
+
|
|
510
|
+
```typescript
|
|
511
|
+
{
|
|
512
|
+
provider: 'cohere',
|
|
513
|
+
apiKey: process.env.COHERE_API_KEY!,
|
|
514
|
+
model: 'command-r-plus',
|
|
515
|
+
temperature: 0.7,
|
|
516
|
+
maxTokens: 4000,
|
|
517
|
+
}
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
### Ollama (Local)
|
|
521
|
+
|
|
522
|
+
```typescript
|
|
523
|
+
{
|
|
524
|
+
provider: 'ollama',
|
|
525
|
+
apiUrl: 'http://localhost:11434',
|
|
526
|
+
model: 'llama3.2',
|
|
527
|
+
temperature: 0.7,
|
|
528
|
+
maxTokens: 4096,
|
|
529
|
+
}
|
|
530
|
+
```
|
|
531
|
+
|
|
532
|
+
## Performance
|
|
533
|
+
|
|
534
|
+
| Metric | Target |
|
|
535
|
+
|--------|--------|
|
|
536
|
+
| Provider initialization | <500ms |
|
|
537
|
+
| Request routing | <5ms |
|
|
538
|
+
| Cache lookup | <1ms |
|
|
539
|
+
| Health check | <2s |
|
|
540
|
+
|
|
541
|
+
## Integration with Claude Flow
|
|
542
|
+
|
|
543
|
+
```typescript
|
|
544
|
+
import { createProviderManager } from '@claude-flow/providers';
|
|
545
|
+
import { createUnifiedSwarmCoordinator } from '@claude-flow/swarm';
|
|
546
|
+
|
|
547
|
+
// Create provider manager
|
|
548
|
+
const providers = await createProviderManager({
|
|
549
|
+
providers: [
|
|
550
|
+
{ provider: 'anthropic', apiKey: '...', model: 'claude-3-5-sonnet-latest' },
|
|
551
|
+
{ provider: 'openai', apiKey: '...', model: 'gpt-4o' },
|
|
552
|
+
],
|
|
553
|
+
loadBalancing: { enabled: true, strategy: 'cost-based' },
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
// Use with swarm coordinator
|
|
557
|
+
const coordinator = createUnifiedSwarmCoordinator({
|
|
558
|
+
// Agents can use provider manager for LLM calls
|
|
559
|
+
extensions: {
|
|
560
|
+
providers,
|
|
561
|
+
},
|
|
562
|
+
});
|
|
563
|
+
```
|
|
564
|
+
|
|
565
|
+
## Related Packages
|
|
566
|
+
|
|
567
|
+
- [@claude-flow/embeddings](../embeddings) - Embedding generation
|
|
568
|
+
- [@claude-flow/memory](../memory) - Vector storage and retrieval
|
|
569
|
+
- [@claude-flow/swarm](../swarm) - Multi-agent coordination
|
|
570
|
+
- [@claude-flow/neural](../neural) - SONA learning integration
|
|
571
|
+
|
|
572
|
+
## License
|
|
573
|
+
|
|
574
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sparkleideas/providers",
|
|
3
|
+
"version": "3.5.2-patch.1",
|
|
4
|
+
"description": "Multi-LLM Provider System for Claude Flow V3",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.js",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc",
|
|
21
|
+
"dev": "tsc --watch",
|
|
22
|
+
"test": "vitest run",
|
|
23
|
+
"test:watch": "vitest",
|
|
24
|
+
"test:integration": "vitest run src/__tests__/provider-integration.test.ts",
|
|
25
|
+
"test:coverage": "vitest run --coverage",
|
|
26
|
+
"test:quick": "npx tsx src/__tests__/quick-test.ts",
|
|
27
|
+
"clean": "rm -rf dist",
|
|
28
|
+
"typecheck": "tsc --noEmit"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"claude-flow",
|
|
32
|
+
"llm",
|
|
33
|
+
"provider",
|
|
34
|
+
"anthropic",
|
|
35
|
+
"openai",
|
|
36
|
+
"google",
|
|
37
|
+
"cohere",
|
|
38
|
+
"ollama",
|
|
39
|
+
"multi-provider",
|
|
40
|
+
"load-balancing",
|
|
41
|
+
"failover"
|
|
42
|
+
],
|
|
43
|
+
"author": "Claude Flow Team",
|
|
44
|
+
"license": "MIT",
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"events": "^3.3.0"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/node": "^20.0.0",
|
|
50
|
+
"dotenv": "^16.4.5",
|
|
51
|
+
"tsx": "^4.19.0",
|
|
52
|
+
"typescript": "^5.5.0",
|
|
53
|
+
"vitest": "^4.0.16"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"@ruvector/ruvllm": "^0.2.3"
|
|
57
|
+
},
|
|
58
|
+
"peerDependenciesMeta": {
|
|
59
|
+
"@ruvector/ruvllm": {
|
|
60
|
+
"optional": true
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"engines": {
|
|
64
|
+
"node": ">=20.0.0"
|
|
65
|
+
},
|
|
66
|
+
"publishConfig": {
|
|
67
|
+
"access": "public",
|
|
68
|
+
"tag": "v3alpha"
|
|
69
|
+
}
|
|
70
|
+
}
|