@retrivora-ai/rag-engine 0.1.3 → 0.1.4
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/ChromaDBProvider-QNI7UCX4.mjs +8 -0
- package/dist/DocumentChunker-BQ5kQD7B.d.ts +155 -0
- package/dist/DocumentChunker-D9-fObJp.d.mts +155 -0
- package/dist/LLMFactory-XC55FTD2.mjs +7 -0
- package/dist/MilvusProvider-OO6QGZDZ.mjs +8 -0
- package/dist/MongoDBProvider-WWVJG3WT.mjs +8 -0
- package/dist/PineconeProvider-NJ675H7U.mjs +8 -0
- package/dist/{RAGPipeline-BmkIv1HD.d.mts → Pipeline-Bo6CUCox.d.mts} +76 -159
- package/dist/{RAGPipeline-BmkIv1HD.d.ts → Pipeline-Bo6CUCox.d.ts} +76 -159
- package/dist/PostgreSQLProvider-ISNMD3BE.mjs +8 -0
- package/dist/QdrantProvider-LJWOIGES.mjs +8 -0
- package/dist/RedisProvider-ASONNYBI.mjs +8 -0
- package/dist/WeaviateProvider-PSDCUGC7.mjs +8 -0
- package/dist/chunk-6FODXNUF.mjs +91 -0
- package/dist/chunk-7NXI6ZWX.mjs +89 -0
- package/dist/chunk-AALIF3AL.mjs +91 -0
- package/dist/chunk-AIAB2IEE.mjs +394 -0
- package/dist/chunk-FGGSVVSY.mjs +162 -0
- package/dist/chunk-GD3QJFNN.mjs +424 -0
- package/dist/chunk-HUGLYKD6.mjs +84 -0
- package/dist/chunk-I4E63NIC.mjs +24 -0
- package/dist/chunk-QEYVWVT5.mjs +102 -0
- package/dist/chunk-S5DRHETN.mjs +110 -0
- package/dist/chunk-V75V7BT2.mjs +117 -0
- package/dist/chunk-VOIWNO5O.mjs +11 -0
- package/dist/chunk-VPNRDXIA.mjs +74 -0
- package/dist/handlers/index.d.mts +9 -33
- package/dist/handlers/index.d.ts +9 -33
- package/dist/handlers/index.js +1477 -962
- package/dist/handlers/index.mjs +4 -2
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1646 -19
- package/dist/index.mjs +14 -2
- package/dist/server.d.mts +93 -56
- package/dist/server.d.ts +93 -56
- package/dist/server.js +1531 -1099
- package/dist/server.mjs +56 -131
- package/package.json +1 -1
- package/src/app/page.tsx +1 -1
- package/src/components/MessageBubble.tsx +1 -1
- package/src/components/SourceCard.tsx +1 -1
- package/src/config/RagConfig.ts +1 -1
- package/src/config/serverConfig.ts +2 -2
- package/src/core/ConfigResolver.ts +69 -0
- package/src/core/Pipeline.ts +109 -0
- package/src/core/ProviderRegistry.ts +71 -0
- package/src/core/VectorPlugin.ts +51 -0
- package/src/handlers/index.ts +21 -100
- package/src/hooks/useRagChat.ts +1 -1
- package/src/index.ts +8 -6
- package/src/providers/vectordb/BaseVectorProvider.ts +61 -0
- package/src/providers/vectordb/ChromaDBProvider.ts +94 -0
- package/src/providers/vectordb/MilvusProvider.ts +100 -0
- package/src/providers/vectordb/MongoDBProvider.ts +118 -0
- package/src/{vectordb/adapters/PineconeAdapter.ts → providers/vectordb/PineconeProvider.ts} +21 -56
- package/src/{vectordb/adapters/PgVectorAdapter.ts → providers/vectordb/PostgreSQLProvider.ts} +23 -58
- package/src/providers/vectordb/QdrantProvider.ts +95 -0
- package/src/providers/vectordb/RedisProvider.ts +84 -0
- package/src/providers/vectordb/WeaviateProvider.ts +124 -0
- package/src/server.ts +16 -8
- package/src/test-refactor.ts +59 -0
- package/src/types/index.ts +13 -0
- package/src/utils/templateUtils.ts +6 -6
- package/dist/DocumentChunker-BUrIrcPk.d.mts +0 -43
- package/dist/DocumentChunker-BUrIrcPk.d.ts +0 -43
- package/dist/chunk-NVOMLHXW.mjs +0 -1259
- package/dist/chunk-ZPXLQR5Q.mjs +0 -67
- package/src/rag/RAGPipeline.ts +0 -200
- package/src/vectordb/IVectorDB.ts +0 -75
- package/src/vectordb/VectorDBFactory.ts +0 -41
- package/src/vectordb/adapters/MongoDbAdapter.ts +0 -175
- package/src/vectordb/adapters/UniversalVectorDBAdapter.ts +0 -177
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__spreadProps,
|
|
3
|
+
__spreadValues
|
|
4
|
+
} from "./chunk-I4E63NIC.mjs";
|
|
5
|
+
|
|
6
|
+
// src/llm/providers/OpenAIProvider.ts
|
|
7
|
+
import OpenAI from "openai";
|
|
8
|
+
var OpenAIProvider = class {
|
|
9
|
+
constructor(llmConfig, embeddingConfig) {
|
|
10
|
+
if (!llmConfig.apiKey) throw new Error("[OpenAIProvider] llmConfig.apiKey is required");
|
|
11
|
+
this.client = new OpenAI({ apiKey: llmConfig.apiKey });
|
|
12
|
+
this.llmConfig = llmConfig;
|
|
13
|
+
this.embeddingConfig = embeddingConfig;
|
|
14
|
+
}
|
|
15
|
+
async chat(messages, context, options) {
|
|
16
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
17
|
+
const systemContent = (_a = this.llmConfig.systemPrompt) != null ? _a : `You are a helpful assistant. Answer questions based on the provided context.
|
|
18
|
+
|
|
19
|
+
Context:
|
|
20
|
+
${context}`;
|
|
21
|
+
const systemMessage = {
|
|
22
|
+
role: "system",
|
|
23
|
+
content: systemContent.includes("{{context}}") ? systemContent.replace("{{context}}", context) : `${systemContent}
|
|
24
|
+
|
|
25
|
+
Context:
|
|
26
|
+
${context}`
|
|
27
|
+
};
|
|
28
|
+
const formattedMessages = [
|
|
29
|
+
systemMessage,
|
|
30
|
+
...messages.map((m) => ({
|
|
31
|
+
role: m.role,
|
|
32
|
+
content: m.content
|
|
33
|
+
}))
|
|
34
|
+
];
|
|
35
|
+
const completion = await this.client.chat.completions.create({
|
|
36
|
+
model: this.llmConfig.model,
|
|
37
|
+
messages: formattedMessages,
|
|
38
|
+
max_tokens: (_c = (_b = options == null ? void 0 : options.maxTokens) != null ? _b : this.llmConfig.maxTokens) != null ? _c : 1024,
|
|
39
|
+
temperature: (_e = (_d = options == null ? void 0 : options.temperature) != null ? _d : this.llmConfig.temperature) != null ? _e : 0.7,
|
|
40
|
+
stop: options == null ? void 0 : options.stop
|
|
41
|
+
});
|
|
42
|
+
return (_h = (_g = (_f = completion.choices[0]) == null ? void 0 : _f.message) == null ? void 0 : _g.content) != null ? _h : "";
|
|
43
|
+
}
|
|
44
|
+
async embed(text, options) {
|
|
45
|
+
const results = await this.batchEmbed([text], options);
|
|
46
|
+
return results[0];
|
|
47
|
+
}
|
|
48
|
+
async batchEmbed(texts, options) {
|
|
49
|
+
var _a, _b, _c, _d, _e;
|
|
50
|
+
const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (_a = this.embeddingConfig) == null ? void 0 : _a.model) != null ? _c : "text-embedding-3-small";
|
|
51
|
+
const apiKey = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.apiKey) != null ? _e : this.llmConfig.apiKey;
|
|
52
|
+
const client = apiKey !== this.llmConfig.apiKey ? new OpenAI({ apiKey }) : this.client;
|
|
53
|
+
const response = await client.embeddings.create({ model, input: texts });
|
|
54
|
+
return response.data.map((d) => d.embedding);
|
|
55
|
+
}
|
|
56
|
+
async ping() {
|
|
57
|
+
try {
|
|
58
|
+
await this.client.models.list();
|
|
59
|
+
return true;
|
|
60
|
+
} catch (err) {
|
|
61
|
+
console.error("[OpenAIProvider] Ping failed:", err);
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// src/llm/providers/AnthropicProvider.ts
|
|
68
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
69
|
+
var AnthropicProvider = class {
|
|
70
|
+
constructor(llmConfig, embeddingConfig) {
|
|
71
|
+
if (!llmConfig.apiKey) throw new Error("[AnthropicProvider] llmConfig.apiKey is required");
|
|
72
|
+
this.client = new Anthropic({ apiKey: llmConfig.apiKey });
|
|
73
|
+
this.llmConfig = llmConfig;
|
|
74
|
+
this.embeddingConfig = embeddingConfig;
|
|
75
|
+
}
|
|
76
|
+
async chat(messages, context, options) {
|
|
77
|
+
var _a, _b, _c;
|
|
78
|
+
const systemPrompt = (_a = this.llmConfig.systemPrompt) != null ? _a : `You are a helpful assistant. Use the context below to answer the user's question accurately.
|
|
79
|
+
|
|
80
|
+
Context:
|
|
81
|
+
${context}`;
|
|
82
|
+
const system = systemPrompt.includes("{{context}}") ? systemPrompt.replace("{{context}}", context) : `${systemPrompt}
|
|
83
|
+
|
|
84
|
+
Context:
|
|
85
|
+
${context}`;
|
|
86
|
+
const anthropicMessages = messages.map((m) => ({
|
|
87
|
+
role: m.role === "assistant" ? "assistant" : "user",
|
|
88
|
+
content: m.content
|
|
89
|
+
}));
|
|
90
|
+
const response = await this.client.messages.create({
|
|
91
|
+
model: this.llmConfig.model,
|
|
92
|
+
max_tokens: (_c = (_b = options == null ? void 0 : options.maxTokens) != null ? _b : this.llmConfig.maxTokens) != null ? _c : 1024,
|
|
93
|
+
system,
|
|
94
|
+
messages: anthropicMessages
|
|
95
|
+
});
|
|
96
|
+
const block = response.content[0];
|
|
97
|
+
return block.type === "text" ? block.text : "";
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Anthropic does not offer an embedding API.
|
|
101
|
+
* This method throws with a clear error so developers know to configure
|
|
102
|
+
* a separate embedding provider (OpenAI or Ollama).
|
|
103
|
+
*/
|
|
104
|
+
async embed(text, options) {
|
|
105
|
+
void text;
|
|
106
|
+
void options;
|
|
107
|
+
throw new Error(
|
|
108
|
+
'[AnthropicProvider] Anthropic does not provide an embedding API. Set embedding.provider to "openai" or "ollama" in your RagConfig.'
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
async batchEmbed(texts, options) {
|
|
112
|
+
void texts;
|
|
113
|
+
void options;
|
|
114
|
+
throw new Error(
|
|
115
|
+
"[AnthropicProvider] Anthropic does not provide an embedding API."
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
async ping() {
|
|
119
|
+
try {
|
|
120
|
+
await this.client.models.list();
|
|
121
|
+
return true;
|
|
122
|
+
} catch (err) {
|
|
123
|
+
console.error("[AnthropicProvider] Ping failed:", err);
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// src/llm/providers/OllamaProvider.ts
|
|
130
|
+
import axios from "axios";
|
|
131
|
+
var OllamaProvider = class {
|
|
132
|
+
constructor(llmConfig, embeddingConfig) {
|
|
133
|
+
var _a;
|
|
134
|
+
const baseURL = (_a = llmConfig.baseUrl) != null ? _a : "http://localhost:11434";
|
|
135
|
+
this.http = axios.create({ baseURL, timeout: 12e4 });
|
|
136
|
+
this.llmConfig = llmConfig;
|
|
137
|
+
this.embeddingConfig = embeddingConfig;
|
|
138
|
+
}
|
|
139
|
+
async chat(messages, context, options) {
|
|
140
|
+
var _a, _b, _c, _d, _e;
|
|
141
|
+
const systemPrompt = (_a = this.llmConfig.systemPrompt) != null ? _a : `You are a helpful assistant. Use the provided context to answer the user's question.
|
|
142
|
+
|
|
143
|
+
Context:
|
|
144
|
+
${context}`;
|
|
145
|
+
const system = systemPrompt.includes("{{context}}") ? systemPrompt.replace("{{context}}", context) : `${systemPrompt}
|
|
146
|
+
|
|
147
|
+
Context:
|
|
148
|
+
${context}`;
|
|
149
|
+
const { data } = await this.http.post("/api/chat", {
|
|
150
|
+
model: this.llmConfig.model,
|
|
151
|
+
stream: false,
|
|
152
|
+
options: {
|
|
153
|
+
temperature: (_c = (_b = options == null ? void 0 : options.temperature) != null ? _b : this.llmConfig.temperature) != null ? _c : 0.7,
|
|
154
|
+
num_predict: (_e = (_d = options == null ? void 0 : options.maxTokens) != null ? _d : this.llmConfig.maxTokens) != null ? _e : 1024
|
|
155
|
+
},
|
|
156
|
+
messages: [
|
|
157
|
+
{ role: "system", content: system },
|
|
158
|
+
...messages.map((m) => ({ role: m.role, content: m.content }))
|
|
159
|
+
]
|
|
160
|
+
});
|
|
161
|
+
return data.message.content;
|
|
162
|
+
}
|
|
163
|
+
async embed(text, options) {
|
|
164
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
165
|
+
const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (_a = this.embeddingConfig) == null ? void 0 : _a.model) != null ? _c : "nomic-embed-text";
|
|
166
|
+
const baseURL = (_f = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.baseUrl) != null ? _e : this.llmConfig.baseUrl) != null ? _f : "http://localhost:11434";
|
|
167
|
+
const client = baseURL !== ((_g = this.llmConfig.baseUrl) != null ? _g : "http://localhost:11434") ? axios.create({ baseURL, timeout: 6e4 }) : this.http;
|
|
168
|
+
const { data } = await client.post("/api/embeddings", {
|
|
169
|
+
model,
|
|
170
|
+
prompt: text
|
|
171
|
+
});
|
|
172
|
+
return data.embedding;
|
|
173
|
+
}
|
|
174
|
+
async batchEmbed(texts, options) {
|
|
175
|
+
const vectors = [];
|
|
176
|
+
for (const text of texts) {
|
|
177
|
+
vectors.push(await this.embed(text, options));
|
|
178
|
+
}
|
|
179
|
+
return vectors;
|
|
180
|
+
}
|
|
181
|
+
async ping() {
|
|
182
|
+
try {
|
|
183
|
+
await this.http.get("/api/tags");
|
|
184
|
+
return true;
|
|
185
|
+
} catch (err) {
|
|
186
|
+
console.error("[OllamaProvider] Ping failed:", err);
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
// src/llm/providers/UniversalLLMAdapter.ts
|
|
193
|
+
import axios2 from "axios";
|
|
194
|
+
|
|
195
|
+
// src/utils/templateUtils.ts
|
|
196
|
+
function isRecord(value) {
|
|
197
|
+
return typeof value === "object" && value !== null;
|
|
198
|
+
}
|
|
199
|
+
function resolvePath(obj, path) {
|
|
200
|
+
if (!path || !obj) return void 0;
|
|
201
|
+
return path.replace(/\[(\w+)\]/g, ".$1").replace(/^\./, "").split(".").reduce((current, segment) => {
|
|
202
|
+
if (!isRecord(current) && !Array.isArray(current)) {
|
|
203
|
+
return void 0;
|
|
204
|
+
}
|
|
205
|
+
return current[segment];
|
|
206
|
+
}, obj);
|
|
207
|
+
}
|
|
208
|
+
function buildPayload(template, vars) {
|
|
209
|
+
let raw = template;
|
|
210
|
+
for (const [key, val] of Object.entries(vars)) {
|
|
211
|
+
const stringifiedVal = val === void 0 ? "null" : JSON.stringify(val);
|
|
212
|
+
raw = raw.replace(new RegExp(`"\\{\\{${key}\\}\\}"`, "g"), stringifiedVal);
|
|
213
|
+
raw = raw.replace(new RegExp(`\\{\\{${key}\\}\\}`, "g"), stringifiedVal);
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
return JSON.parse(raw);
|
|
217
|
+
} catch (err) {
|
|
218
|
+
throw new Error(`Failed to parse payload template: ${err.message}
|
|
219
|
+
Raw payload: ${raw}`);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function mergeDefined(base, override) {
|
|
223
|
+
const merged = __spreadValues({}, base || {});
|
|
224
|
+
if (!override) {
|
|
225
|
+
return merged;
|
|
226
|
+
}
|
|
227
|
+
for (const [key, value] of Object.entries(override)) {
|
|
228
|
+
if (value !== void 0 && value !== null && value !== "") {
|
|
229
|
+
merged[key] = value;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return merged;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// src/config/UniversalProfiles.ts
|
|
236
|
+
var OPENAI_BASE = {
|
|
237
|
+
chatPath: "/chat/completions",
|
|
238
|
+
embedPath: "/embeddings",
|
|
239
|
+
responseExtractPath: "choices[0].message.content",
|
|
240
|
+
embedExtractPath: "data[0].embedding",
|
|
241
|
+
chatPayloadTemplate: '{"model":"{{model}}","messages":{{messages}},"max_tokens":{{maxTokens}},"temperature":{{temperature}}}'
|
|
242
|
+
};
|
|
243
|
+
var LLM_PROFILES = {
|
|
244
|
+
"openai-compatible": OPENAI_BASE,
|
|
245
|
+
"litellm": __spreadProps(__spreadValues({}, OPENAI_BASE), {
|
|
246
|
+
chatPayloadTemplate: '{"model":"{{model}}","messages":{{messages}},"max_tokens":{{maxTokens}},"temperature":{{temperature}},"stream":false}'
|
|
247
|
+
}),
|
|
248
|
+
"anthropic-claude": {
|
|
249
|
+
chatPath: "/v1/messages",
|
|
250
|
+
responseExtractPath: "content[0].text",
|
|
251
|
+
headers: { "anthropic-version": "2023-06-01" },
|
|
252
|
+
chatPayloadTemplate: '{"model":"{{model}}","messages":{{messages}},"max_tokens":{{maxTokens}}}'
|
|
253
|
+
},
|
|
254
|
+
"google-gemini": {
|
|
255
|
+
chatPath: "/v1beta/models/{{model}}:generateContent",
|
|
256
|
+
responseExtractPath: "candidates[0].content.parts[0].text",
|
|
257
|
+
chatPayloadTemplate: '{"contents":[{"parts":[{"text":"{{messages}}"}]}]}'
|
|
258
|
+
},
|
|
259
|
+
"github-copilot": __spreadProps(__spreadValues({}, OPENAI_BASE), {
|
|
260
|
+
chatPayloadTemplate: '{"model":"{{model}}","messages":{{messages}},"temperature":{{temperature}}}'
|
|
261
|
+
}),
|
|
262
|
+
"ollama-standard": {
|
|
263
|
+
chatPath: "/api/chat",
|
|
264
|
+
embedPath: "/api/embeddings",
|
|
265
|
+
responseExtractPath: "message.content",
|
|
266
|
+
embedExtractPath: "embedding",
|
|
267
|
+
chatPayloadTemplate: '{"model":"{{model}}","messages":{{messages}},"stream":false}',
|
|
268
|
+
embedPayloadTemplate: '{"model":"{{model}}","prompt":"{{input}}"}'
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
// src/llm/providers/UniversalLLMAdapter.ts
|
|
273
|
+
var UniversalLLMAdapter = class {
|
|
274
|
+
constructor(config) {
|
|
275
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
276
|
+
this.model = config.model;
|
|
277
|
+
const llmConfig = config;
|
|
278
|
+
const options = (_a = llmConfig.options) != null ? _a : {};
|
|
279
|
+
let profile = {};
|
|
280
|
+
if (typeof options.profile === "string") {
|
|
281
|
+
profile = LLM_PROFILES[options.profile] || {};
|
|
282
|
+
} else if (typeof options.profile === "object" && options.profile !== null) {
|
|
283
|
+
profile = options.profile;
|
|
284
|
+
}
|
|
285
|
+
this.opts = __spreadValues(__spreadValues({}, profile), (_b = llmConfig.options) != null ? _b : {});
|
|
286
|
+
this.systemPrompt = (_c = llmConfig.systemPrompt) != null ? _c : "You are a helpful AI assistant. Use the provided context to answer the user.";
|
|
287
|
+
this.maxTokens = (_d = llmConfig.maxTokens) != null ? _d : 1024;
|
|
288
|
+
this.temperature = (_e = llmConfig.temperature) != null ? _e : 0;
|
|
289
|
+
const baseUrl = (_f = llmConfig.baseUrl) != null ? _f : this.opts.baseUrl;
|
|
290
|
+
if (!baseUrl) {
|
|
291
|
+
throw new Error("[UniversalLLMAdapter] baseUrl is required in config or config.options");
|
|
292
|
+
}
|
|
293
|
+
this.http = axios2.create({
|
|
294
|
+
baseURL: baseUrl,
|
|
295
|
+
headers: __spreadValues(__spreadValues({
|
|
296
|
+
"Content-Type": "application/json"
|
|
297
|
+
}, config.apiKey ? { Authorization: `Bearer ${config.apiKey}` } : {}), this.opts.headers || {}),
|
|
298
|
+
timeout: (_g = this.opts.timeout) != null ? _g : 6e4
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
async chat(messages, context) {
|
|
302
|
+
var _a, _b;
|
|
303
|
+
const path = (_a = this.opts.chatPath) != null ? _a : "/chat/completions";
|
|
304
|
+
const formattedMessages = [
|
|
305
|
+
{ role: "system", content: `${this.systemPrompt}
|
|
306
|
+
|
|
307
|
+
Context:
|
|
308
|
+
${context != null ? context : "None"}` },
|
|
309
|
+
...messages
|
|
310
|
+
];
|
|
311
|
+
let payload;
|
|
312
|
+
if (this.opts.chatPayloadTemplate) {
|
|
313
|
+
payload = buildPayload(this.opts.chatPayloadTemplate, {
|
|
314
|
+
model: this.model,
|
|
315
|
+
messages: formattedMessages,
|
|
316
|
+
maxTokens: this.maxTokens,
|
|
317
|
+
temperature: this.temperature
|
|
318
|
+
});
|
|
319
|
+
} else {
|
|
320
|
+
payload = {
|
|
321
|
+
model: this.model,
|
|
322
|
+
messages: formattedMessages,
|
|
323
|
+
max_tokens: this.maxTokens,
|
|
324
|
+
temperature: this.temperature
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
const { data } = await this.http.post(path, payload);
|
|
328
|
+
const extractPath = (_b = this.opts.responseExtractPath) != null ? _b : "choices[0].message.content";
|
|
329
|
+
const result = resolvePath(data, extractPath);
|
|
330
|
+
if (result === void 0) {
|
|
331
|
+
throw new Error(`[UniversalLLMAdapter] Could not extract text from path '${extractPath}' in response.`);
|
|
332
|
+
}
|
|
333
|
+
return String(result);
|
|
334
|
+
}
|
|
335
|
+
async embed(text) {
|
|
336
|
+
var _a, _b;
|
|
337
|
+
const path = (_a = this.opts.embedPath) != null ? _a : "/embeddings";
|
|
338
|
+
let payload;
|
|
339
|
+
if (this.opts.embedPayloadTemplate) {
|
|
340
|
+
payload = buildPayload(this.opts.embedPayloadTemplate, {
|
|
341
|
+
model: this.model,
|
|
342
|
+
input: text
|
|
343
|
+
});
|
|
344
|
+
} else {
|
|
345
|
+
payload = {
|
|
346
|
+
model: this.model,
|
|
347
|
+
input: text
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
const { data } = await this.http.post(path, payload);
|
|
351
|
+
const extractPath = (_b = this.opts.embedExtractPath) != null ? _b : "data[0].embedding";
|
|
352
|
+
const vector = resolvePath(data, extractPath);
|
|
353
|
+
if (!Array.isArray(vector)) {
|
|
354
|
+
throw new Error(`[UniversalLLMAdapter] Expected a number array at '${extractPath}' for embeddings.`);
|
|
355
|
+
}
|
|
356
|
+
return vector;
|
|
357
|
+
}
|
|
358
|
+
async batchEmbed(texts) {
|
|
359
|
+
const vectors = [];
|
|
360
|
+
for (const text of texts) {
|
|
361
|
+
vectors.push(await this.embed(text));
|
|
362
|
+
}
|
|
363
|
+
return vectors;
|
|
364
|
+
}
|
|
365
|
+
async ping() {
|
|
366
|
+
try {
|
|
367
|
+
if (this.opts.pingPath) {
|
|
368
|
+
await this.http.get(this.opts.pingPath);
|
|
369
|
+
}
|
|
370
|
+
return true;
|
|
371
|
+
} catch (err) {
|
|
372
|
+
console.error("[UniversalLLMAdapter] Ping failed:", err);
|
|
373
|
+
return false;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
// src/llm/LLMFactory.ts
|
|
379
|
+
var LLMFactory = class _LLMFactory {
|
|
380
|
+
static create(llmConfig, embeddingConfig) {
|
|
381
|
+
var _a;
|
|
382
|
+
switch (llmConfig.provider) {
|
|
383
|
+
case "openai":
|
|
384
|
+
return new OpenAIProvider(llmConfig, embeddingConfig);
|
|
385
|
+
case "anthropic":
|
|
386
|
+
return new AnthropicProvider(llmConfig, embeddingConfig);
|
|
387
|
+
case "ollama":
|
|
388
|
+
return new OllamaProvider(llmConfig, embeddingConfig);
|
|
389
|
+
case "rest":
|
|
390
|
+
case "universal_rest":
|
|
391
|
+
case "custom":
|
|
392
|
+
return new UniversalLLMAdapter(llmConfig);
|
|
393
|
+
default:
|
|
394
|
+
if (llmConfig.baseUrl || ((_a = llmConfig.options) == null ? void 0 : _a.baseUrl)) {
|
|
395
|
+
return new UniversalLLMAdapter(llmConfig);
|
|
396
|
+
}
|
|
397
|
+
throw new Error(
|
|
398
|
+
`[LLMFactory] Unknown provider "${llmConfig.provider}". Supported: openai | anthropic | ollama | rest | custom`
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Creates a dedicated embedding-only provider.
|
|
404
|
+
* Useful when the LLM provider (e.g. Anthropic) doesn't support embeddings.
|
|
405
|
+
*/
|
|
406
|
+
static createEmbeddingProvider(embeddingConfig) {
|
|
407
|
+
const fakeLLMConfig = {
|
|
408
|
+
provider: embeddingConfig.provider,
|
|
409
|
+
model: embeddingConfig.model,
|
|
410
|
+
apiKey: embeddingConfig.apiKey,
|
|
411
|
+
baseUrl: embeddingConfig.baseUrl,
|
|
412
|
+
options: embeddingConfig.options
|
|
413
|
+
};
|
|
414
|
+
return _LLMFactory.create(fakeLLMConfig, embeddingConfig);
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
export {
|
|
419
|
+
mergeDefined,
|
|
420
|
+
OpenAIProvider,
|
|
421
|
+
AnthropicProvider,
|
|
422
|
+
OllamaProvider,
|
|
423
|
+
LLMFactory
|
|
424
|
+
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BaseVectorProvider
|
|
3
|
+
} from "./chunk-VOIWNO5O.mjs";
|
|
4
|
+
import {
|
|
5
|
+
__spreadValues
|
|
6
|
+
} from "./chunk-I4E63NIC.mjs";
|
|
7
|
+
|
|
8
|
+
// src/providers/vectordb/ChromaDBProvider.ts
|
|
9
|
+
import axios from "axios";
|
|
10
|
+
var ChromaDBProvider = class extends BaseVectorProvider {
|
|
11
|
+
constructor(config) {
|
|
12
|
+
super(config);
|
|
13
|
+
this.collectionId = "";
|
|
14
|
+
const opts = config.options;
|
|
15
|
+
const baseUrl = opts.baseUrl;
|
|
16
|
+
if (!baseUrl) throw new Error("[ChromaDBProvider] baseUrl is required");
|
|
17
|
+
this.http = axios.create({
|
|
18
|
+
baseURL: baseUrl,
|
|
19
|
+
headers: { "Content-Type": "application/json" }
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
async initialize() {
|
|
23
|
+
const { data } = await this.http.get(`/api/v1/collections/${this.indexName}`);
|
|
24
|
+
this.collectionId = data.id;
|
|
25
|
+
}
|
|
26
|
+
async upsert(doc, namespace) {
|
|
27
|
+
await this.batchUpsert([doc], namespace);
|
|
28
|
+
}
|
|
29
|
+
async batchUpsert(docs, namespace) {
|
|
30
|
+
const payload = {
|
|
31
|
+
ids: docs.map((d) => d.id),
|
|
32
|
+
embeddings: docs.map((d) => d.vector),
|
|
33
|
+
documents: docs.map((d) => d.content),
|
|
34
|
+
metadatas: docs.map((d) => __spreadValues(__spreadValues({}, d.metadata || {}), namespace ? { namespace } : {}))
|
|
35
|
+
};
|
|
36
|
+
await this.http.post(`/api/v1/collections/${this.collectionId}/add`, payload);
|
|
37
|
+
}
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
39
|
+
async query(vector, topK, namespace, _filter) {
|
|
40
|
+
const payload = {
|
|
41
|
+
query_embeddings: [vector],
|
|
42
|
+
n_results: topK,
|
|
43
|
+
where: namespace ? { namespace: { $eq: namespace } } : void 0
|
|
44
|
+
};
|
|
45
|
+
const { data } = await this.http.post(`/api/v1/collections/${this.collectionId}/query`, payload);
|
|
46
|
+
const matches = [];
|
|
47
|
+
if (data.ids && data.ids[0]) {
|
|
48
|
+
for (let i = 0; i < data.ids[0].length; i++) {
|
|
49
|
+
matches.push({
|
|
50
|
+
id: data.ids[0][i],
|
|
51
|
+
score: data.distances ? 1 - data.distances[0][i] : 0,
|
|
52
|
+
content: data.documents ? data.documents[0][i] : "",
|
|
53
|
+
metadata: data.metadatas ? data.metadatas[0][i] : {}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return matches;
|
|
58
|
+
}
|
|
59
|
+
async delete(id, _namespace) {
|
|
60
|
+
await this.http.post(`/api/v1/collections/${this.collectionId}/delete`, {
|
|
61
|
+
ids: [id],
|
|
62
|
+
where: _namespace ? { namespace: { $eq: _namespace } } : void 0
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
async deleteNamespace(_namespace) {
|
|
66
|
+
await this.http.post(`/api/v1/collections/${this.collectionId}/delete`, {
|
|
67
|
+
where: { namespace: { $eq: _namespace } }
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
async ping() {
|
|
71
|
+
try {
|
|
72
|
+
await this.http.get("/api/v1/heartbeat");
|
|
73
|
+
return true;
|
|
74
|
+
} catch (e) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async disconnect() {
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export {
|
|
83
|
+
ChromaDBProvider
|
|
84
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
|
|
21
|
+
export {
|
|
22
|
+
__spreadValues,
|
|
23
|
+
__spreadProps
|
|
24
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BaseVectorProvider
|
|
3
|
+
} from "./chunk-VOIWNO5O.mjs";
|
|
4
|
+
import {
|
|
5
|
+
__spreadValues
|
|
6
|
+
} from "./chunk-I4E63NIC.mjs";
|
|
7
|
+
|
|
8
|
+
// src/providers/vectordb/MongoDBProvider.ts
|
|
9
|
+
import { MongoClient } from "mongodb";
|
|
10
|
+
var MongoDBProvider = class extends BaseVectorProvider {
|
|
11
|
+
constructor(config) {
|
|
12
|
+
super(config);
|
|
13
|
+
const opts = config.options;
|
|
14
|
+
if (!opts.uri || !opts.database || !opts.collection) {
|
|
15
|
+
throw new Error("[MongoDBProvider] options uri, database, and collection are required.");
|
|
16
|
+
}
|
|
17
|
+
this.client = new MongoClient(opts.uri);
|
|
18
|
+
this.dbName = opts.database;
|
|
19
|
+
this.collectionName = opts.collection;
|
|
20
|
+
this.embeddingKey = opts.embeddingKey || "embedding";
|
|
21
|
+
this.contentKey = opts.contentKey || "content";
|
|
22
|
+
this.metadataKey = opts.metadataKey || "metadata";
|
|
23
|
+
}
|
|
24
|
+
async initialize() {
|
|
25
|
+
await this.client.connect();
|
|
26
|
+
this.db = this.client.db(this.dbName);
|
|
27
|
+
this.collection = this.db.collection(this.collectionName);
|
|
28
|
+
}
|
|
29
|
+
async upsert(doc, namespace) {
|
|
30
|
+
const document = __spreadValues({
|
|
31
|
+
_id: doc.id,
|
|
32
|
+
[this.embeddingKey]: doc.vector,
|
|
33
|
+
[this.contentKey]: doc.content,
|
|
34
|
+
[this.metadataKey]: doc.metadata || {}
|
|
35
|
+
}, namespace ? { namespace } : {});
|
|
36
|
+
await this.collection.updateOne({ _id: doc.id }, { $set: document }, { upsert: true });
|
|
37
|
+
}
|
|
38
|
+
async batchUpsert(docs, namespace) {
|
|
39
|
+
const operations = docs.map((doc) => ({
|
|
40
|
+
updateOne: {
|
|
41
|
+
filter: { _id: doc.id },
|
|
42
|
+
update: {
|
|
43
|
+
$set: __spreadValues({
|
|
44
|
+
[this.embeddingKey]: doc.vector,
|
|
45
|
+
[this.contentKey]: doc.content,
|
|
46
|
+
[this.metadataKey]: doc.metadata || {}
|
|
47
|
+
}, namespace ? { namespace } : {})
|
|
48
|
+
},
|
|
49
|
+
upsert: true
|
|
50
|
+
}
|
|
51
|
+
}));
|
|
52
|
+
await this.collection.bulkWrite(operations);
|
|
53
|
+
}
|
|
54
|
+
async query(vector, topK, namespace, filter) {
|
|
55
|
+
const pipeline = [
|
|
56
|
+
{
|
|
57
|
+
$vectorSearch: __spreadValues({
|
|
58
|
+
index: this.config.options.indexName || "vector_index",
|
|
59
|
+
path: this.embeddingKey,
|
|
60
|
+
queryVector: vector,
|
|
61
|
+
numCandidates: Math.max(topK * 10, 100),
|
|
62
|
+
limit: topK
|
|
63
|
+
}, filter || namespace ? { filter: __spreadValues(__spreadValues({}, filter || {}), namespace ? { namespace } : {}) } : {})
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
$project: {
|
|
67
|
+
score: { $meta: "vectorSearchScore" },
|
|
68
|
+
content: `$${this.contentKey}`,
|
|
69
|
+
metadata: `$${this.metadataKey}`
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
];
|
|
73
|
+
const results = await this.collection.aggregate(pipeline).toArray();
|
|
74
|
+
return results.map((res) => ({
|
|
75
|
+
id: res["_id"].toString(),
|
|
76
|
+
score: res["score"],
|
|
77
|
+
content: res["content"],
|
|
78
|
+
metadata: res["metadata"]
|
|
79
|
+
}));
|
|
80
|
+
}
|
|
81
|
+
async delete(id, namespace) {
|
|
82
|
+
await this.collection.deleteOne(__spreadValues({ _id: id }, namespace ? { namespace } : {}));
|
|
83
|
+
}
|
|
84
|
+
async deleteNamespace(namespace) {
|
|
85
|
+
await this.collection.deleteMany({ namespace });
|
|
86
|
+
}
|
|
87
|
+
async ping() {
|
|
88
|
+
try {
|
|
89
|
+
await this.db.command({ ping: 1 });
|
|
90
|
+
return true;
|
|
91
|
+
} catch (e) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async disconnect() {
|
|
96
|
+
await this.client.close();
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export {
|
|
101
|
+
MongoDBProvider
|
|
102
|
+
};
|