llmjs2 1.3.4 → 1.3.5
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 +8 -0
- package/index.js +48 -1
- package/package.json +1 -1
- package/test-completion.js +2 -1
package/README.md
CHANGED
|
@@ -118,9 +118,17 @@ export OPEN_ROUTER_API_KEY=your_openrouter_api_key
|
|
|
118
118
|
```javascript
|
|
119
119
|
import { completion } from 'llmjs2';
|
|
120
120
|
|
|
121
|
+
// Auto-selects provider based on available API keys
|
|
122
|
+
// Cycles through: Ollama → OpenRouter → OpenAI → Ollama...
|
|
121
123
|
const response = await completion('Explain quantum physics simply');
|
|
122
124
|
```
|
|
123
125
|
|
|
126
|
+
**Auto-Selection Logic:**
|
|
127
|
+
- Checks for `OLLAMA_API_KEY`, `OPEN_ROUTER_API_KEY`, `OPENAI_API_KEY`
|
|
128
|
+
- Cycles sequentially through available providers
|
|
129
|
+
- Uses default model for each provider
|
|
130
|
+
- Falls back gracefully if keys are missing
|
|
131
|
+
|
|
124
132
|
### 2. Provider-Specific Model
|
|
125
133
|
|
|
126
134
|
```javascript
|
package/index.js
CHANGED
|
@@ -5,6 +5,9 @@ const { router } = require('./router');
|
|
|
5
5
|
const { app } = require('./server');
|
|
6
6
|
const logger = require('./logger');
|
|
7
7
|
|
|
8
|
+
// Module-level variable to track sequential provider selection across completion calls
|
|
9
|
+
let autoProviderIndex = 0;
|
|
10
|
+
|
|
8
11
|
class LLMJS2 {
|
|
9
12
|
constructor(config = {}) {
|
|
10
13
|
this.providers = {
|
|
@@ -60,6 +63,38 @@ class LLMJS2 {
|
|
|
60
63
|
return { provider: null, model: modelString };
|
|
61
64
|
}
|
|
62
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Auto-select provider based on available API keys
|
|
68
|
+
* Cycles through available providers sequentially
|
|
69
|
+
*/
|
|
70
|
+
getAutoProvider() {
|
|
71
|
+
// Check which API keys are available
|
|
72
|
+
const availableProviders = [];
|
|
73
|
+
|
|
74
|
+
if (process.env.OLLAMA_API_KEY) {
|
|
75
|
+
availableProviders.push('ollama');
|
|
76
|
+
}
|
|
77
|
+
if (process.env.OPEN_ROUTER_API_KEY) {
|
|
78
|
+
availableProviders.push('openrouter');
|
|
79
|
+
}
|
|
80
|
+
if (process.env.OPENAI_API_KEY) {
|
|
81
|
+
availableProviders.push('openai');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (availableProviders.length === 0) {
|
|
85
|
+
throw new Error('No API keys found. Set OLLAMA_API_KEY, OPEN_ROUTER_API_KEY, or OPENAI_API_KEY environment variables.');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Get next provider in sequence using module-level index
|
|
89
|
+
const providerName = availableProviders[autoProviderIndex];
|
|
90
|
+
autoProviderIndex = (autoProviderIndex + 1) % availableProviders.length;
|
|
91
|
+
|
|
92
|
+
const provider = this.providers[providerName];
|
|
93
|
+
const model = provider.defaultModel;
|
|
94
|
+
|
|
95
|
+
return { provider, model };
|
|
96
|
+
}
|
|
97
|
+
|
|
63
98
|
/**
|
|
64
99
|
* Determine which provider to use
|
|
65
100
|
*/
|
|
@@ -155,7 +190,19 @@ class LLMJS2 {
|
|
|
155
190
|
try {
|
|
156
191
|
const { model, messages, options } = this.validateInput(input);
|
|
157
192
|
|
|
158
|
-
|
|
193
|
+
let provider, finalModel;
|
|
194
|
+
|
|
195
|
+
if (model) {
|
|
196
|
+
// Specific model requested
|
|
197
|
+
const result = this.getProvider(model, options);
|
|
198
|
+
provider = result.provider;
|
|
199
|
+
finalModel = result.model;
|
|
200
|
+
} else {
|
|
201
|
+
// No model specified - auto-select provider based on available API keys
|
|
202
|
+
const autoSelection = this.getAutoProvider();
|
|
203
|
+
provider = autoSelection.provider;
|
|
204
|
+
finalModel = autoSelection.model;
|
|
205
|
+
}
|
|
159
206
|
|
|
160
207
|
// Override provider API key if specified in options
|
|
161
208
|
if (options.apiKey) {
|
package/package.json
CHANGED
package/test-completion.js
CHANGED
|
@@ -12,7 +12,8 @@ async function testSimpleCompletion() {
|
|
|
12
12
|
|
|
13
13
|
try {
|
|
14
14
|
console.log('📤 Sending completion request...');
|
|
15
|
-
console.log('Prompt: "Hello! Can you tell me a short joke?"
|
|
15
|
+
console.log('Prompt: "Hello! Can you tell me a short joke?"');
|
|
16
|
+
console.log('Note: No model specified - will auto-select provider based on available API keys\n');
|
|
16
17
|
|
|
17
18
|
// Simple completion call - no model or API key specified
|
|
18
19
|
const response = await completion('Hello! Can you tell me a short joke?');
|