llmjs2 1.3.3 → 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 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/config.yaml CHANGED
@@ -141,7 +141,7 @@ guardrails:
141
141
 
142
142
  # Router configuration
143
143
  router_settings:
144
- routing_strategy: random # Options: default, random, sequential
144
+ routing_strategy: default # Options: default (sequential), random, sequential
145
145
 
146
146
  # Optional: Custom server settings (can also be set via CLI or env vars)
147
147
  # server_settings:
@@ -133,12 +133,17 @@ Each model in the list is defined with:
133
133
 
134
134
  ### Default Strategy
135
135
 
136
- When no strategy is specified, uses load balancing across models with the same `model_name`.
136
+ When no strategy is specified, uses sequential selection across all available models for auto-routing (when no specific model is requested).
137
137
 
138
138
  ```javascript
139
139
  const route = router(modelList); // or router(modelList, 'default')
140
140
 
141
- // Routes to one of the models with model_name="gpt-3.5-turbo"
141
+ // Auto-route with sequential selection (cycles through all models)
142
+ const response = await route.completion({
143
+ messages: [...]
144
+ });
145
+
146
+ // Routes to one of the models with model_name="gpt-3.5-turbo" (load balancing)
142
147
  const response = await route.completion({
143
148
  model: "gpt-3.5-turbo",
144
149
  messages: [...]
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
- const { provider, model: finalModel } = this.getProvider(model, options);
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "llmjs2",
3
- "version": "1.3.3",
3
+ "version": "1.3.5",
4
4
  "description": "A unified Node.js library for connecting to multiple LLM providers: OpenAI, Ollama, and OpenRouter",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
package/router.js CHANGED
@@ -84,8 +84,10 @@ class Router {
84
84
 
85
85
  case 'default':
86
86
  default:
87
- // Use first available model
88
- return allModels[0];
87
+ // Use sequential selection for auto-routing
88
+ const selectedModel = allModels[this.sequentialIndex];
89
+ this.sequentialIndex = (this.sequentialIndex + 1) % allModels.length;
90
+ return selectedModel;
89
91
  }
90
92
  }
91
93
 
@@ -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?"\n');
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?');