@retrivora-ai/rag-engine 2.0.7 → 2.0.8
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/handlers/index.js +706 -54
- package/dist/handlers/index.mjs +706 -54
- package/dist/index.js +5 -4
- package/dist/index.mjs +5 -4
- package/dist/server.js +706 -54
- package/dist/server.mjs +706 -54
- package/package.json +1 -1
- package/src/core/Pipeline.ts +19 -37
- package/src/llm/providers/UniversalLLMAdapter.ts +74 -16
- package/src/utils/UITransformer.ts +4 -5
package/dist/handlers/index.mjs
CHANGED
|
@@ -106,6 +106,609 @@ var init_templateUtils = __esm({
|
|
|
106
106
|
}
|
|
107
107
|
});
|
|
108
108
|
|
|
109
|
+
// ../../services/llm-gateway/providers/groq.ts
|
|
110
|
+
async function handleGroqRequest(req, apiKeyOverride) {
|
|
111
|
+
const apiKey = apiKeyOverride || process.env.GROQ_API_KEY;
|
|
112
|
+
if (!apiKey) {
|
|
113
|
+
throw new Error("Groq API Key missing. Please set GROQ_API_KEY in environment variables.");
|
|
114
|
+
}
|
|
115
|
+
const modelName = req.model.replace(/^groq\//i, "");
|
|
116
|
+
const payload = __spreadProps(__spreadValues({}, req), {
|
|
117
|
+
model: modelName
|
|
118
|
+
});
|
|
119
|
+
const response = await fetch("https://api.groq.com/openai/v1/chat/completions", {
|
|
120
|
+
method: "POST",
|
|
121
|
+
headers: {
|
|
122
|
+
"Content-Type": "application/json",
|
|
123
|
+
"Authorization": `Bearer ${apiKey}`
|
|
124
|
+
},
|
|
125
|
+
body: JSON.stringify(payload)
|
|
126
|
+
});
|
|
127
|
+
if (!response.ok) {
|
|
128
|
+
const errorText = await response.text();
|
|
129
|
+
throw new Error(`Groq API Error (${response.status}): ${errorText}`);
|
|
130
|
+
}
|
|
131
|
+
if (req.stream && response.body) {
|
|
132
|
+
return { stream: response.body };
|
|
133
|
+
}
|
|
134
|
+
const json = await response.json();
|
|
135
|
+
return { response: json };
|
|
136
|
+
}
|
|
137
|
+
var init_groq = __esm({
|
|
138
|
+
"../../services/llm-gateway/providers/groq.ts"() {
|
|
139
|
+
"use strict";
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
// ../../services/llm-gateway/providers/openai.ts
|
|
144
|
+
async function handleOpenAIRequest(req, apiKeyOverride) {
|
|
145
|
+
const apiKey = apiKeyOverride || process.env.OPENAI_API_KEY;
|
|
146
|
+
if (!apiKey) {
|
|
147
|
+
throw new Error("OpenAI API Key missing. Please set OPENAI_API_KEY in environment variables.");
|
|
148
|
+
}
|
|
149
|
+
const modelName = req.model.replace(/^(openai|gpt)\//i, "");
|
|
150
|
+
const payload = __spreadProps(__spreadValues({}, req), {
|
|
151
|
+
model: modelName
|
|
152
|
+
});
|
|
153
|
+
const response = await fetch("https://api.openai.com/v1/chat/completions", {
|
|
154
|
+
method: "POST",
|
|
155
|
+
headers: {
|
|
156
|
+
"Content-Type": "application/json",
|
|
157
|
+
"Authorization": `Bearer ${apiKey}`
|
|
158
|
+
},
|
|
159
|
+
body: JSON.stringify(payload)
|
|
160
|
+
});
|
|
161
|
+
if (!response.ok) {
|
|
162
|
+
const errorText = await response.text();
|
|
163
|
+
throw new Error(`OpenAI API Error (${response.status}): ${errorText}`);
|
|
164
|
+
}
|
|
165
|
+
if (req.stream && response.body) {
|
|
166
|
+
return { stream: response.body };
|
|
167
|
+
}
|
|
168
|
+
const json = await response.json();
|
|
169
|
+
return { response: json };
|
|
170
|
+
}
|
|
171
|
+
async function handleOpenAIEmbedding(req, apiKeyOverride) {
|
|
172
|
+
const apiKey = apiKeyOverride || process.env.OPENAI_API_KEY;
|
|
173
|
+
if (!apiKey) {
|
|
174
|
+
throw new Error("OpenAI API Key missing. Please set OPENAI_API_KEY in environment variables.");
|
|
175
|
+
}
|
|
176
|
+
const modelName = req.model.replace(/^(openai)\//i, "");
|
|
177
|
+
const response = await fetch("https://api.openai.com/v1/embeddings", {
|
|
178
|
+
method: "POST",
|
|
179
|
+
headers: {
|
|
180
|
+
"Content-Type": "application/json",
|
|
181
|
+
"Authorization": `Bearer ${apiKey}`
|
|
182
|
+
},
|
|
183
|
+
body: JSON.stringify({
|
|
184
|
+
model: modelName,
|
|
185
|
+
input: req.input
|
|
186
|
+
})
|
|
187
|
+
});
|
|
188
|
+
if (!response.ok) {
|
|
189
|
+
const errorText = await response.text();
|
|
190
|
+
throw new Error(`OpenAI Embedding API Error (${response.status}): ${errorText}`);
|
|
191
|
+
}
|
|
192
|
+
return await response.json();
|
|
193
|
+
}
|
|
194
|
+
var init_openai = __esm({
|
|
195
|
+
"../../services/llm-gateway/providers/openai.ts"() {
|
|
196
|
+
"use strict";
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// ../../services/llm-gateway/providers/gemini.ts
|
|
201
|
+
async function handleGeminiRequest(req, apiKeyOverride) {
|
|
202
|
+
const apiKey = apiKeyOverride || process.env.GEMINI_API_KEY;
|
|
203
|
+
if (!apiKey) {
|
|
204
|
+
throw new Error("Gemini API Key missing. Please set GEMINI_API_KEY in environment variables.");
|
|
205
|
+
}
|
|
206
|
+
let modelName = req.model.replace(/^(google|gemini)\//i, "");
|
|
207
|
+
if (!modelName.startsWith("gemini-")) {
|
|
208
|
+
modelName = `gemini-${modelName}`;
|
|
209
|
+
}
|
|
210
|
+
const payload = __spreadProps(__spreadValues({}, req), {
|
|
211
|
+
model: modelName
|
|
212
|
+
});
|
|
213
|
+
const response = await fetch("https://generativelanguage.googleapis.com/v1beta/openai/chat/completions", {
|
|
214
|
+
method: "POST",
|
|
215
|
+
headers: {
|
|
216
|
+
"Content-Type": "application/json",
|
|
217
|
+
"Authorization": `Bearer ${apiKey}`
|
|
218
|
+
},
|
|
219
|
+
body: JSON.stringify(payload)
|
|
220
|
+
});
|
|
221
|
+
if (!response.ok) {
|
|
222
|
+
const errorText = await response.text();
|
|
223
|
+
throw new Error(`Google Gemini API Error (${response.status}): ${errorText}`);
|
|
224
|
+
}
|
|
225
|
+
if (req.stream && response.body) {
|
|
226
|
+
return { stream: response.body };
|
|
227
|
+
}
|
|
228
|
+
const json = await response.json();
|
|
229
|
+
return { response: json };
|
|
230
|
+
}
|
|
231
|
+
async function handleGeminiEmbedding(req, apiKeyOverride) {
|
|
232
|
+
const apiKey = apiKeyOverride || process.env.GEMINI_API_KEY;
|
|
233
|
+
if (!apiKey) {
|
|
234
|
+
throw new Error("Gemini API Key missing. Please set GEMINI_API_KEY in environment variables.");
|
|
235
|
+
}
|
|
236
|
+
let modelName = req.model.replace(/^(google|gemini)\//i, "");
|
|
237
|
+
if (!modelName || modelName === "default" || modelName.includes("text-embedding-3") || modelName.includes("ada") || !modelName.startsWith("text-embedding-00") && !modelName.startsWith("embedding-00")) {
|
|
238
|
+
modelName = "text-embedding-004";
|
|
239
|
+
}
|
|
240
|
+
const response = await fetch("https://generativelanguage.googleapis.com/v1beta/openai/embeddings", {
|
|
241
|
+
method: "POST",
|
|
242
|
+
headers: {
|
|
243
|
+
"Content-Type": "application/json",
|
|
244
|
+
"Authorization": `Bearer ${apiKey}`
|
|
245
|
+
},
|
|
246
|
+
body: JSON.stringify({
|
|
247
|
+
model: modelName,
|
|
248
|
+
input: req.input
|
|
249
|
+
})
|
|
250
|
+
});
|
|
251
|
+
if (!response.ok) {
|
|
252
|
+
const errorText = await response.text();
|
|
253
|
+
throw new Error(`Google Gemini Embedding API Error (${response.status}): ${errorText}`);
|
|
254
|
+
}
|
|
255
|
+
return await response.json();
|
|
256
|
+
}
|
|
257
|
+
var init_gemini = __esm({
|
|
258
|
+
"../../services/llm-gateway/providers/gemini.ts"() {
|
|
259
|
+
"use strict";
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
// ../../services/llm-gateway/providers/huggingface.ts
|
|
264
|
+
async function handleHuggingFaceChatRequest(req, apiKeyOverride) {
|
|
265
|
+
var _a2, _b;
|
|
266
|
+
const apiKey = apiKeyOverride || process.env.HUGGINGFACE_API_KEY || process.env.HF_TOKEN;
|
|
267
|
+
if (!apiKey) {
|
|
268
|
+
throw new Error("HuggingFace API Key missing. Set HUGGINGFACE_API_KEY or HF_TOKEN in environment variables.");
|
|
269
|
+
}
|
|
270
|
+
let modelName = req.model.replace(/^(huggingface|hf)\//i, "");
|
|
271
|
+
if (!modelName || modelName === "default" || modelName.toLowerCase() === "qwen" || modelName.toLowerCase() === "qwen-coder") {
|
|
272
|
+
modelName = "Qwen/Qwen2.5-Coder-32B-Instruct";
|
|
273
|
+
}
|
|
274
|
+
const payload = __spreadProps(__spreadValues({}, req), {
|
|
275
|
+
model: modelName
|
|
276
|
+
});
|
|
277
|
+
try {
|
|
278
|
+
const response = await fetch("https://router.huggingface.co/v1/chat/completions", {
|
|
279
|
+
method: "POST",
|
|
280
|
+
headers: {
|
|
281
|
+
"Content-Type": "application/json",
|
|
282
|
+
"Authorization": `Bearer ${apiKey}`
|
|
283
|
+
},
|
|
284
|
+
body: JSON.stringify(payload)
|
|
285
|
+
});
|
|
286
|
+
if (response.ok) {
|
|
287
|
+
if (req.stream && response.body) {
|
|
288
|
+
return { stream: response.body };
|
|
289
|
+
}
|
|
290
|
+
const json = await response.json();
|
|
291
|
+
return { response: json };
|
|
292
|
+
}
|
|
293
|
+
} catch (e) {
|
|
294
|
+
}
|
|
295
|
+
try {
|
|
296
|
+
const response = await fetch("https://router.huggingface.co/hf-inference/v1/chat/completions", {
|
|
297
|
+
method: "POST",
|
|
298
|
+
headers: {
|
|
299
|
+
"Content-Type": "application/json",
|
|
300
|
+
"Authorization": `Bearer ${apiKey}`
|
|
301
|
+
},
|
|
302
|
+
body: JSON.stringify(payload)
|
|
303
|
+
});
|
|
304
|
+
if (response.ok) {
|
|
305
|
+
if (req.stream && response.body) {
|
|
306
|
+
return { stream: response.body };
|
|
307
|
+
}
|
|
308
|
+
const json = await response.json();
|
|
309
|
+
return { response: json };
|
|
310
|
+
}
|
|
311
|
+
} catch (e) {
|
|
312
|
+
}
|
|
313
|
+
const lastUserMsg = ((_a2 = req.messages.filter((m) => m.role === "user").pop()) == null ? void 0 : _a2.content) || "Hello";
|
|
314
|
+
const pipelineRes = await fetch(`https://router.huggingface.co/hf-inference/models/${modelName}`, {
|
|
315
|
+
method: "POST",
|
|
316
|
+
headers: {
|
|
317
|
+
"Content-Type": "application/json",
|
|
318
|
+
"Authorization": `Bearer ${apiKey}`
|
|
319
|
+
},
|
|
320
|
+
body: JSON.stringify({
|
|
321
|
+
inputs: lastUserMsg,
|
|
322
|
+
parameters: { max_new_tokens: req.max_tokens || 256, temperature: req.temperature || 0.7 },
|
|
323
|
+
options: { wait_for_model: true }
|
|
324
|
+
})
|
|
325
|
+
});
|
|
326
|
+
if (!pipelineRes.ok) {
|
|
327
|
+
const errorText = await pipelineRes.text();
|
|
328
|
+
throw new Error(`HuggingFace API Error (${pipelineRes.status}): ${errorText}`);
|
|
329
|
+
}
|
|
330
|
+
const raw = await pipelineRes.json();
|
|
331
|
+
const textOutput = Array.isArray(raw) ? ((_b = raw[0]) == null ? void 0 : _b.generated_text) || JSON.stringify(raw) : JSON.stringify(raw);
|
|
332
|
+
const formattedResponse = {
|
|
333
|
+
id: `hf-${Date.now()}`,
|
|
334
|
+
object: "chat.completion",
|
|
335
|
+
created: Math.floor(Date.now() / 1e3),
|
|
336
|
+
model: modelName,
|
|
337
|
+
choices: [
|
|
338
|
+
{
|
|
339
|
+
index: 0,
|
|
340
|
+
message: {
|
|
341
|
+
role: "assistant",
|
|
342
|
+
content: textOutput
|
|
343
|
+
},
|
|
344
|
+
finish_reason: "stop"
|
|
345
|
+
}
|
|
346
|
+
],
|
|
347
|
+
usage: {
|
|
348
|
+
prompt_tokens: lastUserMsg.length,
|
|
349
|
+
completion_tokens: textOutput.length,
|
|
350
|
+
total_tokens: lastUserMsg.length + textOutput.length
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
return { response: formattedResponse };
|
|
354
|
+
}
|
|
355
|
+
async function handleHuggingFaceEmbedding(req, apiKeyOverride) {
|
|
356
|
+
const apiKey = apiKeyOverride || process.env.HUGGINGFACE_API_KEY || process.env.HF_TOKEN;
|
|
357
|
+
if (!apiKey) {
|
|
358
|
+
throw new Error("HuggingFace API Key missing. Set HUGGINGFACE_API_KEY or HF_TOKEN in environment variables.");
|
|
359
|
+
}
|
|
360
|
+
let modelName = req.model.replace(/^(huggingface|hf)\//i, "");
|
|
361
|
+
if (!modelName || modelName === "default" || modelName.includes("text-embedding-3") || modelName.includes("ada") || !modelName.includes("/") && !modelName.includes("bge") && !modelName.includes("sentence-transformers")) {
|
|
362
|
+
modelName = "BAAI/bge-base-en-v1.5";
|
|
363
|
+
}
|
|
364
|
+
const inputs = Array.isArray(req.input) ? req.input : [req.input];
|
|
365
|
+
const fetchEmbeddingFromModel = async (targetModel) => {
|
|
366
|
+
try {
|
|
367
|
+
const routerRes = await fetch("https://router.huggingface.co/hf-inference/v1/embeddings", {
|
|
368
|
+
method: "POST",
|
|
369
|
+
headers: {
|
|
370
|
+
"Content-Type": "application/json",
|
|
371
|
+
"Authorization": `Bearer ${apiKey}`
|
|
372
|
+
},
|
|
373
|
+
body: JSON.stringify({
|
|
374
|
+
model: targetModel,
|
|
375
|
+
input: inputs
|
|
376
|
+
})
|
|
377
|
+
});
|
|
378
|
+
if (routerRes.ok) {
|
|
379
|
+
const json = await routerRes.json();
|
|
380
|
+
if (json && json.data) return json;
|
|
381
|
+
}
|
|
382
|
+
} catch (e) {
|
|
383
|
+
}
|
|
384
|
+
const response = await fetch(`https://router.huggingface.co/hf-inference/models/${targetModel}`, {
|
|
385
|
+
method: "POST",
|
|
386
|
+
headers: {
|
|
387
|
+
"Content-Type": "application/json",
|
|
388
|
+
"Authorization": `Bearer ${apiKey}`
|
|
389
|
+
},
|
|
390
|
+
body: JSON.stringify({
|
|
391
|
+
inputs,
|
|
392
|
+
options: { wait_for_model: true }
|
|
393
|
+
})
|
|
394
|
+
});
|
|
395
|
+
if (!response.ok) {
|
|
396
|
+
const errorText = await response.text();
|
|
397
|
+
throw new Error(`HuggingFace API Error (${response.status}): ${errorText}`);
|
|
398
|
+
}
|
|
399
|
+
const rawData = await response.json();
|
|
400
|
+
const embeddings = Array.isArray(rawData[0]) ? typeof rawData[0][0] === "number" ? rawData : rawData.map((arr) => arr[0]) : [rawData];
|
|
401
|
+
return {
|
|
402
|
+
object: "list",
|
|
403
|
+
data: embeddings.map((vec, idx) => ({
|
|
404
|
+
object: "embedding",
|
|
405
|
+
embedding: vec,
|
|
406
|
+
index: idx
|
|
407
|
+
})),
|
|
408
|
+
model: targetModel,
|
|
409
|
+
usage: {
|
|
410
|
+
prompt_tokens: inputs.reduce((acc, str) => acc + str.length, 0),
|
|
411
|
+
completion_tokens: 0,
|
|
412
|
+
total_tokens: inputs.reduce((acc, str) => acc + str.length, 0)
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
};
|
|
416
|
+
try {
|
|
417
|
+
return await fetchEmbeddingFromModel(modelName);
|
|
418
|
+
} catch (err) {
|
|
419
|
+
if (modelName !== "BAAI/bge-base-en-v1.5") {
|
|
420
|
+
console.warn(`[LLM Gateway] HuggingFace model ${modelName} failed. Falling back to BAAI/bge-base-en-v1.5...`, err);
|
|
421
|
+
return await fetchEmbeddingFromModel("BAAI/bge-base-en-v1.5");
|
|
422
|
+
}
|
|
423
|
+
throw err;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
var init_huggingface = __esm({
|
|
427
|
+
"../../services/llm-gateway/providers/huggingface.ts"() {
|
|
428
|
+
"use strict";
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
// ../../services/llm-gateway/providers/anthropic.ts
|
|
433
|
+
async function handleAnthropicRequest(req, apiKeyOverride) {
|
|
434
|
+
var _a2, _b;
|
|
435
|
+
const apiKey = apiKeyOverride || process.env.ANTHROPIC_API_KEY;
|
|
436
|
+
if (!apiKey) {
|
|
437
|
+
throw new Error("Anthropic API Key missing. Please set ANTHROPIC_API_KEY in environment variables.");
|
|
438
|
+
}
|
|
439
|
+
const modelName = req.model.replace(/^(anthropic|claude)\//i, "");
|
|
440
|
+
let systemPrompt = void 0;
|
|
441
|
+
const anthropicMessages = [];
|
|
442
|
+
for (const msg of req.messages) {
|
|
443
|
+
if (msg.role === "system") {
|
|
444
|
+
systemPrompt = systemPrompt ? `${systemPrompt}
|
|
445
|
+
${msg.content}` : msg.content;
|
|
446
|
+
} else {
|
|
447
|
+
anthropicMessages.push({
|
|
448
|
+
role: msg.role === "assistant" ? "assistant" : "user",
|
|
449
|
+
content: msg.content
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
if (anthropicMessages.length === 0) {
|
|
454
|
+
anthropicMessages.push({ role: "user", content: "Hello" });
|
|
455
|
+
}
|
|
456
|
+
const payload = {
|
|
457
|
+
model: modelName,
|
|
458
|
+
messages: anthropicMessages,
|
|
459
|
+
max_tokens: req.max_tokens || req.max_completion_tokens || 1024,
|
|
460
|
+
stream: req.stream || false
|
|
461
|
+
};
|
|
462
|
+
if (systemPrompt) {
|
|
463
|
+
payload.system = systemPrompt;
|
|
464
|
+
}
|
|
465
|
+
if (req.temperature !== void 0) {
|
|
466
|
+
payload.temperature = req.temperature;
|
|
467
|
+
}
|
|
468
|
+
const response = await fetch("https://api.anthropic.com/v1/messages", {
|
|
469
|
+
method: "POST",
|
|
470
|
+
headers: {
|
|
471
|
+
"Content-Type": "application/json",
|
|
472
|
+
"x-api-key": apiKey,
|
|
473
|
+
"anthropic-version": "2023-06-01"
|
|
474
|
+
},
|
|
475
|
+
body: JSON.stringify(payload)
|
|
476
|
+
});
|
|
477
|
+
if (!response.ok) {
|
|
478
|
+
const errorText = await response.text();
|
|
479
|
+
throw new Error(`Anthropic API Error (${response.status}): ${errorText}`);
|
|
480
|
+
}
|
|
481
|
+
if (req.stream && response.body) {
|
|
482
|
+
return { stream: response.body };
|
|
483
|
+
}
|
|
484
|
+
const json = await response.json();
|
|
485
|
+
const textContent = ((_b = (_a2 = json.content) == null ? void 0 : _a2[0]) == null ? void 0 : _b.text) || "";
|
|
486
|
+
const openAIResponse = {
|
|
487
|
+
id: json.id || `chatcmpl-${Date.now()}`,
|
|
488
|
+
object: "chat.completion",
|
|
489
|
+
created: Math.floor(Date.now() / 1e3),
|
|
490
|
+
model: json.model || modelName,
|
|
491
|
+
choices: [
|
|
492
|
+
{
|
|
493
|
+
index: 0,
|
|
494
|
+
message: {
|
|
495
|
+
role: "assistant",
|
|
496
|
+
content: textContent
|
|
497
|
+
},
|
|
498
|
+
finish_reason: json.stop_reason === "end_turn" ? "stop" : json.stop_reason || "stop"
|
|
499
|
+
}
|
|
500
|
+
],
|
|
501
|
+
usage: json.usage ? {
|
|
502
|
+
prompt_tokens: json.usage.input_tokens,
|
|
503
|
+
completion_tokens: json.usage.output_tokens,
|
|
504
|
+
total_tokens: json.usage.input_tokens + json.usage.output_tokens
|
|
505
|
+
} : void 0
|
|
506
|
+
};
|
|
507
|
+
return { response: openAIResponse };
|
|
508
|
+
}
|
|
509
|
+
var init_anthropic = __esm({
|
|
510
|
+
"../../services/llm-gateway/providers/anthropic.ts"() {
|
|
511
|
+
"use strict";
|
|
512
|
+
}
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
// ../../services/llm-gateway/providers/ollama.ts
|
|
516
|
+
async function handleOllamaRequest(req, baseUrlOverride) {
|
|
517
|
+
const baseUrl = (baseUrlOverride || process.env.OLLAMA_BASE_URL || "http://localhost:11434/v1").replace(/\/+$/, "");
|
|
518
|
+
const modelName = req.model.replace(/^ollama\//i, "");
|
|
519
|
+
const payload = __spreadProps(__spreadValues({}, req), {
|
|
520
|
+
model: modelName
|
|
521
|
+
});
|
|
522
|
+
const response = await fetch(`${baseUrl}/chat/completions`, {
|
|
523
|
+
method: "POST",
|
|
524
|
+
headers: {
|
|
525
|
+
"Content-Type": "application/json"
|
|
526
|
+
},
|
|
527
|
+
body: JSON.stringify(payload)
|
|
528
|
+
});
|
|
529
|
+
if (!response.ok) {
|
|
530
|
+
const errorText = await response.text();
|
|
531
|
+
throw new Error(`Ollama API Error (${response.status}): ${errorText}`);
|
|
532
|
+
}
|
|
533
|
+
if (req.stream && response.body) {
|
|
534
|
+
return { stream: response.body };
|
|
535
|
+
}
|
|
536
|
+
const json = await response.json();
|
|
537
|
+
return { response: json };
|
|
538
|
+
}
|
|
539
|
+
var init_ollama = __esm({
|
|
540
|
+
"../../services/llm-gateway/providers/ollama.ts"() {
|
|
541
|
+
"use strict";
|
|
542
|
+
}
|
|
543
|
+
});
|
|
544
|
+
|
|
545
|
+
// ../../services/llm-gateway/router.ts
|
|
546
|
+
var router_exports = {};
|
|
547
|
+
__export(router_exports, {
|
|
548
|
+
SUPPORTED_MODELS: () => SUPPORTED_MODELS,
|
|
549
|
+
dispatchChatCompletion: () => dispatchChatCompletion,
|
|
550
|
+
dispatchEmbedding: () => dispatchEmbedding,
|
|
551
|
+
resolveProvider: () => resolveProvider
|
|
552
|
+
});
|
|
553
|
+
function resolveProvider(model) {
|
|
554
|
+
const lower = model.toLowerCase();
|
|
555
|
+
if (lower.startsWith("huggingface/") || lower.startsWith("hf/")) {
|
|
556
|
+
return "huggingface";
|
|
557
|
+
}
|
|
558
|
+
if (lower.startsWith("ollama/")) {
|
|
559
|
+
return "ollama";
|
|
560
|
+
}
|
|
561
|
+
if (lower.startsWith("groq/") || lower.includes("llama") || lower.includes("qwen") || lower.includes("mixtral")) {
|
|
562
|
+
return "groq";
|
|
563
|
+
}
|
|
564
|
+
if (process.env.GROQ_API_KEY) return "groq";
|
|
565
|
+
if (process.env.OPENAI_API_KEY) return "openai";
|
|
566
|
+
if (process.env.GEMINI_API_KEY) return "gemini";
|
|
567
|
+
if (process.env.ANTHROPIC_API_KEY) return "anthropic";
|
|
568
|
+
if (process.env.HF_TOKEN || process.env.HUGGINGFACE_API_KEY) return "huggingface";
|
|
569
|
+
return "groq";
|
|
570
|
+
}
|
|
571
|
+
function cleanApiKeyOverride(apiKeyOverride) {
|
|
572
|
+
if (!apiKeyOverride) return void 0;
|
|
573
|
+
const key = apiKeyOverride.trim();
|
|
574
|
+
if (key === process.env.LITELLM_API_KEY || key === process.env.LITELLM_MASTER_KEY || key.startsWith("sk-retrivora") || !key.startsWith("gsk_") && !key.startsWith("AIza") && !key.startsWith("sk-ant-") && !key.startsWith("sk-proj-")) {
|
|
575
|
+
return void 0;
|
|
576
|
+
}
|
|
577
|
+
return key;
|
|
578
|
+
}
|
|
579
|
+
async function dispatchChatCompletion(req, apiKeyOverride) {
|
|
580
|
+
const effectiveKey = cleanApiKeyOverride(apiKeyOverride);
|
|
581
|
+
const provider = resolveProvider(req.model);
|
|
582
|
+
try {
|
|
583
|
+
switch (provider) {
|
|
584
|
+
case "groq":
|
|
585
|
+
return await handleGroqRequest(req, effectiveKey);
|
|
586
|
+
case "openai":
|
|
587
|
+
return await handleOpenAIRequest(req, effectiveKey);
|
|
588
|
+
case "gemini":
|
|
589
|
+
return await handleGeminiRequest(req, effectiveKey);
|
|
590
|
+
case "anthropic":
|
|
591
|
+
return await handleAnthropicRequest(req, effectiveKey);
|
|
592
|
+
case "ollama":
|
|
593
|
+
return await handleOllamaRequest(req);
|
|
594
|
+
case "huggingface":
|
|
595
|
+
return await handleHuggingFaceChatRequest(req, effectiveKey);
|
|
596
|
+
default:
|
|
597
|
+
throw new Error(`Unsupported LLM provider for model: ${req.model}`);
|
|
598
|
+
}
|
|
599
|
+
} catch (error) {
|
|
600
|
+
if (/rate[- ]?limit|429|exhausted/i.test(error.message)) {
|
|
601
|
+
const isGroqKey = process.env.GROQ_API_KEY || effectiveKey && effectiveKey.startsWith("gsk_");
|
|
602
|
+
const reqModelLower = req.model.toLowerCase();
|
|
603
|
+
if (isGroqKey && !reqModelLower.includes("llama-3.3-70b")) {
|
|
604
|
+
console.warn(`[LLM Gateway] Provider ${provider} rate limited (${error.message}). Falling back to secondary Groq model (llama-3.3-70b-versatile)...`);
|
|
605
|
+
try {
|
|
606
|
+
return await handleGroqRequest(__spreadProps(__spreadValues({}, req), { model: "llama-3.3-70b-versatile" }), effectiveKey);
|
|
607
|
+
} catch (e) {
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
const geminiKey = process.env.GEMINI_API_KEY || effectiveKey;
|
|
611
|
+
if (provider !== "gemini" && geminiKey && (geminiKey.startsWith("AIza") || !geminiKey.startsWith("AQ."))) {
|
|
612
|
+
console.warn(`[LLM Gateway] Provider ${provider} rate limited (${error.message}). Falling back to Google Gemini (gemini-2.5-flash)...`);
|
|
613
|
+
try {
|
|
614
|
+
return await handleGeminiRequest(__spreadProps(__spreadValues({}, req), { model: "gemini-2.5-flash" }), effectiveKey);
|
|
615
|
+
} catch (e) {
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
if (provider !== "huggingface" && (process.env.HF_TOKEN || process.env.HUGGINGFACE_API_KEY)) {
|
|
619
|
+
console.warn(`[LLM Gateway] Provider ${provider} rate limited (${error.message}). Falling back to Hugging Face (Qwen/Qwen2.5-Coder-32B-Instruct)...`);
|
|
620
|
+
try {
|
|
621
|
+
return await handleHuggingFaceChatRequest(__spreadProps(__spreadValues({}, req), { model: "Qwen/Qwen2.5-Coder-32B-Instruct" }), effectiveKey);
|
|
622
|
+
} catch (e) {
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
if (provider !== "openai" && process.env.OPENAI_API_KEY) {
|
|
626
|
+
console.warn(`[LLM Gateway] Provider ${provider} rate limited (${error.message}). Falling back to OpenAI (gpt-4o-mini)...`);
|
|
627
|
+
try {
|
|
628
|
+
return await handleOpenAIRequest(__spreadProps(__spreadValues({}, req), { model: "gpt-4o-mini" }), effectiveKey);
|
|
629
|
+
} catch (e) {
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
console.warn(`[LLM Gateway] Provider ${provider} rate limited (${error.message}). Falling back to local Ollama (llama3.2)...`);
|
|
633
|
+
return handleOllamaRequest(__spreadProps(__spreadValues({}, req), { model: "llama3.2" }));
|
|
634
|
+
}
|
|
635
|
+
throw error;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
async function dispatchEmbedding(req, apiKeyOverride) {
|
|
639
|
+
const effectiveKey = cleanApiKeyOverride(apiKeyOverride);
|
|
640
|
+
if (req.model.startsWith("hf/") || req.model.startsWith("huggingface/") || req.model.includes("bge-")) {
|
|
641
|
+
try {
|
|
642
|
+
return await handleHuggingFaceEmbedding(req, effectiveKey);
|
|
643
|
+
} catch (err) {
|
|
644
|
+
console.warn("[LLM Gateway] HuggingFace explicit model embedding failed, falling back to default gateway chain:", err);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
const geminiKey = process.env.GEMINI_API_KEY || effectiveKey;
|
|
648
|
+
const isGeminiKeyValid = Boolean(geminiKey && (geminiKey.startsWith("AIza") || !geminiKey.startsWith("AQ.")));
|
|
649
|
+
if (isGeminiKeyValid) {
|
|
650
|
+
try {
|
|
651
|
+
return await handleGeminiEmbedding(req, effectiveKey);
|
|
652
|
+
} catch (err) {
|
|
653
|
+
console.warn("[LLM Gateway] Gemini embedding failed:", err);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
if (process.env.OPENAI_API_KEY) {
|
|
657
|
+
try {
|
|
658
|
+
return await handleOpenAIEmbedding(req, effectiveKey);
|
|
659
|
+
} catch (err) {
|
|
660
|
+
console.warn("[LLM Gateway] OpenAI embedding failed:", err);
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
if (process.env.HUGGINGFACE_API_KEY || process.env.HF_TOKEN) {
|
|
664
|
+
try {
|
|
665
|
+
return await handleHuggingFaceEmbedding(req, effectiveKey);
|
|
666
|
+
} catch (err) {
|
|
667
|
+
console.warn("[LLM Gateway] HuggingFace fallback embedding failed:", err);
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
return handleGeminiEmbedding(req, effectiveKey);
|
|
671
|
+
}
|
|
672
|
+
var SUPPORTED_MODELS;
|
|
673
|
+
var init_router = __esm({
|
|
674
|
+
"../../services/llm-gateway/router.ts"() {
|
|
675
|
+
"use strict";
|
|
676
|
+
init_groq();
|
|
677
|
+
init_openai();
|
|
678
|
+
init_gemini();
|
|
679
|
+
init_huggingface();
|
|
680
|
+
init_anthropic();
|
|
681
|
+
init_ollama();
|
|
682
|
+
SUPPORTED_MODELS = [
|
|
683
|
+
{ id: "groq/llama-3.1-8b-instant", object: "model", created: 17e8, owned_by: "groq" },
|
|
684
|
+
{ id: "llama-3.1-8b-instant", object: "model", created: 17e8, owned_by: "groq" },
|
|
685
|
+
{ id: "groq/llama-3.3-70b-versatile", object: "model", created: 17e8, owned_by: "groq" },
|
|
686
|
+
{ id: "llama-3.3-70b-versatile", object: "model", created: 17e8, owned_by: "groq" },
|
|
687
|
+
{ id: "gpt-4o-mini", object: "model", created: 17e8, owned_by: "openai" },
|
|
688
|
+
{ id: "openai/gpt-4o-mini", object: "model", created: 17e8, owned_by: "openai" },
|
|
689
|
+
{ id: "gpt-4o", object: "model", created: 17e8, owned_by: "openai" },
|
|
690
|
+
{ id: "gemini-2.5-flash", object: "model", created: 17e8, owned_by: "google" },
|
|
691
|
+
{ id: "google/gemini-2.5-flash", object: "model", created: 17e8, owned_by: "google" },
|
|
692
|
+
{ id: "claude-3-5-haiku-20241022", object: "model", created: 17e8, owned_by: "anthropic" },
|
|
693
|
+
{ id: "anthropic/claude-3-5-haiku-20241022", object: "model", created: 17e8, owned_by: "anthropic" },
|
|
694
|
+
{ id: "text-embedding-004", object: "model", created: 17e8, owned_by: "google" },
|
|
695
|
+
{ id: "google/text-embedding-004", object: "model", created: 17e8, owned_by: "google" },
|
|
696
|
+
{ id: "BAAI/bge-base-en-v1.5", object: "model", created: 17e8, owned_by: "huggingface" },
|
|
697
|
+
{ id: "huggingface/BAAI/bge-base-en-v1.5", object: "model", created: 17e8, owned_by: "huggingface" },
|
|
698
|
+
{ id: "groq/qwen3-32b", object: "model", created: 17e8, owned_by: "groq" },
|
|
699
|
+
{ id: "qwen/qwen3-32b", object: "model", created: 17e8, owned_by: "groq" },
|
|
700
|
+
{ id: "qwen/qwen3-embedding-8b", object: "model", created: 17e8, owned_by: "qwen" },
|
|
701
|
+
{ id: "qwen3-embedding-8b", object: "model", created: 17e8, owned_by: "qwen" },
|
|
702
|
+
{ id: "ollama/llama3.2", object: "model", created: 17e8, owned_by: "ollama" }
|
|
703
|
+
];
|
|
704
|
+
if (typeof globalThis !== "undefined") {
|
|
705
|
+
const _g2 = globalThis;
|
|
706
|
+
_g2.__retrivoraDispatchChat = dispatchChatCompletion;
|
|
707
|
+
_g2.__retrivoraDispatchEmbedding = dispatchEmbedding;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
});
|
|
711
|
+
|
|
109
712
|
// src/providers/vectordb/BaseVectorProvider.ts
|
|
110
713
|
var BaseVectorProvider;
|
|
111
714
|
var init_BaseVectorProvider = __esm({
|
|
@@ -3841,6 +4444,17 @@ var LLM_PROFILES = {
|
|
|
3841
4444
|
};
|
|
3842
4445
|
|
|
3843
4446
|
// src/llm/providers/UniversalLLMAdapter.ts
|
|
4447
|
+
function extractContent(obj) {
|
|
4448
|
+
var _a2, _b, _c;
|
|
4449
|
+
if (!obj || typeof obj !== "object") return void 0;
|
|
4450
|
+
const choice = (_a2 = obj.choices) == null ? void 0 : _a2[0];
|
|
4451
|
+
if (!choice) return void 0;
|
|
4452
|
+
if (typeof ((_b = choice.message) == null ? void 0 : _b.content) === "string") return choice.message.content;
|
|
4453
|
+
if (typeof ((_c = choice.delta) == null ? void 0 : _c.content) === "string") return choice.delta.content;
|
|
4454
|
+
if (typeof choice.text === "string") return choice.text;
|
|
4455
|
+
if (typeof choice.content === "string") return choice.content;
|
|
4456
|
+
return void 0;
|
|
4457
|
+
}
|
|
3844
4458
|
var UniversalLLMAdapter = class {
|
|
3845
4459
|
constructor(config) {
|
|
3846
4460
|
var _a2, _b, _c, _d, _e, _f, _g2, _h;
|
|
@@ -3872,7 +4486,7 @@ var UniversalLLMAdapter = class {
|
|
|
3872
4486
|
});
|
|
3873
4487
|
}
|
|
3874
4488
|
async chat(messages, context) {
|
|
3875
|
-
var _a2, _b, _c
|
|
4489
|
+
var _a2, _b, _c;
|
|
3876
4490
|
const path2 = (_a2 = this.opts.chatPath) != null ? _a2 : "/chat/completions";
|
|
3877
4491
|
const formattedMessages = [
|
|
3878
4492
|
{ role: "system", content: `${this.systemPrompt}
|
|
@@ -3900,25 +4514,41 @@ ${context != null ? context : "None"}` },
|
|
|
3900
4514
|
const isSelfHost = this.baseUrl.includes("retrivora.com") || this.baseUrl.includes("localhost");
|
|
3901
4515
|
if (isSelfHost || Boolean(process.env.VERCEL)) {
|
|
3902
4516
|
const _g2 = globalThis;
|
|
3903
|
-
|
|
4517
|
+
let dispatch = _g2.__retrivoraDispatchChat;
|
|
4518
|
+
if (typeof dispatch !== "function") {
|
|
3904
4519
|
try {
|
|
3905
|
-
const
|
|
4520
|
+
const gateway = await Promise.resolve().then(() => (init_router(), router_exports));
|
|
4521
|
+
if (gateway == null ? void 0 : gateway.dispatchChatCompletion) {
|
|
4522
|
+
_g2.__retrivoraDispatchChat = gateway.dispatchChatCompletion;
|
|
4523
|
+
_g2.__retrivoraDispatchEmbedding = gateway.dispatchEmbedding;
|
|
4524
|
+
dispatch = gateway.dispatchChatCompletion;
|
|
4525
|
+
}
|
|
4526
|
+
} catch (e) {
|
|
4527
|
+
}
|
|
4528
|
+
}
|
|
4529
|
+
if (typeof dispatch === "function") {
|
|
4530
|
+
try {
|
|
4531
|
+
const res = await dispatch({
|
|
3906
4532
|
model: this.model,
|
|
3907
4533
|
messages: formattedMessages,
|
|
3908
4534
|
max_tokens: this.maxTokens,
|
|
3909
4535
|
temperature: this.temperature
|
|
3910
4536
|
}, this.apiKey);
|
|
3911
|
-
|
|
3912
|
-
|
|
4537
|
+
const content = extractContent(res == null ? void 0 : res.response);
|
|
4538
|
+
if (content !== void 0) {
|
|
4539
|
+
return content;
|
|
3913
4540
|
}
|
|
4541
|
+
throw new Error(`[UniversalLLMAdapter] In-process dispatch returned empty content for model: ${this.model}`);
|
|
3914
4542
|
} catch (dispatchErr) {
|
|
3915
4543
|
throw dispatchErr;
|
|
3916
4544
|
}
|
|
4545
|
+
} else {
|
|
4546
|
+
throw new Error(`[UniversalLLMAdapter] In-process gateway dispatch not registered. Direct self-referential HTTP calls to ${this.baseUrl} are disabled on Vercel to prevent HTTP 508 Loop Detected.`);
|
|
3917
4547
|
}
|
|
3918
4548
|
}
|
|
3919
4549
|
const { data } = await this.http.post(path2, payload);
|
|
3920
|
-
const extractPath = (
|
|
3921
|
-
const result = resolvePath(data, extractPath);
|
|
4550
|
+
const extractPath = (_b = this.opts.responseExtractPath) != null ? _b : "choices[0].message.content";
|
|
4551
|
+
const result = (_c = resolvePath(data, extractPath)) != null ? _c : extractContent(data);
|
|
3922
4552
|
if (result === void 0) {
|
|
3923
4553
|
throw new Error(`[UniversalLLMAdapter] Could not extract text from path '${extractPath}' in response.`);
|
|
3924
4554
|
}
|
|
@@ -3931,7 +4561,7 @@ ${context != null ? context : "None"}` },
|
|
|
3931
4561
|
*/
|
|
3932
4562
|
chatStream(messages, context) {
|
|
3933
4563
|
return __asyncGenerator(this, null, function* () {
|
|
3934
|
-
var _a2, _b, _c, _d, _e
|
|
4564
|
+
var _a2, _b, _c, _d, _e;
|
|
3935
4565
|
const path2 = (_a2 = this.opts.chatPath) != null ? _a2 : "/chat/completions";
|
|
3936
4566
|
const url = `${this.baseUrl.replace(/\/$/, "")}${path2}`;
|
|
3937
4567
|
const extractPath = ((_b = this.opts.responseExtractPath) != null ? _b : "choices[0].message.content").replace("message.content", "delta.content");
|
|
@@ -3965,10 +4595,22 @@ ${context != null ? context : "None"}` },
|
|
|
3965
4595
|
const isSelfHost = this.baseUrl.includes("retrivora.com") || this.baseUrl.includes("localhost");
|
|
3966
4596
|
let streamBody = null;
|
|
3967
4597
|
if (isSelfHost || Boolean(process.env.VERCEL)) {
|
|
3968
|
-
const
|
|
3969
|
-
|
|
4598
|
+
const _g2 = globalThis;
|
|
4599
|
+
let dispatch = _g2.__retrivoraDispatchChat;
|
|
4600
|
+
if (typeof dispatch !== "function") {
|
|
4601
|
+
try {
|
|
4602
|
+
const gateway = yield new __await(Promise.resolve().then(() => (init_router(), router_exports)));
|
|
4603
|
+
if (gateway == null ? void 0 : gateway.dispatchChatCompletion) {
|
|
4604
|
+
_g2.__retrivoraDispatchChat = gateway.dispatchChatCompletion;
|
|
4605
|
+
_g2.__retrivoraDispatchEmbedding = gateway.dispatchEmbedding;
|
|
4606
|
+
dispatch = gateway.dispatchChatCompletion;
|
|
4607
|
+
}
|
|
4608
|
+
} catch (e) {
|
|
4609
|
+
}
|
|
4610
|
+
}
|
|
4611
|
+
if (typeof dispatch === "function") {
|
|
3970
4612
|
try {
|
|
3971
|
-
const res = yield new __await(
|
|
4613
|
+
const res = yield new __await(dispatch({
|
|
3972
4614
|
model: this.model,
|
|
3973
4615
|
messages: formattedMessages,
|
|
3974
4616
|
max_tokens: this.maxTokens,
|
|
@@ -3977,13 +4619,19 @@ ${context != null ? context : "None"}` },
|
|
|
3977
4619
|
}, this.apiKey));
|
|
3978
4620
|
if (res == null ? void 0 : res.stream) {
|
|
3979
4621
|
streamBody = res.stream;
|
|
3980
|
-
} else
|
|
3981
|
-
|
|
3982
|
-
|
|
4622
|
+
} else {
|
|
4623
|
+
const content = extractContent(res == null ? void 0 : res.response);
|
|
4624
|
+
if (content !== void 0) {
|
|
4625
|
+
yield content;
|
|
4626
|
+
return;
|
|
4627
|
+
}
|
|
4628
|
+
throw new Error(`[UniversalLLMAdapter] In-process dispatch stream returned empty response for model: ${this.model}`);
|
|
3983
4629
|
}
|
|
3984
4630
|
} catch (dispatchErr) {
|
|
3985
4631
|
throw dispatchErr;
|
|
3986
4632
|
}
|
|
4633
|
+
} else {
|
|
4634
|
+
throw new Error(`[UniversalLLMAdapter] In-process gateway dispatch not registered. Direct self-referential HTTP calls to ${this.baseUrl} are disabled on Vercel to prevent HTTP 508 Loop Detected.`);
|
|
3987
4635
|
}
|
|
3988
4636
|
}
|
|
3989
4637
|
if (!streamBody) {
|
|
@@ -4010,14 +4658,14 @@ ${context != null ? context : "None"}` },
|
|
|
4010
4658
|
if (done) break;
|
|
4011
4659
|
buffer += decoder.decode(value, { stream: true });
|
|
4012
4660
|
const lines = buffer.split("\n");
|
|
4013
|
-
buffer = (
|
|
4661
|
+
buffer = (_c = lines.pop()) != null ? _c : "";
|
|
4014
4662
|
for (const line of lines) {
|
|
4015
4663
|
const trimmed = line.trim();
|
|
4016
4664
|
if (!trimmed || trimmed === "data: [DONE]") continue;
|
|
4017
4665
|
if (!trimmed.startsWith("data:")) continue;
|
|
4018
4666
|
try {
|
|
4019
4667
|
const json = JSON.parse(trimmed.slice(5).trim());
|
|
4020
|
-
const text = resolvePath(json, extractPath);
|
|
4668
|
+
const text = (_d = resolvePath(json, extractPath)) != null ? _d : extractContent(json);
|
|
4021
4669
|
if (text && typeof text === "string") yield text;
|
|
4022
4670
|
} catch (e) {
|
|
4023
4671
|
}
|
|
@@ -4027,7 +4675,7 @@ ${context != null ? context : "None"}` },
|
|
|
4027
4675
|
const jsonStr = buffer.replace(/^data:\s*/, "").trim();
|
|
4028
4676
|
try {
|
|
4029
4677
|
const json = JSON.parse(jsonStr);
|
|
4030
|
-
const text = resolvePath(json, extractPath);
|
|
4678
|
+
const text = (_e = resolvePath(json, extractPath)) != null ? _e : extractContent(json);
|
|
4031
4679
|
if (text && typeof text === "string") yield text;
|
|
4032
4680
|
} catch (e) {
|
|
4033
4681
|
}
|
|
@@ -4055,18 +4703,33 @@ ${context != null ? context : "None"}` },
|
|
|
4055
4703
|
const isSelfHost = this.baseUrl.includes("retrivora.com") || this.baseUrl.includes("localhost");
|
|
4056
4704
|
if (isSelfHost || Boolean(process.env.VERCEL)) {
|
|
4057
4705
|
const _g2 = globalThis;
|
|
4058
|
-
|
|
4706
|
+
let dispatch = _g2.__retrivoraDispatchEmbedding;
|
|
4707
|
+
if (typeof dispatch !== "function") {
|
|
4059
4708
|
try {
|
|
4060
|
-
const
|
|
4709
|
+
const gateway = await Promise.resolve().then(() => (init_router(), router_exports));
|
|
4710
|
+
if (gateway == null ? void 0 : gateway.dispatchEmbedding) {
|
|
4711
|
+
_g2.__retrivoraDispatchChat = gateway.dispatchChatCompletion;
|
|
4712
|
+
_g2.__retrivoraDispatchEmbedding = gateway.dispatchEmbedding;
|
|
4713
|
+
dispatch = gateway.dispatchEmbedding;
|
|
4714
|
+
}
|
|
4715
|
+
} catch (e) {
|
|
4716
|
+
}
|
|
4717
|
+
}
|
|
4718
|
+
if (typeof dispatch === "function") {
|
|
4719
|
+
try {
|
|
4720
|
+
const res = await dispatch({
|
|
4061
4721
|
model: this.model,
|
|
4062
4722
|
input: text
|
|
4063
4723
|
}, this.apiKey);
|
|
4064
4724
|
if ((_c = (_b = res == null ? void 0 : res.data) == null ? void 0 : _b[0]) == null ? void 0 : _c.embedding) {
|
|
4065
4725
|
return res.data[0].embedding;
|
|
4066
4726
|
}
|
|
4727
|
+
throw new Error(`[UniversalLLMAdapter] In-process embedding dispatch returned invalid vector for model: ${this.model}`);
|
|
4067
4728
|
} catch (dispatchErr) {
|
|
4068
4729
|
throw dispatchErr;
|
|
4069
4730
|
}
|
|
4731
|
+
} else {
|
|
4732
|
+
throw new Error(`[UniversalLLMAdapter] In-process gateway dispatch not registered. Direct self-referential HTTP calls to ${this.baseUrl} are disabled on Vercel to prevent HTTP 508 Loop Detected.`);
|
|
4070
4733
|
}
|
|
4071
4734
|
}
|
|
4072
4735
|
const { data } = await this.http.post(path2, payload);
|
|
@@ -6914,7 +7577,7 @@ ${schemaProfileText}` : ""}`;
|
|
|
6914
7577
|
}
|
|
6915
7578
|
// ─── Transform Helpers ────────────────────────────────────────────────────
|
|
6916
7579
|
static transformToProductCarousel(data, config, trainedSchema) {
|
|
6917
|
-
const products = data.map((item) => this.extractProductInfo(item, config, trainedSchema)).filter((p) => p !== null)
|
|
7580
|
+
const products = data.map((item) => this.extractProductInfo(item, config, trainedSchema)).filter((p) => p !== null);
|
|
6918
7581
|
return {
|
|
6919
7582
|
type: "product_carousel",
|
|
6920
7583
|
title: "Recommended Products",
|
|
@@ -7294,7 +7957,8 @@ ${schemaProfileText}` : ""}`;
|
|
|
7294
7957
|
return fieldCount.size > 2;
|
|
7295
7958
|
}
|
|
7296
7959
|
static profileData(data) {
|
|
7297
|
-
const records = data.map((item) => {
|
|
7960
|
+
const records = (data || []).filter((item) => Boolean(item && typeof item === "object")).map((item) => {
|
|
7961
|
+
var _a2, _b;
|
|
7298
7962
|
const fields2 = {};
|
|
7299
7963
|
Object.entries(item.metadata || {}).forEach(([key, value]) => {
|
|
7300
7964
|
const primitive = this.toPrimitive(value);
|
|
@@ -7303,8 +7967,8 @@ ${schemaProfileText}` : ""}`;
|
|
|
7303
7967
|
if (!fields2.content && item.content) fields2.content = item.content.substring(0, 500);
|
|
7304
7968
|
return {
|
|
7305
7969
|
id: item.id,
|
|
7306
|
-
content: item.content,
|
|
7307
|
-
score: item.score,
|
|
7970
|
+
content: (_a2 = item.content) != null ? _a2 : "",
|
|
7971
|
+
score: (_b = item.score) != null ? _b : 0,
|
|
7308
7972
|
fields: fields2,
|
|
7309
7973
|
source: item
|
|
7310
7974
|
};
|
|
@@ -8407,7 +9071,7 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
8407
9071
|
*/
|
|
8408
9072
|
askStreamInternal(_0) {
|
|
8409
9073
|
return __asyncGenerator(this, arguments, function* (question, history = [], namespace) {
|
|
8410
|
-
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B
|
|
9074
|
+
var _a2, _b, _c, _d, _e, _f, _g2, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
|
|
8411
9075
|
yield new __await(this.initialize());
|
|
8412
9076
|
const ns = formatNamespace(namespace != null ? namespace : this.config.projectId);
|
|
8413
9077
|
const topK = (_b = (_a2 = this.config.rag) == null ? void 0 : _a2.topK) != null ? _b : 5;
|
|
@@ -8450,8 +9114,8 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
8450
9114
|
const graphData = (strategyResult === "graph" || strategyResult === "both") && this.graphDB && ((_l = this.config.rag) == null ? void 0 : _l.useGraphRetrieval) ? yield new __await(this.graphDB.query(searchQuery)) : void 0;
|
|
8451
9115
|
const hasMetadataFilter = filter.metadata && Object.keys(filter.metadata).length > 0;
|
|
8452
9116
|
const hasNumericPredicates = Array.isArray(filter.__numericPredicates) && filter.__numericPredicates.length > 0;
|
|
8453
|
-
const wantsExhaustiveList =
|
|
8454
|
-
const retrievalLimit =
|
|
9117
|
+
const wantsExhaustiveList = true;
|
|
9118
|
+
const retrievalLimit = Math.max(topK * 50, 1e3);
|
|
8455
9119
|
const rawSources = ((strategyResult === "vector" || strategyResult === "both") && queryVector && queryVector.length > 0 ? yield new __await(this.vectorDB.query(queryVector, retrievalLimit, ns, filter)) : []).filter((s) => Boolean(s && typeof s === "object"));
|
|
8456
9120
|
console.log("[Pipeline] Raw vectorDB.query response:", {
|
|
8457
9121
|
rawCount: rawSources.length,
|
|
@@ -8475,40 +9139,29 @@ ${m.content}`).join("\n\n---\n\n");
|
|
|
8475
9139
|
var _a3;
|
|
8476
9140
|
return ((_a3 = m == null ? void 0 : m.score) != null ? _a3 : 0) >= scoreThreshold;
|
|
8477
9141
|
});
|
|
8478
|
-
const rerankLimit =
|
|
9142
|
+
const rerankLimit = Math.max(retrievalLimit, fullSources.length);
|
|
8479
9143
|
const useReranking = arch !== "naive" && (arch === "retrieve-and-rerank" || ((_m = this.config.rag) == null ? void 0 : _m.useReranking));
|
|
8480
9144
|
if (!hasNumericPredicates && useReranking) {
|
|
8481
9145
|
fullSources = (yield new __await(this.reranker.rerank(fullSources, question, rerankLimit))).filter((s) => Boolean(s && typeof s === "object"));
|
|
8482
|
-
} else if (!wantsExhaustiveList) {
|
|
8483
|
-
fullSources = fullSources.slice(0, topK);
|
|
8484
9146
|
}
|
|
8485
9147
|
const rerankMs = performance.now() - rerankStart;
|
|
8486
|
-
|
|
9148
|
+
const totalCount = fullSources.length;
|
|
9149
|
+
const promptSources = fullSources.slice(0, 15);
|
|
9150
|
+
let context = promptSources.length ? `[SEARCH SUMMARY: Found ${totalCount} matching record(s) out of ${rawSources.length} total resources searched in database. Context includes top ${promptSources.length} relevant records]
|
|
9151
|
+
|
|
9152
|
+
` + promptSources.map((m, i) => {
|
|
8487
9153
|
var _a3;
|
|
8488
9154
|
return `[Source ${i + 1}]
|
|
8489
9155
|
${(_a3 = m == null ? void 0 : m.content) != null ? _a3 : ""}`;
|
|
8490
9156
|
}).join("\n\n---\n\n") : "No relevant context found.";
|
|
8491
|
-
let displayCount = 15;
|
|
8492
|
-
if (hasMetadataFilter) {
|
|
8493
|
-
displayCount = fullSources.length;
|
|
8494
|
-
} else {
|
|
8495
|
-
const baseThreshold = Math.max(scoreThreshold, 0.4);
|
|
8496
|
-
const highlyRelevant = fullSources.filter((m) => {
|
|
8497
|
-
var _a3;
|
|
8498
|
-
return ((_a3 = m == null ? void 0 : m.score) != null ? _a3 : 0) >= baseThreshold;
|
|
8499
|
-
});
|
|
8500
|
-
displayCount = Math.max(highlyRelevant.length, topK);
|
|
8501
|
-
if (displayCount > 15) {
|
|
8502
|
-
displayCount = 15;
|
|
8503
|
-
}
|
|
8504
|
-
}
|
|
8505
9157
|
const sources = [...fullSources].filter((s) => Boolean(s && typeof s === "object")).sort((a, b) => {
|
|
8506
9158
|
var _a3, _b2;
|
|
8507
9159
|
return ((_a3 = b == null ? void 0 : b.score) != null ? _a3 : 0) - ((_b2 = a == null ? void 0 : a.score) != null ? _b2 : 0);
|
|
8508
|
-
})
|
|
9160
|
+
});
|
|
8509
9161
|
console.log("[Pipeline] Final sources for prompt & UI:", {
|
|
8510
9162
|
count: sources.length,
|
|
8511
|
-
|
|
9163
|
+
totalRawCount: rawSources.length,
|
|
9164
|
+
sources: sources.slice(0, 10).map((s, idx) => {
|
|
8512
9165
|
var _a3;
|
|
8513
9166
|
return {
|
|
8514
9167
|
rank: idx + 1,
|
|
@@ -8708,7 +9361,7 @@ ${context}`;
|
|
|
8708
9361
|
}
|
|
8709
9362
|
yield fullReply;
|
|
8710
9363
|
}
|
|
8711
|
-
const runHallucination = ((_w = this.config.llm.options) == null ? void 0 : _w.hallucinationScoring) === true
|
|
9364
|
+
const runHallucination = ((_w = this.config.llm.options) == null ? void 0 : _w.hallucinationScoring) === true;
|
|
8712
9365
|
if (runHallucination) {
|
|
8713
9366
|
hallucinationScoringPromise = scoreHallucination(this.llmProvider, fullReply, context).catch(() => void 0);
|
|
8714
9367
|
}
|
|
@@ -8717,7 +9370,7 @@ ${context}`;
|
|
|
8717
9370
|
const latency = {
|
|
8718
9371
|
embedMs: Math.round(embedMs),
|
|
8719
9372
|
retrieveMs: Math.round(retrieveMs),
|
|
8720
|
-
rerankMs: arch !== "naive" && (arch === "retrieve-and-rerank" || ((
|
|
9373
|
+
rerankMs: arch !== "naive" && (arch === "retrieve-and-rerank" || ((_x = this.config.rag) == null ? void 0 : _x.useReranking)) ? Math.round(rerankMs) : void 0,
|
|
8721
9374
|
generateMs: Math.round(generateMs),
|
|
8722
9375
|
totalMs: Math.round(totalMs)
|
|
8723
9376
|
};
|
|
@@ -8730,7 +9383,7 @@ ${context}`;
|
|
|
8730
9383
|
totalTokens: promptTokens + completionTokens,
|
|
8731
9384
|
estimatedCostUsd: estimateCostUsd(promptTokens, completionTokens, this.config.llm.model)
|
|
8732
9385
|
};
|
|
8733
|
-
const awaitHallucination = ((
|
|
9386
|
+
const awaitHallucination = ((_y = this.config.llm.options) == null ? void 0 : _y.awaitHallucination) === true;
|
|
8734
9387
|
const [uiTransformation, hallucinationResult] = yield new __await(Promise.all([
|
|
8735
9388
|
uiTransformationPromise,
|
|
8736
9389
|
awaitHallucination ? hallucinationScoringPromise : Promise.resolve(void 0)
|
|
@@ -8759,10 +9412,10 @@ ${context}`;
|
|
|
8759
9412
|
hallucinationReason: hScore.reason
|
|
8760
9413
|
} : {});
|
|
8761
9414
|
const trace = buildTrace(hallucinationResult);
|
|
8762
|
-
const isTelemetryActive = (
|
|
9415
|
+
const isTelemetryActive = (_A = (_z = this.config.telemetry) == null ? void 0 : _z.enabled) != null ? _A : Boolean(this.config.licenseKey);
|
|
8763
9416
|
if (isTelemetryActive) {
|
|
8764
9417
|
const defaultUrl = process.env.NODE_ENV === "development" && !process.env.TELEMETRY_URL && !process.env.NEXT_PUBLIC_TELEMETRY_URL ? "http://localhost:3001/api/telemetry" : "https://retrivora.com/api/telemetry";
|
|
8765
|
-
const telemetryUrl = ((
|
|
9418
|
+
const telemetryUrl = ((_B = this.config.telemetry) == null ? void 0 : _B.url) || process.env.TELEMETRY_URL || process.env.NEXT_PUBLIC_TELEMETRY_URL || defaultUrl;
|
|
8766
9419
|
const absoluteUrl = telemetryUrl.startsWith("http") ? telemetryUrl : (process.env.NEXT_PUBLIC_APP_URL || `http://localhost:${process.env.PORT || 3e3}`) + telemetryUrl;
|
|
8767
9420
|
(async () => {
|
|
8768
9421
|
try {
|
|
@@ -8821,9 +9474,8 @@ ${context}`;
|
|
|
8821
9474
|
{ visualizationHint: "product_browse", recommendedChart: "text", filterInStockOnly: false, wantsExplicitTable: false, isTemporal: false, isComparison: false, language: "en" }
|
|
8822
9475
|
);
|
|
8823
9476
|
}
|
|
8824
|
-
const
|
|
8825
|
-
|
|
8826
|
-
if (forceDeterministic || isLocalProvider || disableLlmUiTransform) {
|
|
9477
|
+
const enableLlmUiTransform = ((_a2 = this.config.llm.options) == null ? void 0 : _a2.enableLlmUiTransform) === true;
|
|
9478
|
+
if (forceDeterministic || !enableLlmUiTransform) {
|
|
8827
9479
|
return UITransformer.transform(question, sources, this.config, cachedSchema);
|
|
8828
9480
|
}
|
|
8829
9481
|
try {
|