adaptive-memory-multi-model-router 1.9.0 → 1.9.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/dist/cli.js +344 -109
- package/dist/index.js +14 -0
- package/dist/providers/providerConfig.js +452 -0
- package/dist/providers/registry.js +60 -41
- package/dist/routing/advancedRouter.js +368 -310
- package/package.json +2 -2
- package/test.js +376 -0
|
@@ -1,332 +1,390 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
3
|
+
* A3M Router - Generic Adaptive Routing (RouteLLM Style)
|
|
4
|
+
*
|
|
5
|
+
* Routes queries to the best available LLM based on:
|
|
6
|
+
* - Query features (code, math, creative, etc.)
|
|
7
|
+
* - Provider availability (checks API keys)
|
|
8
|
+
* - Cost optimization
|
|
9
|
+
* - Quality vs speed tradeoff
|
|
10
|
+
*
|
|
11
|
+
* All provider references are dynamically loaded from providerConfig.
|
|
12
|
+
* Users can add/remove providers via environment variables or config files.
|
|
7
13
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
exports.extractQueryFeatures = extractQueryFeatures;
|
|
11
|
-
exports.routeQuery = routeQuery;
|
|
12
|
-
exports.routeBatch = routeBatch;
|
|
13
|
-
exports.recommendForTask = recommendForTask;
|
|
14
|
-
exports.updateModelProfile = updateModelProfile;
|
|
14
|
+
|
|
15
|
+
const { getAvailableProviders } = require("../providers/providerConfig");
|
|
15
16
|
const tokenUtils_1 = require("../utils/tokenUtils");
|
|
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
|
-
strengths
|
|
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
|
-
strengths: ["reasoning", "long-context"],
|
|
76
|
-
context_window: 2000000
|
|
77
|
-
},
|
|
78
|
-
"groq/llama-3.3-70b": {
|
|
79
|
-
name: "groq/llama-3.3-70b",
|
|
80
|
-
provider: "groq",
|
|
81
|
-
cost_per_1k_input: 0.59,
|
|
82
|
-
cost_per_1k_output: 0.79,
|
|
83
|
-
latency_ms: 400,
|
|
84
|
-
quality_score: 0.82,
|
|
85
|
-
strengths: ["fast", "coding"],
|
|
86
|
-
context_window: 128000
|
|
87
|
-
},
|
|
88
|
-
"cerebras/llama-3.3-70b": {
|
|
89
|
-
name: "cerebras/llama-3.3-70b",
|
|
90
|
-
provider: "cerebras",
|
|
91
|
-
cost_per_1k_input: 0.60,
|
|
92
|
-
cost_per_1k_output: 0.60,
|
|
93
|
-
latency_ms: 350,
|
|
94
|
-
quality_score: 0.82,
|
|
95
|
-
strengths: ["fast", "budget"],
|
|
96
|
-
context_window: 128000
|
|
97
|
-
},
|
|
98
|
-
"local/llama-3.3-70b": {
|
|
99
|
-
name: "local/llama-3.3-70b",
|
|
100
|
-
provider: "ollama",
|
|
101
|
-
cost_per_1k_input: 0.00,
|
|
102
|
-
cost_per_1k_output: 0.00,
|
|
103
|
-
latency_ms: 100,
|
|
104
|
-
quality_score: 0.75,
|
|
105
|
-
strengths: ["privacy", "free"],
|
|
106
|
-
context_window: 128000
|
|
17
|
+
|
|
18
|
+
// ============================================================
|
|
19
|
+
// DYNAMIC MODEL PROFILES (built from available providers)
|
|
20
|
+
// ============================================================
|
|
21
|
+
|
|
22
|
+
function buildModelProfiles() {
|
|
23
|
+
const profiles = {};
|
|
24
|
+
const available = getAvailableProviders();
|
|
25
|
+
|
|
26
|
+
for (const [providerId, provider] of Object.entries(available)) {
|
|
27
|
+
for (const model of provider.models) {
|
|
28
|
+
const modelKey = model.includes('/') ? model : providerId + '/' + model;
|
|
29
|
+
const costPerKInput = provider.costPerK ? provider.costPerK.input : 0;
|
|
30
|
+
const costPerKOutput = provider.costPerK ? provider.costPerK.output : 0;
|
|
31
|
+
|
|
32
|
+
// Assign strengths based on model characteristics
|
|
33
|
+
const strengths = [];
|
|
34
|
+
if (provider.type === 'cli') {
|
|
35
|
+
strengths.push('free', 'local');
|
|
36
|
+
}
|
|
37
|
+
if (costPerKInput < 0.3) {
|
|
38
|
+
strengths.push('budget', 'fast');
|
|
39
|
+
} else if (costPerKInput > 2) {
|
|
40
|
+
strengths.push('premium', 'reasoning');
|
|
41
|
+
}
|
|
42
|
+
if (provider.name === 'Mistral' || provider.name === 'Groq' || provider.name === 'Cerebras') {
|
|
43
|
+
strengths.push('fast', 'coding');
|
|
44
|
+
}
|
|
45
|
+
if (provider.name === 'CommandCode') {
|
|
46
|
+
strengths.push('code-aware', 'context-rich');
|
|
47
|
+
}
|
|
48
|
+
if (provider.name === 'OpenCode') {
|
|
49
|
+
strengths.push('free', 'multi-model');
|
|
50
|
+
}
|
|
51
|
+
if (provider.name === 'Google') {
|
|
52
|
+
strengths.push('multilingual', 'long-context');
|
|
53
|
+
}
|
|
54
|
+
if (provider.name === 'OpenAI') {
|
|
55
|
+
strengths.push('reasoning', 'coding', 'analysis');
|
|
56
|
+
}
|
|
57
|
+
if (provider.name === 'Anthropic') {
|
|
58
|
+
strengths.push('reasoning', 'creative', 'analysis');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
profiles[modelKey] = {
|
|
62
|
+
name: modelKey,
|
|
63
|
+
provider: providerId,
|
|
64
|
+
providerName: provider.name,
|
|
65
|
+
cost_per_1k_input: costPerKInput,
|
|
66
|
+
cost_per_1k_output: costPerKOutput,
|
|
67
|
+
latency_ms: provider.type === 'cli' ? 5000 : (provider.priority * 200 + 300),
|
|
68
|
+
quality_score: strengths.includes('premium') ? 0.95 :
|
|
69
|
+
strengths.includes('reasoning') ? 0.90 :
|
|
70
|
+
strengths.includes('fast') ? 0.82 : 0.80,
|
|
71
|
+
strengths,
|
|
72
|
+
context_window: provider.maxTokens || 8192,
|
|
73
|
+
type: provider.type,
|
|
74
|
+
priority: provider.priority,
|
|
75
|
+
};
|
|
107
76
|
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return profiles;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let MODEL_PROFILES = buildModelProfiles();
|
|
83
|
+
|
|
84
|
+
// Refresh profiles when providers change
|
|
85
|
+
function refreshModelProfiles() {
|
|
86
|
+
MODEL_PROFILES = buildModelProfiles();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
exports.MODEL_PROFILES = MODEL_PROFILES;
|
|
90
|
+
|
|
91
|
+
// ============================================================
|
|
92
|
+
// FEATURE EXTRACTION
|
|
93
|
+
// ============================================================
|
|
94
|
+
|
|
112
95
|
function extractQueryFeatures(prompt) {
|
|
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
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
96
|
+
const lower = prompt.toLowerCase();
|
|
97
|
+
|
|
98
|
+
// Code patterns
|
|
99
|
+
const code_indicators = [
|
|
100
|
+
"function", "class ", "def ", "import ", "const ", "let ",
|
|
101
|
+
"python", "javascript", "typescript", "java", "cpp", "rust",
|
|
102
|
+
"```", "=>", "->", "async", "await"
|
|
103
|
+
];
|
|
104
|
+
const has_code = code_indicators.some(pattern => lower.includes(pattern));
|
|
105
|
+
|
|
106
|
+
// Math patterns
|
|
107
|
+
const math_indicators = [
|
|
108
|
+
"equation", "formula", "calculate", "sqrt", "^", "log",
|
|
109
|
+
"sin", "cos", "tan", "integral", "derivative", "$", "math",
|
|
110
|
+
"∫", "∂", "∑", "∏", "√", "∞", "π", "θ", "β",
|
|
111
|
+
"dx", "dy", "dz", "=", "solver", "compute"
|
|
112
|
+
];
|
|
113
|
+
const has_math = math_indicators.some(pattern => prompt.includes(pattern));
|
|
114
|
+
|
|
115
|
+
// Multilingual
|
|
116
|
+
const lang_patterns = [
|
|
117
|
+
/[\u4e00-\u9fff]/, // Chinese
|
|
118
|
+
/[\u3040-\u309f\u30a0-\u30ff]/, // Japanese
|
|
119
|
+
/[\uac00-\ud7af]/, // Korean
|
|
120
|
+
/[а-яА-Я]/, // Russian
|
|
121
|
+
/[áéíóúñ]/ // Spanish accented
|
|
122
|
+
];
|
|
123
|
+
const is_multilingual = lang_patterns.some(pattern => pattern.test(prompt));
|
|
124
|
+
|
|
125
|
+
// Translation detection
|
|
126
|
+
const translation_indicators = ["translate", "translation", "translate to", "in french", "in spanish", "in japanese"];
|
|
127
|
+
const is_translation = translation_indicators.some(pattern => lower.includes(pattern));
|
|
128
|
+
|
|
129
|
+
// Creative writing
|
|
130
|
+
const creative_indicators = [
|
|
131
|
+
"write a", "story", "poem", "creative", "imagine",
|
|
132
|
+
"describe", "explain in", "tell me", "narrative", "joke"
|
|
133
|
+
];
|
|
134
|
+
const is_creative = creative_indicators.some(pattern => lower.includes(pattern));
|
|
135
|
+
|
|
136
|
+
// Reasoning
|
|
137
|
+
const reasoning_indicators = [
|
|
138
|
+
"explain", "why", "because", "therefore", "thus",
|
|
139
|
+
"analyze", "think", "consider", "reason", "logic"
|
|
140
|
+
];
|
|
141
|
+
const requires_reasoning = reasoning_indicators.some(pattern => lower.includes(pattern));
|
|
142
|
+
|
|
143
|
+
// Security/specialized
|
|
144
|
+
const security_indicators = ["security", "vulnerability", "inject", "exploit", "attack", "encryption", "auth"];
|
|
145
|
+
const is_security = security_indicators.some(pattern => lower.includes(pattern));
|
|
146
|
+
|
|
147
|
+
// DevOps
|
|
148
|
+
const devops_indicators = ["ci/cd", "docker", "kubernetes", "k8s", "deploy", "pipeline", "github action", "terraform"];
|
|
149
|
+
const is_devops = devops_indicators.some(pattern => lower.includes(pattern));
|
|
150
|
+
|
|
151
|
+
// Data/ML
|
|
152
|
+
const data_indicators = ["dataset", "pandas", "numpy", "training", "model", "neural", "transformer", "bert", "llm"];
|
|
153
|
+
const is_data = data_indicators.some(pattern => lower.includes(pattern));
|
|
154
|
+
|
|
155
|
+
// Complexity estimation
|
|
156
|
+
const tokens = tokenUtils_1.countTokens(prompt, "gpt-4o");
|
|
157
|
+
let complexity = 0.3;
|
|
158
|
+
if (tokens > 1000) complexity += 0.2;
|
|
159
|
+
if (has_code) complexity += 0.15;
|
|
160
|
+
if (has_math) complexity += 0.2;
|
|
161
|
+
if (requires_reasoning) complexity += 0.15;
|
|
162
|
+
if (is_creative) complexity += 0.1;
|
|
163
|
+
if (is_security) complexity += 0.1;
|
|
164
|
+
if (is_devops) complexity += 0.1;
|
|
165
|
+
if (is_data) complexity += 0.15;
|
|
166
|
+
complexity = Math.min(1.0, complexity);
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
complexity,
|
|
170
|
+
length: tokens,
|
|
171
|
+
has_code,
|
|
172
|
+
has_math,
|
|
173
|
+
is_multilingual,
|
|
174
|
+
is_translation,
|
|
175
|
+
is_creative,
|
|
176
|
+
requires_reasoning,
|
|
177
|
+
is_security,
|
|
178
|
+
is_devops,
|
|
179
|
+
is_data,
|
|
180
|
+
};
|
|
173
181
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
182
|
+
|
|
183
|
+
exports.extractQueryFeatures = extractQueryFeatures;
|
|
184
|
+
|
|
185
|
+
// ============================================================
|
|
186
|
+
// SCORING FUNCTIONS
|
|
187
|
+
// ============================================================
|
|
188
|
+
|
|
177
189
|
function scoreModelFit(model, features) {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
if (features.complexity < 0.4 && model.latency_ms < 1000) {
|
|
197
|
-
score += 0.1;
|
|
198
|
-
}
|
|
199
|
-
return score;
|
|
190
|
+
let score = model.quality_score * 0.4; // Base quality
|
|
191
|
+
|
|
192
|
+
if (features.has_code && model.strengths.includes("coding")) score += 0.2;
|
|
193
|
+
if (features.requires_reasoning && model.strengths.includes("reasoning")) score += 0.2;
|
|
194
|
+
if (features.is_creative && model.strengths.includes("creative")) score += 0.15;
|
|
195
|
+
if (features.is_multilingual && model.strengths.includes("multilingual")) score += 0.15;
|
|
196
|
+
if (features.has_math && model.strengths.includes("analysis")) score += 0.15;
|
|
197
|
+
if (features.is_security && model.strengths.includes("reasoning")) score += 0.1;
|
|
198
|
+
if (features.is_data && model.strengths.includes("analysis")) score += 0.1;
|
|
199
|
+
|
|
200
|
+
// Free/local providers bonus for simple tasks
|
|
201
|
+
if (features.complexity < 0.4 && model.strengths.includes("free")) score += 0.15;
|
|
202
|
+
if (features.complexity < 0.4 && model.latency_ms < 1000) score += 0.1;
|
|
203
|
+
|
|
204
|
+
// Code-aware providers for code tasks
|
|
205
|
+
if (features.has_code && model.strengths.includes("code-aware")) score += 0.2;
|
|
206
|
+
|
|
207
|
+
return score;
|
|
200
208
|
}
|
|
201
|
-
|
|
202
|
-
* Cost efficiency score (inverse of normalized cost)
|
|
203
|
-
*/
|
|
209
|
+
|
|
204
210
|
function costEfficiency(model, features) {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
// For complex tasks, deprioritize cost
|
|
211
|
-
return (1 - Math.min(avg_cost / 10, 1)) * 0.2;
|
|
211
|
+
const avg_cost = (model.cost_per_1k_input + model.cost_per_1k_output) / 2;
|
|
212
|
+
if (features.complexity < 0.5) {
|
|
213
|
+
return (1 - Math.min(avg_cost / 10, 1)) * 0.6;
|
|
214
|
+
}
|
|
215
|
+
return (1 - Math.min(avg_cost / 10, 1)) * 0.2;
|
|
212
216
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
217
|
+
|
|
218
|
+
// ============================================================
|
|
219
|
+
// ROUTING
|
|
220
|
+
// ============================================================
|
|
221
|
+
|
|
216
222
|
function routeQuery(prompt, available_models, budget_multiplier = 1.0) {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
const score_b = b.quality_score * complexity_bias + b.cost_score * (1 - complexity_bias);
|
|
238
|
-
return score_b - score_a;
|
|
223
|
+
// Refresh profiles to ensure we have latest provider config
|
|
224
|
+
refreshModelProfiles();
|
|
225
|
+
|
|
226
|
+
const features = extractQueryFeatures(prompt);
|
|
227
|
+
const candidate_names = available_models || Object.keys(MODEL_PROFILES);
|
|
228
|
+
|
|
229
|
+
// Filter to available models
|
|
230
|
+
const candidates = candidate_names
|
|
231
|
+
.filter(name => MODEL_PROFILES[name])
|
|
232
|
+
.map(name => {
|
|
233
|
+
const profile = MODEL_PROFILES[name];
|
|
234
|
+
const quality = scoreModelFit(profile, features);
|
|
235
|
+
const cost = costEfficiency(profile, features);
|
|
236
|
+
return {
|
|
237
|
+
name,
|
|
238
|
+
profile,
|
|
239
|
+
quality_score: quality,
|
|
240
|
+
cost_score: cost,
|
|
241
|
+
total_score: quality + cost
|
|
242
|
+
};
|
|
239
243
|
});
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
// Calculate confidence based on score gap
|
|
243
|
-
let confidence = 0.5;
|
|
244
|
-
if (candidates.length > 1) {
|
|
245
|
-
const gap = primary.total_score - candidates[1].total_score;
|
|
246
|
-
confidence = Math.min(0.95, 0.5 + gap * 2);
|
|
247
|
-
}
|
|
248
|
-
// Build reasoning
|
|
249
|
-
const reasons = [];
|
|
250
|
-
if (features.has_code)
|
|
251
|
-
reasons.push("code detected");
|
|
252
|
-
if (features.requires_reasoning)
|
|
253
|
-
reasons.push("reasoning needed");
|
|
254
|
-
if (features.complexity > 0.6)
|
|
255
|
-
reasons.push("high complexity");
|
|
256
|
-
if (features.is_multilingual)
|
|
257
|
-
reasons.push("multilingual");
|
|
258
|
-
const estimated_tokens = features.length * 1.5; // rough completion estimate
|
|
259
|
-
const estimated_cost = (0, tokenUtils_1.estimateCost)(features.length, estimated_tokens, primary.name);
|
|
244
|
+
|
|
245
|
+
if (candidates.length === 0) {
|
|
260
246
|
return {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
247
|
+
primary_model: null,
|
|
248
|
+
fallback_models: [],
|
|
249
|
+
confidence: 0,
|
|
250
|
+
reasoning: "No providers available - configure API keys in ~/.config/a3m-router/providers.json",
|
|
251
|
+
estimated_cost: 0,
|
|
252
|
+
estimated_latency_ms: 0,
|
|
267
253
|
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Sort by total score (quality vs cost tradeoff based on complexity)
|
|
257
|
+
const complexity_bias = features.complexity > 0.6 ? 0.7 : 0.3;
|
|
258
|
+
candidates.sort((a, b) => {
|
|
259
|
+
const score_a = a.quality_score * complexity_bias + a.cost_score * (1 - complexity_bias);
|
|
260
|
+
const score_b = b.quality_score * complexity_bias + b.cost_score * (1 - complexity_bias);
|
|
261
|
+
return score_b - score_a;
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
const primary = candidates[0];
|
|
265
|
+
const secondary = candidates.slice(1, 3);
|
|
266
|
+
|
|
267
|
+
// Calculate confidence based on score gap
|
|
268
|
+
let confidence = 0.5;
|
|
269
|
+
if (candidates.length > 1) {
|
|
270
|
+
const gap = primary.total_score - candidates[1].total_score;
|
|
271
|
+
confidence = Math.min(0.95, 0.5 + gap * 2);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Build reasoning
|
|
275
|
+
const reasons = [];
|
|
276
|
+
if (features.has_code) reasons.push("code detected");
|
|
277
|
+
if (features.requires_reasoning) reasons.push("reasoning needed");
|
|
278
|
+
if (features.complexity > 0.6) reasons.push("high complexity");
|
|
279
|
+
if (features.is_multilingual) reasons.push("multilingual");
|
|
280
|
+
if (features.is_translation) reasons.push("translation");
|
|
281
|
+
if (primary.profile.strengths.includes("free")) reasons.push("free tier");
|
|
282
|
+
|
|
283
|
+
const estimated_tokens = features.length * 1.5;
|
|
284
|
+
const estimated_cost = tokenUtils_1.estimateCost(features.length, estimated_tokens, primary.name);
|
|
285
|
+
|
|
286
|
+
return {
|
|
287
|
+
primary_model: primary.name,
|
|
288
|
+
fallback_models: secondary.map(c => c.name),
|
|
289
|
+
confidence,
|
|
290
|
+
reasoning: `Selected ${primary.profile.providerName || primary.profile.provider}/${primary.name} for ${reasons.join(", ") || "general query"}`,
|
|
291
|
+
estimated_cost: estimated_cost * budget_multiplier,
|
|
292
|
+
estimated_latency_ms: primary.profile.latency_ms,
|
|
293
|
+
features,
|
|
294
|
+
provider_type: primary.profile.type,
|
|
295
|
+
};
|
|
268
296
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
297
|
+
|
|
298
|
+
exports.routeQuery = routeQuery;
|
|
299
|
+
|
|
300
|
+
// ============================================================
|
|
301
|
+
// BATCH ROUTING
|
|
302
|
+
// ============================================================
|
|
303
|
+
|
|
304
|
+
function routeBatch(prompts, options = {}) {
|
|
305
|
+
const decisions = prompts.map(p => routeQuery(p));
|
|
306
|
+
|
|
307
|
+
if (options.same_model && decisions.length > 0) {
|
|
308
|
+
const primary_model = decisions[0].primary_model;
|
|
309
|
+
decisions.forEach(d => {
|
|
310
|
+
d.primary_model = primary_model;
|
|
311
|
+
d.fallback_models = decisions[0].fallback_models;
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (options.max_cost_per_prompt) {
|
|
316
|
+
decisions.forEach(d => {
|
|
317
|
+
if (d.estimated_cost > options.max_cost_per_prompt) {
|
|
318
|
+
const cheap = Object.entries(MODEL_PROFILES)
|
|
319
|
+
.find(([name, p]) => p.cost_per_1k_input < 0.5);
|
|
320
|
+
if (cheap) {
|
|
321
|
+
d.primary_model = cheap[0];
|
|
322
|
+
d.reasoning = `Budget-limited routing to ${cheap[1].providerName || cheap[1].provider}`;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return decisions;
|
|
296
329
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
330
|
+
|
|
331
|
+
exports.routeBatch = routeBatch;
|
|
332
|
+
|
|
333
|
+
// ============================================================
|
|
334
|
+
// TASK RECOMMENDATIONS
|
|
335
|
+
// ============================================================
|
|
336
|
+
|
|
300
337
|
function recommendForTask(task) {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
338
|
+
refreshModelProfiles();
|
|
339
|
+
const features = extractQueryFeatures(task);
|
|
340
|
+
const decision = routeQuery(task);
|
|
341
|
+
return {
|
|
342
|
+
primary: decision.primary_model,
|
|
343
|
+
fallbacks: decision.fallback_models,
|
|
344
|
+
reason: decision.reasoning,
|
|
345
|
+
features,
|
|
346
|
+
};
|
|
305
347
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
348
|
+
|
|
349
|
+
exports.recommendForTask = recommendForTask;
|
|
350
|
+
|
|
351
|
+
// ============================================================
|
|
352
|
+
// ONLINE LEARNING - Update model profiles from feedback
|
|
353
|
+
// ============================================================
|
|
354
|
+
|
|
355
|
+
function updateModelProfile(model_name, actual_latency_ms, actual_cost, quality_rating) {
|
|
356
|
+
refreshModelProfiles();
|
|
357
|
+
const profile = MODEL_PROFILES[model_name];
|
|
358
|
+
if (!profile) return;
|
|
359
|
+
|
|
360
|
+
const alpha = 0.2; // Learning rate
|
|
361
|
+
profile.latency_ms = profile.latency_ms * (1 - alpha) + actual_latency_ms * alpha;
|
|
362
|
+
profile.quality_score = profile.quality_score * (1 - alpha) + quality_rating * alpha;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
exports.updateModelProfile = updateModelProfile;
|
|
366
|
+
|
|
367
|
+
// ============================================================
|
|
368
|
+
// PROVIDER HEALTH CHECK
|
|
369
|
+
// ============================================================
|
|
370
|
+
|
|
371
|
+
async function getProviderHealth() {
|
|
372
|
+
const { checkAllProviders } = require("../providers/providerConfig");
|
|
373
|
+
return checkAllProviders();
|
|
323
374
|
}
|
|
375
|
+
|
|
376
|
+
exports.getProviderHealth = getProviderHealth;
|
|
377
|
+
|
|
378
|
+
// ============================================================
|
|
379
|
+
// Default export
|
|
380
|
+
// ============================================================
|
|
381
|
+
|
|
324
382
|
exports.default = {
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
383
|
+
extractQueryFeatures,
|
|
384
|
+
routeQuery,
|
|
385
|
+
routeBatch,
|
|
386
|
+
recommendForTask,
|
|
387
|
+
updateModelProfile,
|
|
388
|
+
getProviderHealth,
|
|
389
|
+
MODEL_PROFILES,
|
|
331
390
|
};
|
|
332
|
-
//# sourceMappingURL=advancedRouter.js.map
|