@retrivora-ai/rag-engine 0.1.4 → 0.1.6
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/DocumentChunker-BEyzadsv.d.mts +93 -0
- package/dist/DocumentChunker-BEyzadsv.d.ts +93 -0
- package/dist/{LLMFactory-XC55FTD2.mjs → LLMFactory-JFOY2V4X.mjs} +2 -1
- package/dist/{PineconeProvider-NJ675H7U.mjs → PineconeProvider-ZRAFNFEC.mjs} +1 -1
- package/dist/{PostgreSQLProvider-ISNMD3BE.mjs → PostgreSQLProvider-ZNXA67IM.mjs} +1 -1
- package/dist/{QdrantProvider-LJWOIGES.mjs → QdrantProvider-NYGHAA7C.mjs} +1 -1
- package/dist/{Pipeline-Bo6CUCox.d.mts → RagConfig-hBGXJmSx.d.mts} +23 -93
- package/dist/{Pipeline-Bo6CUCox.d.ts → RagConfig-hBGXJmSx.d.ts} +23 -93
- package/dist/{chunk-6FODXNUF.mjs → chunk-5K23H7JL.mjs} +27 -1
- package/dist/chunk-HSBXE2WV.mjs +1758 -0
- package/dist/{chunk-S5DRHETN.mjs → chunk-IUTAZ7QR.mjs} +31 -2
- package/dist/{chunk-GD3QJFNN.mjs → chunk-JI6VD5TJ.mjs} +4 -41
- package/dist/chunk-UKDXCXW7.mjs +49 -0
- package/dist/{chunk-AALIF3AL.mjs → chunk-ZM6TYIDH.mjs} +3 -3
- package/dist/handlers/index.d.mts +3 -44
- package/dist/handlers/index.d.ts +3 -44
- package/dist/handlers/index.js +1322 -57
- package/dist/handlers/index.mjs +3 -3
- package/dist/index-Bx182KKn.d.ts +64 -0
- package/dist/index-Ck2pt7-8.d.mts +64 -0
- package/dist/index.d.mts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +19 -1646
- package/dist/index.mjs +1 -11
- package/dist/server.d.mts +188 -11
- package/dist/server.d.ts +188 -11
- package/dist/server.js +1322 -57
- package/dist/server.mjs +10 -11
- package/package.json +20 -2
- package/src/config/serverConfig.ts +3 -0
- package/src/core/BatchProcessor.ts +347 -0
- package/src/core/ConfigValidator.ts +560 -0
- package/src/core/Pipeline.ts +91 -49
- package/src/core/ProviderHealthCheck.ts +565 -0
- package/src/core/VectorPlugin.ts +51 -9
- package/src/handlers/index.ts +2 -2
- package/src/index.ts +1 -7
- package/src/providers/vectordb/BaseVectorProvider.ts +1 -1
- package/src/providers/vectordb/MilvusProvider.ts +1 -1
- package/src/providers/vectordb/MongoDBProvider.ts +1 -1
- package/src/providers/vectordb/PineconeProvider.ts +4 -4
- package/src/providers/vectordb/PostgreSQLProvider.ts +35 -3
- package/src/providers/vectordb/QdrantProvider.ts +32 -2
- package/src/rag/DocumentChunker.ts +2 -2
- package/src/server.ts +1 -1
- package/src/types/index.ts +13 -2
- package/dist/DocumentChunker-BQ5kQD7B.d.ts +0 -155
- package/dist/DocumentChunker-D9-fObJp.d.mts +0 -155
- package/dist/chunk-AIAB2IEE.mjs +0 -394
- package/dist/chunk-FGGSVVSY.mjs +0 -162
package/dist/index.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __create = Object.create;
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
|
-
var __defProps = Object.defineProperties;
|
|
5
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
7
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
6
|
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
9
7
|
var __getProtoOf = Object.getPrototypeOf;
|
|
@@ -21,10 +19,6 @@ var __spreadValues = (a, b) => {
|
|
|
21
19
|
}
|
|
22
20
|
return a;
|
|
23
21
|
};
|
|
24
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
25
|
-
var __esm = (fn, res) => function __init() {
|
|
26
|
-
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
27
|
-
};
|
|
28
22
|
var __export = (target, all) => {
|
|
29
23
|
for (var name in all)
|
|
30
24
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -47,1651 +41,19 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
47
41
|
));
|
|
48
42
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
49
43
|
|
|
50
|
-
// src/utils/templateUtils.ts
|
|
51
|
-
function isRecord(value) {
|
|
52
|
-
return typeof value === "object" && value !== null;
|
|
53
|
-
}
|
|
54
|
-
function resolvePath(obj, path) {
|
|
55
|
-
if (!path || !obj) return void 0;
|
|
56
|
-
return path.replace(/\[(\w+)\]/g, ".$1").replace(/^\./, "").split(".").reduce((current, segment) => {
|
|
57
|
-
if (!isRecord(current) && !Array.isArray(current)) {
|
|
58
|
-
return void 0;
|
|
59
|
-
}
|
|
60
|
-
return current[segment];
|
|
61
|
-
}, obj);
|
|
62
|
-
}
|
|
63
|
-
function buildPayload(template, vars) {
|
|
64
|
-
let raw = template;
|
|
65
|
-
for (const [key, val] of Object.entries(vars)) {
|
|
66
|
-
const stringifiedVal = val === void 0 ? "null" : JSON.stringify(val);
|
|
67
|
-
raw = raw.replace(new RegExp(`"\\{\\{${key}\\}\\}"`, "g"), stringifiedVal);
|
|
68
|
-
raw = raw.replace(new RegExp(`\\{\\{${key}\\}\\}`, "g"), stringifiedVal);
|
|
69
|
-
}
|
|
70
|
-
try {
|
|
71
|
-
return JSON.parse(raw);
|
|
72
|
-
} catch (err) {
|
|
73
|
-
throw new Error(`Failed to parse payload template: ${err.message}
|
|
74
|
-
Raw payload: ${raw}`);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
function mergeDefined(base, override) {
|
|
78
|
-
const merged = __spreadValues({}, base || {});
|
|
79
|
-
if (!override) {
|
|
80
|
-
return merged;
|
|
81
|
-
}
|
|
82
|
-
for (const [key, value] of Object.entries(override)) {
|
|
83
|
-
if (value !== void 0 && value !== null && value !== "") {
|
|
84
|
-
merged[key] = value;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return merged;
|
|
88
|
-
}
|
|
89
|
-
var init_templateUtils = __esm({
|
|
90
|
-
"src/utils/templateUtils.ts"() {
|
|
91
|
-
"use strict";
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
// src/llm/providers/OpenAIProvider.ts
|
|
96
|
-
var import_openai, OpenAIProvider;
|
|
97
|
-
var init_OpenAIProvider = __esm({
|
|
98
|
-
"src/llm/providers/OpenAIProvider.ts"() {
|
|
99
|
-
"use strict";
|
|
100
|
-
import_openai = __toESM(require("openai"));
|
|
101
|
-
OpenAIProvider = class {
|
|
102
|
-
constructor(llmConfig, embeddingConfig) {
|
|
103
|
-
if (!llmConfig.apiKey) throw new Error("[OpenAIProvider] llmConfig.apiKey is required");
|
|
104
|
-
this.client = new import_openai.default({ apiKey: llmConfig.apiKey });
|
|
105
|
-
this.llmConfig = llmConfig;
|
|
106
|
-
this.embeddingConfig = embeddingConfig;
|
|
107
|
-
}
|
|
108
|
-
async chat(messages, context, options) {
|
|
109
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
110
|
-
const systemContent = (_a = this.llmConfig.systemPrompt) != null ? _a : `You are a helpful assistant. Answer questions based on the provided context.
|
|
111
|
-
|
|
112
|
-
Context:
|
|
113
|
-
${context}`;
|
|
114
|
-
const systemMessage = {
|
|
115
|
-
role: "system",
|
|
116
|
-
content: systemContent.includes("{{context}}") ? systemContent.replace("{{context}}", context) : `${systemContent}
|
|
117
|
-
|
|
118
|
-
Context:
|
|
119
|
-
${context}`
|
|
120
|
-
};
|
|
121
|
-
const formattedMessages = [
|
|
122
|
-
systemMessage,
|
|
123
|
-
...messages.map((m) => ({
|
|
124
|
-
role: m.role,
|
|
125
|
-
content: m.content
|
|
126
|
-
}))
|
|
127
|
-
];
|
|
128
|
-
const completion = await this.client.chat.completions.create({
|
|
129
|
-
model: this.llmConfig.model,
|
|
130
|
-
messages: formattedMessages,
|
|
131
|
-
max_tokens: (_c = (_b = options == null ? void 0 : options.maxTokens) != null ? _b : this.llmConfig.maxTokens) != null ? _c : 1024,
|
|
132
|
-
temperature: (_e = (_d = options == null ? void 0 : options.temperature) != null ? _d : this.llmConfig.temperature) != null ? _e : 0.7,
|
|
133
|
-
stop: options == null ? void 0 : options.stop
|
|
134
|
-
});
|
|
135
|
-
return (_h = (_g = (_f = completion.choices[0]) == null ? void 0 : _f.message) == null ? void 0 : _g.content) != null ? _h : "";
|
|
136
|
-
}
|
|
137
|
-
async embed(text, options) {
|
|
138
|
-
const results = await this.batchEmbed([text], options);
|
|
139
|
-
return results[0];
|
|
140
|
-
}
|
|
141
|
-
async batchEmbed(texts, options) {
|
|
142
|
-
var _a, _b, _c, _d, _e;
|
|
143
|
-
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";
|
|
144
|
-
const apiKey = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.apiKey) != null ? _e : this.llmConfig.apiKey;
|
|
145
|
-
const client = apiKey !== this.llmConfig.apiKey ? new import_openai.default({ apiKey }) : this.client;
|
|
146
|
-
const response = await client.embeddings.create({ model, input: texts });
|
|
147
|
-
return response.data.map((d) => d.embedding);
|
|
148
|
-
}
|
|
149
|
-
async ping() {
|
|
150
|
-
try {
|
|
151
|
-
await this.client.models.list();
|
|
152
|
-
return true;
|
|
153
|
-
} catch (err) {
|
|
154
|
-
console.error("[OpenAIProvider] Ping failed:", err);
|
|
155
|
-
return false;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
// src/llm/providers/AnthropicProvider.ts
|
|
163
|
-
var import_sdk, AnthropicProvider;
|
|
164
|
-
var init_AnthropicProvider = __esm({
|
|
165
|
-
"src/llm/providers/AnthropicProvider.ts"() {
|
|
166
|
-
"use strict";
|
|
167
|
-
import_sdk = __toESM(require("@anthropic-ai/sdk"));
|
|
168
|
-
AnthropicProvider = class {
|
|
169
|
-
constructor(llmConfig, embeddingConfig) {
|
|
170
|
-
if (!llmConfig.apiKey) throw new Error("[AnthropicProvider] llmConfig.apiKey is required");
|
|
171
|
-
this.client = new import_sdk.default({ apiKey: llmConfig.apiKey });
|
|
172
|
-
this.llmConfig = llmConfig;
|
|
173
|
-
this.embeddingConfig = embeddingConfig;
|
|
174
|
-
}
|
|
175
|
-
async chat(messages, context, options) {
|
|
176
|
-
var _a, _b, _c;
|
|
177
|
-
const systemPrompt = (_a = this.llmConfig.systemPrompt) != null ? _a : `You are a helpful assistant. Use the context below to answer the user's question accurately.
|
|
178
|
-
|
|
179
|
-
Context:
|
|
180
|
-
${context}`;
|
|
181
|
-
const system = systemPrompt.includes("{{context}}") ? systemPrompt.replace("{{context}}", context) : `${systemPrompt}
|
|
182
|
-
|
|
183
|
-
Context:
|
|
184
|
-
${context}`;
|
|
185
|
-
const anthropicMessages = messages.map((m) => ({
|
|
186
|
-
role: m.role === "assistant" ? "assistant" : "user",
|
|
187
|
-
content: m.content
|
|
188
|
-
}));
|
|
189
|
-
const response = await this.client.messages.create({
|
|
190
|
-
model: this.llmConfig.model,
|
|
191
|
-
max_tokens: (_c = (_b = options == null ? void 0 : options.maxTokens) != null ? _b : this.llmConfig.maxTokens) != null ? _c : 1024,
|
|
192
|
-
system,
|
|
193
|
-
messages: anthropicMessages
|
|
194
|
-
});
|
|
195
|
-
const block = response.content[0];
|
|
196
|
-
return block.type === "text" ? block.text : "";
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* Anthropic does not offer an embedding API.
|
|
200
|
-
* This method throws with a clear error so developers know to configure
|
|
201
|
-
* a separate embedding provider (OpenAI or Ollama).
|
|
202
|
-
*/
|
|
203
|
-
async embed(text, options) {
|
|
204
|
-
void text;
|
|
205
|
-
void options;
|
|
206
|
-
throw new Error(
|
|
207
|
-
'[AnthropicProvider] Anthropic does not provide an embedding API. Set embedding.provider to "openai" or "ollama" in your RagConfig.'
|
|
208
|
-
);
|
|
209
|
-
}
|
|
210
|
-
async batchEmbed(texts, options) {
|
|
211
|
-
void texts;
|
|
212
|
-
void options;
|
|
213
|
-
throw new Error(
|
|
214
|
-
"[AnthropicProvider] Anthropic does not provide an embedding API."
|
|
215
|
-
);
|
|
216
|
-
}
|
|
217
|
-
async ping() {
|
|
218
|
-
try {
|
|
219
|
-
await this.client.models.list();
|
|
220
|
-
return true;
|
|
221
|
-
} catch (err) {
|
|
222
|
-
console.error("[AnthropicProvider] Ping failed:", err);
|
|
223
|
-
return false;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
};
|
|
227
|
-
}
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
// src/llm/providers/OllamaProvider.ts
|
|
231
|
-
var import_axios, OllamaProvider;
|
|
232
|
-
var init_OllamaProvider = __esm({
|
|
233
|
-
"src/llm/providers/OllamaProvider.ts"() {
|
|
234
|
-
"use strict";
|
|
235
|
-
import_axios = __toESM(require("axios"));
|
|
236
|
-
OllamaProvider = class {
|
|
237
|
-
constructor(llmConfig, embeddingConfig) {
|
|
238
|
-
var _a;
|
|
239
|
-
const baseURL = (_a = llmConfig.baseUrl) != null ? _a : "http://localhost:11434";
|
|
240
|
-
this.http = import_axios.default.create({ baseURL, timeout: 12e4 });
|
|
241
|
-
this.llmConfig = llmConfig;
|
|
242
|
-
this.embeddingConfig = embeddingConfig;
|
|
243
|
-
}
|
|
244
|
-
async chat(messages, context, options) {
|
|
245
|
-
var _a, _b, _c, _d, _e;
|
|
246
|
-
const systemPrompt = (_a = this.llmConfig.systemPrompt) != null ? _a : `You are a helpful assistant. Use the provided context to answer the user's question.
|
|
247
|
-
|
|
248
|
-
Context:
|
|
249
|
-
${context}`;
|
|
250
|
-
const system = systemPrompt.includes("{{context}}") ? systemPrompt.replace("{{context}}", context) : `${systemPrompt}
|
|
251
|
-
|
|
252
|
-
Context:
|
|
253
|
-
${context}`;
|
|
254
|
-
const { data } = await this.http.post("/api/chat", {
|
|
255
|
-
model: this.llmConfig.model,
|
|
256
|
-
stream: false,
|
|
257
|
-
options: {
|
|
258
|
-
temperature: (_c = (_b = options == null ? void 0 : options.temperature) != null ? _b : this.llmConfig.temperature) != null ? _c : 0.7,
|
|
259
|
-
num_predict: (_e = (_d = options == null ? void 0 : options.maxTokens) != null ? _d : this.llmConfig.maxTokens) != null ? _e : 1024
|
|
260
|
-
},
|
|
261
|
-
messages: [
|
|
262
|
-
{ role: "system", content: system },
|
|
263
|
-
...messages.map((m) => ({ role: m.role, content: m.content }))
|
|
264
|
-
]
|
|
265
|
-
});
|
|
266
|
-
return data.message.content;
|
|
267
|
-
}
|
|
268
|
-
async embed(text, options) {
|
|
269
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
270
|
-
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";
|
|
271
|
-
const baseURL = (_f = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.baseUrl) != null ? _e : this.llmConfig.baseUrl) != null ? _f : "http://localhost:11434";
|
|
272
|
-
const client = baseURL !== ((_g = this.llmConfig.baseUrl) != null ? _g : "http://localhost:11434") ? import_axios.default.create({ baseURL, timeout: 6e4 }) : this.http;
|
|
273
|
-
const { data } = await client.post("/api/embeddings", {
|
|
274
|
-
model,
|
|
275
|
-
prompt: text
|
|
276
|
-
});
|
|
277
|
-
return data.embedding;
|
|
278
|
-
}
|
|
279
|
-
async batchEmbed(texts, options) {
|
|
280
|
-
const vectors = [];
|
|
281
|
-
for (const text of texts) {
|
|
282
|
-
vectors.push(await this.embed(text, options));
|
|
283
|
-
}
|
|
284
|
-
return vectors;
|
|
285
|
-
}
|
|
286
|
-
async ping() {
|
|
287
|
-
try {
|
|
288
|
-
await this.http.get("/api/tags");
|
|
289
|
-
return true;
|
|
290
|
-
} catch (err) {
|
|
291
|
-
console.error("[OllamaProvider] Ping failed:", err);
|
|
292
|
-
return false;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
// src/config/UniversalProfiles.ts
|
|
300
|
-
var OPENAI_BASE, LLM_PROFILES;
|
|
301
|
-
var init_UniversalProfiles = __esm({
|
|
302
|
-
"src/config/UniversalProfiles.ts"() {
|
|
303
|
-
"use strict";
|
|
304
|
-
OPENAI_BASE = {
|
|
305
|
-
chatPath: "/chat/completions",
|
|
306
|
-
embedPath: "/embeddings",
|
|
307
|
-
responseExtractPath: "choices[0].message.content",
|
|
308
|
-
embedExtractPath: "data[0].embedding",
|
|
309
|
-
chatPayloadTemplate: '{"model":"{{model}}","messages":{{messages}},"max_tokens":{{maxTokens}},"temperature":{{temperature}}}'
|
|
310
|
-
};
|
|
311
|
-
LLM_PROFILES = {
|
|
312
|
-
"openai-compatible": OPENAI_BASE,
|
|
313
|
-
"litellm": __spreadProps(__spreadValues({}, OPENAI_BASE), {
|
|
314
|
-
chatPayloadTemplate: '{"model":"{{model}}","messages":{{messages}},"max_tokens":{{maxTokens}},"temperature":{{temperature}},"stream":false}'
|
|
315
|
-
}),
|
|
316
|
-
"anthropic-claude": {
|
|
317
|
-
chatPath: "/v1/messages",
|
|
318
|
-
responseExtractPath: "content[0].text",
|
|
319
|
-
headers: { "anthropic-version": "2023-06-01" },
|
|
320
|
-
chatPayloadTemplate: '{"model":"{{model}}","messages":{{messages}},"max_tokens":{{maxTokens}}}'
|
|
321
|
-
},
|
|
322
|
-
"google-gemini": {
|
|
323
|
-
chatPath: "/v1beta/models/{{model}}:generateContent",
|
|
324
|
-
responseExtractPath: "candidates[0].content.parts[0].text",
|
|
325
|
-
chatPayloadTemplate: '{"contents":[{"parts":[{"text":"{{messages}}"}]}]}'
|
|
326
|
-
},
|
|
327
|
-
"github-copilot": __spreadProps(__spreadValues({}, OPENAI_BASE), {
|
|
328
|
-
chatPayloadTemplate: '{"model":"{{model}}","messages":{{messages}},"temperature":{{temperature}}}'
|
|
329
|
-
}),
|
|
330
|
-
"ollama-standard": {
|
|
331
|
-
chatPath: "/api/chat",
|
|
332
|
-
embedPath: "/api/embeddings",
|
|
333
|
-
responseExtractPath: "message.content",
|
|
334
|
-
embedExtractPath: "embedding",
|
|
335
|
-
chatPayloadTemplate: '{"model":"{{model}}","messages":{{messages}},"stream":false}',
|
|
336
|
-
embedPayloadTemplate: '{"model":"{{model}}","prompt":"{{input}}"}'
|
|
337
|
-
}
|
|
338
|
-
};
|
|
339
|
-
}
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
// src/llm/providers/UniversalLLMAdapter.ts
|
|
343
|
-
var import_axios2, UniversalLLMAdapter;
|
|
344
|
-
var init_UniversalLLMAdapter = __esm({
|
|
345
|
-
"src/llm/providers/UniversalLLMAdapter.ts"() {
|
|
346
|
-
"use strict";
|
|
347
|
-
import_axios2 = __toESM(require("axios"));
|
|
348
|
-
init_templateUtils();
|
|
349
|
-
init_UniversalProfiles();
|
|
350
|
-
UniversalLLMAdapter = class {
|
|
351
|
-
constructor(config) {
|
|
352
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
353
|
-
this.model = config.model;
|
|
354
|
-
const llmConfig = config;
|
|
355
|
-
const options = (_a = llmConfig.options) != null ? _a : {};
|
|
356
|
-
let profile = {};
|
|
357
|
-
if (typeof options.profile === "string") {
|
|
358
|
-
profile = LLM_PROFILES[options.profile] || {};
|
|
359
|
-
} else if (typeof options.profile === "object" && options.profile !== null) {
|
|
360
|
-
profile = options.profile;
|
|
361
|
-
}
|
|
362
|
-
this.opts = __spreadValues(__spreadValues({}, profile), (_b = llmConfig.options) != null ? _b : {});
|
|
363
|
-
this.systemPrompt = (_c = llmConfig.systemPrompt) != null ? _c : "You are a helpful AI assistant. Use the provided context to answer the user.";
|
|
364
|
-
this.maxTokens = (_d = llmConfig.maxTokens) != null ? _d : 1024;
|
|
365
|
-
this.temperature = (_e = llmConfig.temperature) != null ? _e : 0;
|
|
366
|
-
const baseUrl = (_f = llmConfig.baseUrl) != null ? _f : this.opts.baseUrl;
|
|
367
|
-
if (!baseUrl) {
|
|
368
|
-
throw new Error("[UniversalLLMAdapter] baseUrl is required in config or config.options");
|
|
369
|
-
}
|
|
370
|
-
this.http = import_axios2.default.create({
|
|
371
|
-
baseURL: baseUrl,
|
|
372
|
-
headers: __spreadValues(__spreadValues({
|
|
373
|
-
"Content-Type": "application/json"
|
|
374
|
-
}, config.apiKey ? { Authorization: `Bearer ${config.apiKey}` } : {}), this.opts.headers || {}),
|
|
375
|
-
timeout: (_g = this.opts.timeout) != null ? _g : 6e4
|
|
376
|
-
});
|
|
377
|
-
}
|
|
378
|
-
async chat(messages, context) {
|
|
379
|
-
var _a, _b;
|
|
380
|
-
const path = (_a = this.opts.chatPath) != null ? _a : "/chat/completions";
|
|
381
|
-
const formattedMessages = [
|
|
382
|
-
{ role: "system", content: `${this.systemPrompt}
|
|
383
|
-
|
|
384
|
-
Context:
|
|
385
|
-
${context != null ? context : "None"}` },
|
|
386
|
-
...messages
|
|
387
|
-
];
|
|
388
|
-
let payload;
|
|
389
|
-
if (this.opts.chatPayloadTemplate) {
|
|
390
|
-
payload = buildPayload(this.opts.chatPayloadTemplate, {
|
|
391
|
-
model: this.model,
|
|
392
|
-
messages: formattedMessages,
|
|
393
|
-
maxTokens: this.maxTokens,
|
|
394
|
-
temperature: this.temperature
|
|
395
|
-
});
|
|
396
|
-
} else {
|
|
397
|
-
payload = {
|
|
398
|
-
model: this.model,
|
|
399
|
-
messages: formattedMessages,
|
|
400
|
-
max_tokens: this.maxTokens,
|
|
401
|
-
temperature: this.temperature
|
|
402
|
-
};
|
|
403
|
-
}
|
|
404
|
-
const { data } = await this.http.post(path, payload);
|
|
405
|
-
const extractPath = (_b = this.opts.responseExtractPath) != null ? _b : "choices[0].message.content";
|
|
406
|
-
const result = resolvePath(data, extractPath);
|
|
407
|
-
if (result === void 0) {
|
|
408
|
-
throw new Error(`[UniversalLLMAdapter] Could not extract text from path '${extractPath}' in response.`);
|
|
409
|
-
}
|
|
410
|
-
return String(result);
|
|
411
|
-
}
|
|
412
|
-
async embed(text) {
|
|
413
|
-
var _a, _b;
|
|
414
|
-
const path = (_a = this.opts.embedPath) != null ? _a : "/embeddings";
|
|
415
|
-
let payload;
|
|
416
|
-
if (this.opts.embedPayloadTemplate) {
|
|
417
|
-
payload = buildPayload(this.opts.embedPayloadTemplate, {
|
|
418
|
-
model: this.model,
|
|
419
|
-
input: text
|
|
420
|
-
});
|
|
421
|
-
} else {
|
|
422
|
-
payload = {
|
|
423
|
-
model: this.model,
|
|
424
|
-
input: text
|
|
425
|
-
};
|
|
426
|
-
}
|
|
427
|
-
const { data } = await this.http.post(path, payload);
|
|
428
|
-
const extractPath = (_b = this.opts.embedExtractPath) != null ? _b : "data[0].embedding";
|
|
429
|
-
const vector = resolvePath(data, extractPath);
|
|
430
|
-
if (!Array.isArray(vector)) {
|
|
431
|
-
throw new Error(`[UniversalLLMAdapter] Expected a number array at '${extractPath}' for embeddings.`);
|
|
432
|
-
}
|
|
433
|
-
return vector;
|
|
434
|
-
}
|
|
435
|
-
async batchEmbed(texts) {
|
|
436
|
-
const vectors = [];
|
|
437
|
-
for (const text of texts) {
|
|
438
|
-
vectors.push(await this.embed(text));
|
|
439
|
-
}
|
|
440
|
-
return vectors;
|
|
441
|
-
}
|
|
442
|
-
async ping() {
|
|
443
|
-
try {
|
|
444
|
-
if (this.opts.pingPath) {
|
|
445
|
-
await this.http.get(this.opts.pingPath);
|
|
446
|
-
}
|
|
447
|
-
return true;
|
|
448
|
-
} catch (err) {
|
|
449
|
-
console.error("[UniversalLLMAdapter] Ping failed:", err);
|
|
450
|
-
return false;
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
};
|
|
454
|
-
}
|
|
455
|
-
});
|
|
456
|
-
|
|
457
|
-
// src/llm/LLMFactory.ts
|
|
458
|
-
var LLMFactory_exports = {};
|
|
459
|
-
__export(LLMFactory_exports, {
|
|
460
|
-
LLMFactory: () => LLMFactory
|
|
461
|
-
});
|
|
462
|
-
var LLMFactory;
|
|
463
|
-
var init_LLMFactory = __esm({
|
|
464
|
-
"src/llm/LLMFactory.ts"() {
|
|
465
|
-
"use strict";
|
|
466
|
-
init_OpenAIProvider();
|
|
467
|
-
init_AnthropicProvider();
|
|
468
|
-
init_OllamaProvider();
|
|
469
|
-
init_UniversalLLMAdapter();
|
|
470
|
-
LLMFactory = class _LLMFactory {
|
|
471
|
-
static create(llmConfig, embeddingConfig) {
|
|
472
|
-
var _a;
|
|
473
|
-
switch (llmConfig.provider) {
|
|
474
|
-
case "openai":
|
|
475
|
-
return new OpenAIProvider(llmConfig, embeddingConfig);
|
|
476
|
-
case "anthropic":
|
|
477
|
-
return new AnthropicProvider(llmConfig, embeddingConfig);
|
|
478
|
-
case "ollama":
|
|
479
|
-
return new OllamaProvider(llmConfig, embeddingConfig);
|
|
480
|
-
case "rest":
|
|
481
|
-
case "universal_rest":
|
|
482
|
-
case "custom":
|
|
483
|
-
return new UniversalLLMAdapter(llmConfig);
|
|
484
|
-
default:
|
|
485
|
-
if (llmConfig.baseUrl || ((_a = llmConfig.options) == null ? void 0 : _a.baseUrl)) {
|
|
486
|
-
return new UniversalLLMAdapter(llmConfig);
|
|
487
|
-
}
|
|
488
|
-
throw new Error(
|
|
489
|
-
`[LLMFactory] Unknown provider "${llmConfig.provider}". Supported: openai | anthropic | ollama | rest | custom`
|
|
490
|
-
);
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
/**
|
|
494
|
-
* Creates a dedicated embedding-only provider.
|
|
495
|
-
* Useful when the LLM provider (e.g. Anthropic) doesn't support embeddings.
|
|
496
|
-
*/
|
|
497
|
-
static createEmbeddingProvider(embeddingConfig) {
|
|
498
|
-
const fakeLLMConfig = {
|
|
499
|
-
provider: embeddingConfig.provider,
|
|
500
|
-
model: embeddingConfig.model,
|
|
501
|
-
apiKey: embeddingConfig.apiKey,
|
|
502
|
-
baseUrl: embeddingConfig.baseUrl,
|
|
503
|
-
options: embeddingConfig.options
|
|
504
|
-
};
|
|
505
|
-
return _LLMFactory.create(fakeLLMConfig, embeddingConfig);
|
|
506
|
-
}
|
|
507
|
-
};
|
|
508
|
-
}
|
|
509
|
-
});
|
|
510
|
-
|
|
511
|
-
// src/providers/vectordb/BaseVectorProvider.ts
|
|
512
|
-
var BaseVectorProvider;
|
|
513
|
-
var init_BaseVectorProvider = __esm({
|
|
514
|
-
"src/providers/vectordb/BaseVectorProvider.ts"() {
|
|
515
|
-
"use strict";
|
|
516
|
-
BaseVectorProvider = class {
|
|
517
|
-
constructor(config) {
|
|
518
|
-
this.config = config;
|
|
519
|
-
this.indexName = config.indexName || "default";
|
|
520
|
-
}
|
|
521
|
-
};
|
|
522
|
-
}
|
|
523
|
-
});
|
|
524
|
-
|
|
525
|
-
// src/providers/vectordb/PineconeProvider.ts
|
|
526
|
-
var PineconeProvider_exports = {};
|
|
527
|
-
__export(PineconeProvider_exports, {
|
|
528
|
-
PineconeProvider: () => PineconeProvider
|
|
529
|
-
});
|
|
530
|
-
var import_pinecone, PineconeProvider;
|
|
531
|
-
var init_PineconeProvider = __esm({
|
|
532
|
-
"src/providers/vectordb/PineconeProvider.ts"() {
|
|
533
|
-
"use strict";
|
|
534
|
-
import_pinecone = require("@pinecone-database/pinecone");
|
|
535
|
-
init_BaseVectorProvider();
|
|
536
|
-
PineconeProvider = class extends BaseVectorProvider {
|
|
537
|
-
constructor(config) {
|
|
538
|
-
super(config);
|
|
539
|
-
const opts = config.options;
|
|
540
|
-
if (!opts.apiKey) throw new Error("[PineconeProvider] options.apiKey is required");
|
|
541
|
-
this.apiKey = opts.apiKey;
|
|
542
|
-
}
|
|
543
|
-
async initialize() {
|
|
544
|
-
var _a, _b;
|
|
545
|
-
this.client = new import_pinecone.Pinecone({ apiKey: this.apiKey });
|
|
546
|
-
const indexes = await this.client.listIndexes();
|
|
547
|
-
const names = (_b = (_a = indexes.indexes) == null ? void 0 : _a.map((i) => i.name)) != null ? _b : [];
|
|
548
|
-
if (!names.includes(this.indexName)) {
|
|
549
|
-
throw new Error(`[PineconeProvider] Index "${this.indexName}" not found.`);
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
index(namespace) {
|
|
553
|
-
const idx = this.client.index(this.indexName);
|
|
554
|
-
return namespace ? idx.namespace(namespace) : idx.namespace("");
|
|
555
|
-
}
|
|
556
|
-
async upsert(doc, namespace) {
|
|
557
|
-
var _a;
|
|
558
|
-
await this.index(namespace).upsert({
|
|
559
|
-
records: [{
|
|
560
|
-
id: doc.id,
|
|
561
|
-
values: doc.vector,
|
|
562
|
-
metadata: __spreadValues({ content: doc.content }, (_a = doc.metadata) != null ? _a : {})
|
|
563
|
-
}]
|
|
564
|
-
});
|
|
565
|
-
}
|
|
566
|
-
async batchUpsert(docs, namespace) {
|
|
567
|
-
const BATCH = 100;
|
|
568
|
-
for (let i = 0; i < docs.length; i += BATCH) {
|
|
569
|
-
const records = docs.slice(i, i + BATCH).map((d) => {
|
|
570
|
-
var _a;
|
|
571
|
-
return {
|
|
572
|
-
id: d.id,
|
|
573
|
-
values: d.vector,
|
|
574
|
-
metadata: __spreadValues({ content: d.content }, (_a = d.metadata) != null ? _a : {})
|
|
575
|
-
};
|
|
576
|
-
});
|
|
577
|
-
await this.index(namespace).upsert({ records });
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
async query(vector, topK, namespace, filter) {
|
|
581
|
-
var _a;
|
|
582
|
-
const result = await this.index(namespace).query(__spreadValues({
|
|
583
|
-
vector,
|
|
584
|
-
topK,
|
|
585
|
-
includeMetadata: true
|
|
586
|
-
}, filter ? { filter } : {}));
|
|
587
|
-
return ((_a = result.matches) != null ? _a : []).map((m) => {
|
|
588
|
-
var _a2, _b;
|
|
589
|
-
return {
|
|
590
|
-
id: m.id,
|
|
591
|
-
score: (_a2 = m.score) != null ? _a2 : 0,
|
|
592
|
-
content: ((_b = m.metadata) == null ? void 0 : _b.content) || "",
|
|
593
|
-
metadata: m.metadata
|
|
594
|
-
};
|
|
595
|
-
});
|
|
596
|
-
}
|
|
597
|
-
async delete(id, namespace) {
|
|
598
|
-
await this.index(namespace).deleteOne({ id });
|
|
599
|
-
}
|
|
600
|
-
async deleteNamespace(namespace) {
|
|
601
|
-
await this.client.index(this.indexName).namespace(namespace).deleteAll();
|
|
602
|
-
}
|
|
603
|
-
async ping() {
|
|
604
|
-
try {
|
|
605
|
-
await this.client.listIndexes();
|
|
606
|
-
return true;
|
|
607
|
-
} catch (e) {
|
|
608
|
-
return false;
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
async disconnect() {
|
|
612
|
-
}
|
|
613
|
-
};
|
|
614
|
-
}
|
|
615
|
-
});
|
|
616
|
-
|
|
617
|
-
// src/providers/vectordb/PostgreSQLProvider.ts
|
|
618
|
-
var PostgreSQLProvider_exports = {};
|
|
619
|
-
__export(PostgreSQLProvider_exports, {
|
|
620
|
-
PostgreSQLProvider: () => PostgreSQLProvider
|
|
621
|
-
});
|
|
622
|
-
var import_pg, PostgreSQLProvider;
|
|
623
|
-
var init_PostgreSQLProvider = __esm({
|
|
624
|
-
"src/providers/vectordb/PostgreSQLProvider.ts"() {
|
|
625
|
-
"use strict";
|
|
626
|
-
import_pg = require("pg");
|
|
627
|
-
init_BaseVectorProvider();
|
|
628
|
-
PostgreSQLProvider = class extends BaseVectorProvider {
|
|
629
|
-
constructor(config) {
|
|
630
|
-
var _a;
|
|
631
|
-
super(config);
|
|
632
|
-
this.tableName = this.indexName.replace(/[^a-z0-9_]/gi, "_");
|
|
633
|
-
const opts = config.options;
|
|
634
|
-
if (!opts.connectionString) throw new Error("[PostgreSQLProvider] options.connectionString is required");
|
|
635
|
-
this.connectionString = opts.connectionString;
|
|
636
|
-
this.dimensions = (_a = opts.dimensions) != null ? _a : 1536;
|
|
637
|
-
}
|
|
638
|
-
async initialize() {
|
|
639
|
-
this.pool = new import_pg.Pool({ connectionString: this.connectionString });
|
|
640
|
-
const client = await this.pool.connect();
|
|
641
|
-
try {
|
|
642
|
-
await client.query("CREATE EXTENSION IF NOT EXISTS vector");
|
|
643
|
-
await client.query(`
|
|
644
|
-
CREATE TABLE IF NOT EXISTS ${this.tableName} (
|
|
645
|
-
id TEXT PRIMARY KEY,
|
|
646
|
-
namespace TEXT NOT NULL DEFAULT '',
|
|
647
|
-
content TEXT NOT NULL,
|
|
648
|
-
metadata JSONB,
|
|
649
|
-
embedding VECTOR(${this.dimensions})
|
|
650
|
-
)
|
|
651
|
-
`);
|
|
652
|
-
await client.query(`
|
|
653
|
-
CREATE INDEX IF NOT EXISTS ${this.tableName}_embedding_idx
|
|
654
|
-
ON ${this.tableName}
|
|
655
|
-
USING hnsw (embedding vector_cosine_ops)
|
|
656
|
-
`);
|
|
657
|
-
} finally {
|
|
658
|
-
client.release();
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
async upsert(doc, namespace = "") {
|
|
662
|
-
var _a;
|
|
663
|
-
const vectorLiteral = `[${doc.vector.join(",")}]`;
|
|
664
|
-
await this.pool.query(
|
|
665
|
-
`INSERT INTO ${this.tableName} (id, namespace, content, metadata, embedding)
|
|
666
|
-
VALUES ($1, $2, $3, $4, $5::vector)
|
|
667
|
-
ON CONFLICT (id) DO UPDATE
|
|
668
|
-
SET namespace = EXCLUDED.namespace,
|
|
669
|
-
content = EXCLUDED.content,
|
|
670
|
-
metadata = EXCLUDED.metadata,
|
|
671
|
-
embedding = EXCLUDED.embedding`,
|
|
672
|
-
[doc.id, namespace, doc.content, JSON.stringify((_a = doc.metadata) != null ? _a : {}), vectorLiteral]
|
|
673
|
-
);
|
|
674
|
-
}
|
|
675
|
-
async batchUpsert(docs, namespace = "") {
|
|
676
|
-
for (const doc of docs) {
|
|
677
|
-
await this.upsert(doc, namespace);
|
|
678
|
-
}
|
|
679
|
-
}
|
|
680
|
-
async query(vector, topK, namespace, filter) {
|
|
681
|
-
const vectorLiteral = `[${vector.join(",")}]`;
|
|
682
|
-
let whereClause = namespace ? `WHERE namespace = $3` : "";
|
|
683
|
-
const params = [vectorLiteral, topK];
|
|
684
|
-
if (namespace) params.push(namespace);
|
|
685
|
-
if (filter && Object.keys(filter).length > 0) {
|
|
686
|
-
const filterConditions = Object.entries(filter).map(([key, val]) => {
|
|
687
|
-
const paramIdx = params.length + 1;
|
|
688
|
-
params.push(JSON.stringify(val));
|
|
689
|
-
return `metadata->>'${key}' = $${paramIdx}`;
|
|
690
|
-
}).join(" AND ");
|
|
691
|
-
whereClause = whereClause ? `${whereClause} AND ${filterConditions}` : `WHERE ${filterConditions}`;
|
|
692
|
-
}
|
|
693
|
-
const result = await this.pool.query(
|
|
694
|
-
`SELECT id, content, metadata, 1 - (embedding <=> $1::vector) AS score
|
|
695
|
-
FROM ${this.tableName}
|
|
696
|
-
${whereClause}
|
|
697
|
-
ORDER BY embedding <=> $1::vector
|
|
698
|
-
LIMIT $2`,
|
|
699
|
-
params
|
|
700
|
-
);
|
|
701
|
-
return result.rows.map((row) => ({
|
|
702
|
-
id: String(row["id"]),
|
|
703
|
-
score: parseFloat(String(row["score"])),
|
|
704
|
-
content: String(row["content"]),
|
|
705
|
-
metadata: row["metadata"]
|
|
706
|
-
}));
|
|
707
|
-
}
|
|
708
|
-
async delete(id, namespace) {
|
|
709
|
-
const where = namespace ? "WHERE id = $1 AND namespace = $2" : "WHERE id = $1";
|
|
710
|
-
const params = namespace ? [id, namespace] : [id];
|
|
711
|
-
await this.pool.query(`DELETE FROM ${this.tableName} ${where}`, params);
|
|
712
|
-
}
|
|
713
|
-
async deleteNamespace(namespace) {
|
|
714
|
-
await this.pool.query(`DELETE FROM ${this.tableName} WHERE namespace = $1`, [namespace]);
|
|
715
|
-
}
|
|
716
|
-
async ping() {
|
|
717
|
-
try {
|
|
718
|
-
await this.pool.query("SELECT 1");
|
|
719
|
-
return true;
|
|
720
|
-
} catch (e) {
|
|
721
|
-
return false;
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
async disconnect() {
|
|
725
|
-
await this.pool.end();
|
|
726
|
-
}
|
|
727
|
-
};
|
|
728
|
-
}
|
|
729
|
-
});
|
|
730
|
-
|
|
731
|
-
// src/providers/vectordb/MongoDBProvider.ts
|
|
732
|
-
var MongoDBProvider_exports = {};
|
|
733
|
-
__export(MongoDBProvider_exports, {
|
|
734
|
-
MongoDBProvider: () => MongoDBProvider
|
|
735
|
-
});
|
|
736
|
-
var import_mongodb, MongoDBProvider;
|
|
737
|
-
var init_MongoDBProvider = __esm({
|
|
738
|
-
"src/providers/vectordb/MongoDBProvider.ts"() {
|
|
739
|
-
"use strict";
|
|
740
|
-
import_mongodb = require("mongodb");
|
|
741
|
-
init_BaseVectorProvider();
|
|
742
|
-
MongoDBProvider = class extends BaseVectorProvider {
|
|
743
|
-
constructor(config) {
|
|
744
|
-
super(config);
|
|
745
|
-
const opts = config.options;
|
|
746
|
-
if (!opts.uri || !opts.database || !opts.collection) {
|
|
747
|
-
throw new Error("[MongoDBProvider] options uri, database, and collection are required.");
|
|
748
|
-
}
|
|
749
|
-
this.client = new import_mongodb.MongoClient(opts.uri);
|
|
750
|
-
this.dbName = opts.database;
|
|
751
|
-
this.collectionName = opts.collection;
|
|
752
|
-
this.embeddingKey = opts.embeddingKey || "embedding";
|
|
753
|
-
this.contentKey = opts.contentKey || "content";
|
|
754
|
-
this.metadataKey = opts.metadataKey || "metadata";
|
|
755
|
-
}
|
|
756
|
-
async initialize() {
|
|
757
|
-
await this.client.connect();
|
|
758
|
-
this.db = this.client.db(this.dbName);
|
|
759
|
-
this.collection = this.db.collection(this.collectionName);
|
|
760
|
-
}
|
|
761
|
-
async upsert(doc, namespace) {
|
|
762
|
-
const document = __spreadValues({
|
|
763
|
-
_id: doc.id,
|
|
764
|
-
[this.embeddingKey]: doc.vector,
|
|
765
|
-
[this.contentKey]: doc.content,
|
|
766
|
-
[this.metadataKey]: doc.metadata || {}
|
|
767
|
-
}, namespace ? { namespace } : {});
|
|
768
|
-
await this.collection.updateOne({ _id: doc.id }, { $set: document }, { upsert: true });
|
|
769
|
-
}
|
|
770
|
-
async batchUpsert(docs, namespace) {
|
|
771
|
-
const operations = docs.map((doc) => ({
|
|
772
|
-
updateOne: {
|
|
773
|
-
filter: { _id: doc.id },
|
|
774
|
-
update: {
|
|
775
|
-
$set: __spreadValues({
|
|
776
|
-
[this.embeddingKey]: doc.vector,
|
|
777
|
-
[this.contentKey]: doc.content,
|
|
778
|
-
[this.metadataKey]: doc.metadata || {}
|
|
779
|
-
}, namespace ? { namespace } : {})
|
|
780
|
-
},
|
|
781
|
-
upsert: true
|
|
782
|
-
}
|
|
783
|
-
}));
|
|
784
|
-
await this.collection.bulkWrite(operations);
|
|
785
|
-
}
|
|
786
|
-
async query(vector, topK, namespace, filter) {
|
|
787
|
-
const pipeline = [
|
|
788
|
-
{
|
|
789
|
-
$vectorSearch: __spreadValues({
|
|
790
|
-
index: this.config.options.indexName || "vector_index",
|
|
791
|
-
path: this.embeddingKey,
|
|
792
|
-
queryVector: vector,
|
|
793
|
-
numCandidates: Math.max(topK * 10, 100),
|
|
794
|
-
limit: topK
|
|
795
|
-
}, filter || namespace ? { filter: __spreadValues(__spreadValues({}, filter || {}), namespace ? { namespace } : {}) } : {})
|
|
796
|
-
},
|
|
797
|
-
{
|
|
798
|
-
$project: {
|
|
799
|
-
score: { $meta: "vectorSearchScore" },
|
|
800
|
-
content: `$${this.contentKey}`,
|
|
801
|
-
metadata: `$${this.metadataKey}`
|
|
802
|
-
}
|
|
803
|
-
}
|
|
804
|
-
];
|
|
805
|
-
const results = await this.collection.aggregate(pipeline).toArray();
|
|
806
|
-
return results.map((res) => ({
|
|
807
|
-
id: res["_id"].toString(),
|
|
808
|
-
score: res["score"],
|
|
809
|
-
content: res["content"],
|
|
810
|
-
metadata: res["metadata"]
|
|
811
|
-
}));
|
|
812
|
-
}
|
|
813
|
-
async delete(id, namespace) {
|
|
814
|
-
await this.collection.deleteOne(__spreadValues({ _id: id }, namespace ? { namespace } : {}));
|
|
815
|
-
}
|
|
816
|
-
async deleteNamespace(namespace) {
|
|
817
|
-
await this.collection.deleteMany({ namespace });
|
|
818
|
-
}
|
|
819
|
-
async ping() {
|
|
820
|
-
try {
|
|
821
|
-
await this.db.command({ ping: 1 });
|
|
822
|
-
return true;
|
|
823
|
-
} catch (e) {
|
|
824
|
-
return false;
|
|
825
|
-
}
|
|
826
|
-
}
|
|
827
|
-
async disconnect() {
|
|
828
|
-
await this.client.close();
|
|
829
|
-
}
|
|
830
|
-
};
|
|
831
|
-
}
|
|
832
|
-
});
|
|
833
|
-
|
|
834
|
-
// src/providers/vectordb/MilvusProvider.ts
|
|
835
|
-
var MilvusProvider_exports = {};
|
|
836
|
-
__export(MilvusProvider_exports, {
|
|
837
|
-
MilvusProvider: () => MilvusProvider
|
|
838
|
-
});
|
|
839
|
-
var import_axios3, MilvusProvider;
|
|
840
|
-
var init_MilvusProvider = __esm({
|
|
841
|
-
"src/providers/vectordb/MilvusProvider.ts"() {
|
|
842
|
-
"use strict";
|
|
843
|
-
import_axios3 = __toESM(require("axios"));
|
|
844
|
-
init_BaseVectorProvider();
|
|
845
|
-
MilvusProvider = class extends BaseVectorProvider {
|
|
846
|
-
constructor(config) {
|
|
847
|
-
super(config);
|
|
848
|
-
const opts = config.options;
|
|
849
|
-
const baseUrl = opts.baseUrl || opts.uri;
|
|
850
|
-
if (!baseUrl) throw new Error("[MilvusProvider] baseUrl/uri is required");
|
|
851
|
-
this.http = import_axios3.default.create({
|
|
852
|
-
baseURL: baseUrl,
|
|
853
|
-
headers: __spreadValues(__spreadValues({
|
|
854
|
-
"Content-Type": "application/json"
|
|
855
|
-
}, opts.headers || {}), opts.apiKey ? { Authorization: `Bearer ${opts.apiKey}` } : {})
|
|
856
|
-
});
|
|
857
|
-
}
|
|
858
|
-
async initialize() {
|
|
859
|
-
await this.ping();
|
|
860
|
-
}
|
|
861
|
-
async upsert(doc, namespace) {
|
|
862
|
-
const payload = {
|
|
863
|
-
collectionName: this.indexName,
|
|
864
|
-
data: [__spreadValues({
|
|
865
|
-
vector: doc.vector,
|
|
866
|
-
content: doc.content,
|
|
867
|
-
metadata: doc.metadata || {}
|
|
868
|
-
}, namespace ? { namespace } : {})]
|
|
869
|
-
};
|
|
870
|
-
await this.http.post("/v1/vector/upsert", payload);
|
|
871
|
-
}
|
|
872
|
-
async batchUpsert(docs, namespace) {
|
|
873
|
-
const payload = {
|
|
874
|
-
collectionName: this.indexName,
|
|
875
|
-
data: docs.map((doc) => __spreadValues({
|
|
876
|
-
vector: doc.vector,
|
|
877
|
-
content: doc.content,
|
|
878
|
-
metadata: doc.metadata || {}
|
|
879
|
-
}, namespace ? { namespace } : {}))
|
|
880
|
-
};
|
|
881
|
-
await this.http.post("/v1/vector/upsert", payload);
|
|
882
|
-
}
|
|
883
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
884
|
-
async query(vector, topK, namespace, _filter) {
|
|
885
|
-
const payload = {
|
|
886
|
-
collectionName: this.indexName,
|
|
887
|
-
vector,
|
|
888
|
-
limit: topK,
|
|
889
|
-
filter: namespace ? `namespace == "${namespace}"` : void 0,
|
|
890
|
-
outputFields: ["content", "metadata"]
|
|
891
|
-
};
|
|
892
|
-
const { data } = await this.http.post("/v1/vector/search", payload);
|
|
893
|
-
return (data.data || []).map((res) => ({
|
|
894
|
-
id: String(res["id"]),
|
|
895
|
-
score: res["distance"],
|
|
896
|
-
content: res["content"],
|
|
897
|
-
metadata: res["metadata"]
|
|
898
|
-
}));
|
|
899
|
-
}
|
|
900
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
901
|
-
async delete(id, _namespace) {
|
|
902
|
-
await this.http.post("/v1/vector/delete", {
|
|
903
|
-
collectionName: this.indexName,
|
|
904
|
-
id
|
|
905
|
-
});
|
|
906
|
-
}
|
|
907
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
908
|
-
async deleteNamespace(_namespace) {
|
|
909
|
-
}
|
|
910
|
-
async ping() {
|
|
911
|
-
try {
|
|
912
|
-
await this.http.get("/v1/health");
|
|
913
|
-
return true;
|
|
914
|
-
} catch (e) {
|
|
915
|
-
return false;
|
|
916
|
-
}
|
|
917
|
-
}
|
|
918
|
-
async disconnect() {
|
|
919
|
-
}
|
|
920
|
-
};
|
|
921
|
-
}
|
|
922
|
-
});
|
|
923
|
-
|
|
924
|
-
// src/providers/vectordb/QdrantProvider.ts
|
|
925
|
-
var QdrantProvider_exports = {};
|
|
926
|
-
__export(QdrantProvider_exports, {
|
|
927
|
-
QdrantProvider: () => QdrantProvider
|
|
928
|
-
});
|
|
929
|
-
var import_axios4, QdrantProvider;
|
|
930
|
-
var init_QdrantProvider = __esm({
|
|
931
|
-
"src/providers/vectordb/QdrantProvider.ts"() {
|
|
932
|
-
"use strict";
|
|
933
|
-
import_axios4 = __toESM(require("axios"));
|
|
934
|
-
init_BaseVectorProvider();
|
|
935
|
-
QdrantProvider = class extends BaseVectorProvider {
|
|
936
|
-
constructor(config) {
|
|
937
|
-
super(config);
|
|
938
|
-
const opts = config.options;
|
|
939
|
-
const baseUrl = opts.baseUrl;
|
|
940
|
-
if (!baseUrl) throw new Error("[QdrantProvider] baseUrl is required");
|
|
941
|
-
this.http = import_axios4.default.create({
|
|
942
|
-
baseURL: baseUrl,
|
|
943
|
-
headers: __spreadValues({
|
|
944
|
-
"Content-Type": "application/json"
|
|
945
|
-
}, opts.apiKey ? { "api-key": opts.apiKey } : {})
|
|
946
|
-
});
|
|
947
|
-
}
|
|
948
|
-
async initialize() {
|
|
949
|
-
await this.ping();
|
|
950
|
-
}
|
|
951
|
-
async upsert(doc, namespace) {
|
|
952
|
-
await this.batchUpsert([doc], namespace);
|
|
953
|
-
}
|
|
954
|
-
async batchUpsert(docs, namespace) {
|
|
955
|
-
const payload = {
|
|
956
|
-
points: docs.map((doc) => ({
|
|
957
|
-
id: doc.id,
|
|
958
|
-
vector: doc.vector,
|
|
959
|
-
payload: __spreadValues({
|
|
960
|
-
content: doc.content,
|
|
961
|
-
metadata: doc.metadata || {}
|
|
962
|
-
}, namespace ? { namespace } : {})
|
|
963
|
-
}))
|
|
964
|
-
};
|
|
965
|
-
await this.http.put(`/collections/${this.indexName}/points`, payload);
|
|
966
|
-
}
|
|
967
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
968
|
-
async query(vector, topK, namespace, _filter) {
|
|
969
|
-
const payload = {
|
|
970
|
-
vector,
|
|
971
|
-
limit: topK,
|
|
972
|
-
with_payload: true,
|
|
973
|
-
filter: namespace ? {
|
|
974
|
-
must: [{ key: "namespace", match: { value: namespace } }]
|
|
975
|
-
} : void 0
|
|
976
|
-
};
|
|
977
|
-
const { data } = await this.http.post(`/collections/${this.indexName}/points/search`, payload);
|
|
978
|
-
return (data.result || []).map((res) => {
|
|
979
|
-
var _a, _b;
|
|
980
|
-
return {
|
|
981
|
-
id: res["id"],
|
|
982
|
-
score: res["score"],
|
|
983
|
-
content: ((_a = res["payload"]) == null ? void 0 : _a.content) || "",
|
|
984
|
-
metadata: ((_b = res["payload"]) == null ? void 0 : _b.metadata) || {}
|
|
985
|
-
};
|
|
986
|
-
});
|
|
987
|
-
}
|
|
988
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
989
|
-
async delete(id, _namespace) {
|
|
990
|
-
await this.http.post(`/collections/${this.indexName}/points/delete`, {
|
|
991
|
-
points: [id]
|
|
992
|
-
});
|
|
993
|
-
}
|
|
994
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
995
|
-
async deleteNamespace(_namespace) {
|
|
996
|
-
await this.http.post(`/collections/${this.indexName}/points/delete`, {
|
|
997
|
-
filter: {
|
|
998
|
-
must: [{ key: "namespace", match: { value: _namespace } }]
|
|
999
|
-
}
|
|
1000
|
-
});
|
|
1001
|
-
}
|
|
1002
|
-
async ping() {
|
|
1003
|
-
try {
|
|
1004
|
-
await this.http.get("/healthz");
|
|
1005
|
-
return true;
|
|
1006
|
-
} catch (e) {
|
|
1007
|
-
return false;
|
|
1008
|
-
}
|
|
1009
|
-
}
|
|
1010
|
-
async disconnect() {
|
|
1011
|
-
}
|
|
1012
|
-
};
|
|
1013
|
-
}
|
|
1014
|
-
});
|
|
1015
|
-
|
|
1016
|
-
// src/providers/vectordb/ChromaDBProvider.ts
|
|
1017
|
-
var ChromaDBProvider_exports = {};
|
|
1018
|
-
__export(ChromaDBProvider_exports, {
|
|
1019
|
-
ChromaDBProvider: () => ChromaDBProvider
|
|
1020
|
-
});
|
|
1021
|
-
var import_axios5, ChromaDBProvider;
|
|
1022
|
-
var init_ChromaDBProvider = __esm({
|
|
1023
|
-
"src/providers/vectordb/ChromaDBProvider.ts"() {
|
|
1024
|
-
"use strict";
|
|
1025
|
-
import_axios5 = __toESM(require("axios"));
|
|
1026
|
-
init_BaseVectorProvider();
|
|
1027
|
-
ChromaDBProvider = class extends BaseVectorProvider {
|
|
1028
|
-
constructor(config) {
|
|
1029
|
-
super(config);
|
|
1030
|
-
this.collectionId = "";
|
|
1031
|
-
const opts = config.options;
|
|
1032
|
-
const baseUrl = opts.baseUrl;
|
|
1033
|
-
if (!baseUrl) throw new Error("[ChromaDBProvider] baseUrl is required");
|
|
1034
|
-
this.http = import_axios5.default.create({
|
|
1035
|
-
baseURL: baseUrl,
|
|
1036
|
-
headers: { "Content-Type": "application/json" }
|
|
1037
|
-
});
|
|
1038
|
-
}
|
|
1039
|
-
async initialize() {
|
|
1040
|
-
const { data } = await this.http.get(`/api/v1/collections/${this.indexName}`);
|
|
1041
|
-
this.collectionId = data.id;
|
|
1042
|
-
}
|
|
1043
|
-
async upsert(doc, namespace) {
|
|
1044
|
-
await this.batchUpsert([doc], namespace);
|
|
1045
|
-
}
|
|
1046
|
-
async batchUpsert(docs, namespace) {
|
|
1047
|
-
const payload = {
|
|
1048
|
-
ids: docs.map((d) => d.id),
|
|
1049
|
-
embeddings: docs.map((d) => d.vector),
|
|
1050
|
-
documents: docs.map((d) => d.content),
|
|
1051
|
-
metadatas: docs.map((d) => __spreadValues(__spreadValues({}, d.metadata || {}), namespace ? { namespace } : {}))
|
|
1052
|
-
};
|
|
1053
|
-
await this.http.post(`/api/v1/collections/${this.collectionId}/add`, payload);
|
|
1054
|
-
}
|
|
1055
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1056
|
-
async query(vector, topK, namespace, _filter) {
|
|
1057
|
-
const payload = {
|
|
1058
|
-
query_embeddings: [vector],
|
|
1059
|
-
n_results: topK,
|
|
1060
|
-
where: namespace ? { namespace: { $eq: namespace } } : void 0
|
|
1061
|
-
};
|
|
1062
|
-
const { data } = await this.http.post(`/api/v1/collections/${this.collectionId}/query`, payload);
|
|
1063
|
-
const matches = [];
|
|
1064
|
-
if (data.ids && data.ids[0]) {
|
|
1065
|
-
for (let i = 0; i < data.ids[0].length; i++) {
|
|
1066
|
-
matches.push({
|
|
1067
|
-
id: data.ids[0][i],
|
|
1068
|
-
score: data.distances ? 1 - data.distances[0][i] : 0,
|
|
1069
|
-
content: data.documents ? data.documents[0][i] : "",
|
|
1070
|
-
metadata: data.metadatas ? data.metadatas[0][i] : {}
|
|
1071
|
-
});
|
|
1072
|
-
}
|
|
1073
|
-
}
|
|
1074
|
-
return matches;
|
|
1075
|
-
}
|
|
1076
|
-
async delete(id, _namespace) {
|
|
1077
|
-
await this.http.post(`/api/v1/collections/${this.collectionId}/delete`, {
|
|
1078
|
-
ids: [id],
|
|
1079
|
-
where: _namespace ? { namespace: { $eq: _namespace } } : void 0
|
|
1080
|
-
});
|
|
1081
|
-
}
|
|
1082
|
-
async deleteNamespace(_namespace) {
|
|
1083
|
-
await this.http.post(`/api/v1/collections/${this.collectionId}/delete`, {
|
|
1084
|
-
where: { namespace: { $eq: _namespace } }
|
|
1085
|
-
});
|
|
1086
|
-
}
|
|
1087
|
-
async ping() {
|
|
1088
|
-
try {
|
|
1089
|
-
await this.http.get("/api/v1/heartbeat");
|
|
1090
|
-
return true;
|
|
1091
|
-
} catch (e) {
|
|
1092
|
-
return false;
|
|
1093
|
-
}
|
|
1094
|
-
}
|
|
1095
|
-
async disconnect() {
|
|
1096
|
-
}
|
|
1097
|
-
};
|
|
1098
|
-
}
|
|
1099
|
-
});
|
|
1100
|
-
|
|
1101
|
-
// src/providers/vectordb/RedisProvider.ts
|
|
1102
|
-
var RedisProvider_exports = {};
|
|
1103
|
-
__export(RedisProvider_exports, {
|
|
1104
|
-
RedisProvider: () => RedisProvider
|
|
1105
|
-
});
|
|
1106
|
-
var import_axios6, RedisProvider;
|
|
1107
|
-
var init_RedisProvider = __esm({
|
|
1108
|
-
"src/providers/vectordb/RedisProvider.ts"() {
|
|
1109
|
-
"use strict";
|
|
1110
|
-
import_axios6 = __toESM(require("axios"));
|
|
1111
|
-
init_BaseVectorProvider();
|
|
1112
|
-
RedisProvider = class extends BaseVectorProvider {
|
|
1113
|
-
constructor(config) {
|
|
1114
|
-
super(config);
|
|
1115
|
-
const opts = config.options;
|
|
1116
|
-
const baseUrl = opts.baseUrl;
|
|
1117
|
-
if (!baseUrl) throw new Error("[RedisProvider] baseUrl is required");
|
|
1118
|
-
this.http = import_axios6.default.create({
|
|
1119
|
-
baseURL: baseUrl,
|
|
1120
|
-
headers: {
|
|
1121
|
-
"Content-Type": "application/json",
|
|
1122
|
-
Authorization: `Bearer ${opts.apiKey}`
|
|
1123
|
-
}
|
|
1124
|
-
});
|
|
1125
|
-
}
|
|
1126
|
-
async initialize() {
|
|
1127
|
-
await this.ping();
|
|
1128
|
-
}
|
|
1129
|
-
async upsert(doc, namespace) {
|
|
1130
|
-
const key = namespace ? `${namespace}:${doc.id}` : doc.id;
|
|
1131
|
-
await this.http.post("/", ["JSON.SET", key, "$", JSON.stringify({
|
|
1132
|
-
vector: doc.vector,
|
|
1133
|
-
content: doc.content,
|
|
1134
|
-
metadata: doc.metadata || {}
|
|
1135
|
-
})]);
|
|
1136
|
-
}
|
|
1137
|
-
async batchUpsert(docs, namespace) {
|
|
1138
|
-
for (const doc of docs) {
|
|
1139
|
-
await this.upsert(doc, namespace);
|
|
1140
|
-
}
|
|
1141
|
-
}
|
|
1142
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1143
|
-
async query(vector, topK, namespace, _filter) {
|
|
1144
|
-
const payload = {
|
|
1145
|
-
index: this.indexName,
|
|
1146
|
-
vector,
|
|
1147
|
-
topK,
|
|
1148
|
-
namespace
|
|
1149
|
-
};
|
|
1150
|
-
const { data } = await this.http.post("/search", payload);
|
|
1151
|
-
return (data.results || []).map((res) => ({
|
|
1152
|
-
id: res["id"],
|
|
1153
|
-
score: res["score"],
|
|
1154
|
-
content: res["content"],
|
|
1155
|
-
metadata: res["metadata"]
|
|
1156
|
-
}));
|
|
1157
|
-
}
|
|
1158
|
-
async delete(id, namespace) {
|
|
1159
|
-
const key = namespace ? `${namespace}:${id}` : id;
|
|
1160
|
-
await this.http.post("/", ["DEL", key]);
|
|
1161
|
-
}
|
|
1162
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1163
|
-
async deleteNamespace(_namespace) {
|
|
1164
|
-
}
|
|
1165
|
-
async ping() {
|
|
1166
|
-
try {
|
|
1167
|
-
await this.http.post("/", ["PING"]);
|
|
1168
|
-
return true;
|
|
1169
|
-
} catch (e) {
|
|
1170
|
-
return false;
|
|
1171
|
-
}
|
|
1172
|
-
}
|
|
1173
|
-
async disconnect() {
|
|
1174
|
-
}
|
|
1175
|
-
};
|
|
1176
|
-
}
|
|
1177
|
-
});
|
|
1178
|
-
|
|
1179
|
-
// src/providers/vectordb/WeaviateProvider.ts
|
|
1180
|
-
var WeaviateProvider_exports = {};
|
|
1181
|
-
__export(WeaviateProvider_exports, {
|
|
1182
|
-
WeaviateProvider: () => WeaviateProvider
|
|
1183
|
-
});
|
|
1184
|
-
var import_axios7, WeaviateProvider;
|
|
1185
|
-
var init_WeaviateProvider = __esm({
|
|
1186
|
-
"src/providers/vectordb/WeaviateProvider.ts"() {
|
|
1187
|
-
"use strict";
|
|
1188
|
-
import_axios7 = __toESM(require("axios"));
|
|
1189
|
-
init_BaseVectorProvider();
|
|
1190
|
-
WeaviateProvider = class extends BaseVectorProvider {
|
|
1191
|
-
constructor(config) {
|
|
1192
|
-
super(config);
|
|
1193
|
-
const opts = config.options;
|
|
1194
|
-
const baseUrl = opts.baseUrl;
|
|
1195
|
-
if (!baseUrl) throw new Error("[WeaviateProvider] baseUrl is required");
|
|
1196
|
-
this.http = import_axios7.default.create({
|
|
1197
|
-
baseURL: baseUrl,
|
|
1198
|
-
headers: __spreadValues({
|
|
1199
|
-
"Content-Type": "application/json"
|
|
1200
|
-
}, opts.apiKey ? {
|
|
1201
|
-
"X-OpenAI-Api-Key": opts.apiKey,
|
|
1202
|
-
"Authorization": `Bearer ${opts.apiKey}`
|
|
1203
|
-
} : {})
|
|
1204
|
-
});
|
|
1205
|
-
}
|
|
1206
|
-
async initialize() {
|
|
1207
|
-
await this.ping();
|
|
1208
|
-
}
|
|
1209
|
-
async upsert(doc, namespace) {
|
|
1210
|
-
const payload = {
|
|
1211
|
-
class: this.indexName,
|
|
1212
|
-
id: doc.id,
|
|
1213
|
-
vector: doc.vector,
|
|
1214
|
-
properties: {
|
|
1215
|
-
content: doc.content,
|
|
1216
|
-
metadata: JSON.stringify(doc.metadata || {}),
|
|
1217
|
-
namespace: namespace || ""
|
|
1218
|
-
}
|
|
1219
|
-
};
|
|
1220
|
-
await this.http.post("/v1/objects", payload);
|
|
1221
|
-
}
|
|
1222
|
-
async batchUpsert(docs, namespace) {
|
|
1223
|
-
const payload = {
|
|
1224
|
-
objects: docs.map((doc) => ({
|
|
1225
|
-
class: this.indexName,
|
|
1226
|
-
id: doc.id,
|
|
1227
|
-
vector: doc.vector,
|
|
1228
|
-
properties: {
|
|
1229
|
-
content: doc.content,
|
|
1230
|
-
metadata: JSON.stringify(doc.metadata || {}),
|
|
1231
|
-
namespace: namespace || ""
|
|
1232
|
-
}
|
|
1233
|
-
}))
|
|
1234
|
-
};
|
|
1235
|
-
await this.http.post("/v1/batch/objects", payload);
|
|
1236
|
-
}
|
|
1237
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1238
|
-
async query(vector, topK, namespace, _filter) {
|
|
1239
|
-
var _a, _b;
|
|
1240
|
-
const graphqlQuery = {
|
|
1241
|
-
query: `
|
|
1242
|
-
{
|
|
1243
|
-
Get {
|
|
1244
|
-
${this.indexName}(
|
|
1245
|
-
nearVector: {
|
|
1246
|
-
vector: ${JSON.stringify(vector)}
|
|
1247
|
-
}
|
|
1248
|
-
limit: ${topK}
|
|
1249
|
-
${namespace ? `where: { path: ["namespace"], operator: Equal, valueString: "${namespace}" }` : ""}
|
|
1250
|
-
) {
|
|
1251
|
-
content
|
|
1252
|
-
metadata
|
|
1253
|
-
_additional {
|
|
1254
|
-
id
|
|
1255
|
-
distance
|
|
1256
|
-
}
|
|
1257
|
-
}
|
|
1258
|
-
}
|
|
1259
|
-
}
|
|
1260
|
-
`
|
|
1261
|
-
};
|
|
1262
|
-
const { data } = await this.http.post("/v1/graphql", graphqlQuery);
|
|
1263
|
-
const results = ((_b = (_a = data.data) == null ? void 0 : _a.Get) == null ? void 0 : _b[this.indexName]) || [];
|
|
1264
|
-
return results.map((res) => ({
|
|
1265
|
-
id: res["_additional"].id,
|
|
1266
|
-
score: 1 - res["_additional"].distance,
|
|
1267
|
-
content: res["content"],
|
|
1268
|
-
metadata: res["metadata"] ? JSON.parse(res["metadata"]) : {}
|
|
1269
|
-
}));
|
|
1270
|
-
}
|
|
1271
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1272
|
-
async delete(id, _namespace) {
|
|
1273
|
-
await this.http.delete(`/v1/objects/${this.indexName}/${id}`);
|
|
1274
|
-
}
|
|
1275
|
-
async deleteNamespace(namespace) {
|
|
1276
|
-
await this.http.post("/v1/batch/objects", {
|
|
1277
|
-
match: {
|
|
1278
|
-
class: this.indexName,
|
|
1279
|
-
where: { path: ["namespace"], operator: "Equal", valueString: namespace }
|
|
1280
|
-
}
|
|
1281
|
-
});
|
|
1282
|
-
}
|
|
1283
|
-
async ping() {
|
|
1284
|
-
try {
|
|
1285
|
-
await this.http.get("/v1/.well-known/live");
|
|
1286
|
-
return true;
|
|
1287
|
-
} catch (e) {
|
|
1288
|
-
return false;
|
|
1289
|
-
}
|
|
1290
|
-
}
|
|
1291
|
-
async disconnect() {
|
|
1292
|
-
}
|
|
1293
|
-
};
|
|
1294
|
-
}
|
|
1295
|
-
});
|
|
1296
|
-
|
|
1297
44
|
// src/index.ts
|
|
1298
45
|
var index_exports = {};
|
|
1299
46
|
__export(index_exports, {
|
|
1300
47
|
ChatWidget: () => ChatWidget,
|
|
1301
48
|
ChatWindow: () => ChatWindow,
|
|
1302
49
|
ConfigProvider: () => ConfigProvider,
|
|
1303
|
-
ConfigResolver: () => ConfigResolver,
|
|
1304
50
|
MessageBubble: () => MessageBubble,
|
|
1305
|
-
Pipeline: () => Pipeline,
|
|
1306
|
-
ProviderRegistry: () => ProviderRegistry,
|
|
1307
51
|
SourceCard: () => SourceCard,
|
|
1308
|
-
VectorPlugin: () => VectorPlugin,
|
|
1309
52
|
useConfig: () => useConfig,
|
|
1310
53
|
useRagChat: () => useRagChat
|
|
1311
54
|
});
|
|
1312
55
|
module.exports = __toCommonJS(index_exports);
|
|
1313
56
|
|
|
1314
|
-
// src/core/ConfigResolver.ts
|
|
1315
|
-
init_templateUtils();
|
|
1316
|
-
|
|
1317
|
-
// src/config/serverConfig.ts
|
|
1318
|
-
var VECTOR_DB_PROVIDERS = ["pinecone", "pgvector", "postgresql", "mongodb", "milvus", "qdrant", "chromadb", "redis", "weaviate", "rest", "universal_rest", "custom"];
|
|
1319
|
-
var LLM_PROVIDERS = ["openai", "anthropic", "ollama", "rest", "universal_rest", "custom"];
|
|
1320
|
-
var EMBEDDING_PROVIDERS = ["openai", "ollama", "rest", "universal_rest", "custom"];
|
|
1321
|
-
function readString(env, name) {
|
|
1322
|
-
var _a;
|
|
1323
|
-
const value = (_a = env[name]) == null ? void 0 : _a.trim();
|
|
1324
|
-
return value ? value : void 0;
|
|
1325
|
-
}
|
|
1326
|
-
function readNumber(env, name, fallback) {
|
|
1327
|
-
const value = env[name];
|
|
1328
|
-
if (!value) return fallback;
|
|
1329
|
-
const parsed = Number(value);
|
|
1330
|
-
if (!Number.isFinite(parsed)) {
|
|
1331
|
-
throw new Error(`[getRagConfig] ${name} must be a valid number`);
|
|
1332
|
-
}
|
|
1333
|
-
return parsed;
|
|
1334
|
-
}
|
|
1335
|
-
function readEnum(env, name, fallback, allowed) {
|
|
1336
|
-
var _a;
|
|
1337
|
-
const value = (_a = readString(env, name)) != null ? _a : fallback;
|
|
1338
|
-
if (allowed.includes(value)) {
|
|
1339
|
-
return value;
|
|
1340
|
-
}
|
|
1341
|
-
throw new Error(`[getRagConfig] ${name} must be one of: ${allowed.join(", ")}`);
|
|
1342
|
-
}
|
|
1343
|
-
function getRagConfig(env = process.env) {
|
|
1344
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I, _J;
|
|
1345
|
-
const projectId = (_b = (_a = readString(env, "RAG_PROJECT_ID")) != null ? _a : readString(env, "NEXT_PUBLIC_PROJECT_ID")) != null ? _b : "__default__";
|
|
1346
|
-
const vectorProvider = readEnum(env, "VECTOR_DB_PROVIDER", "pinecone", VECTOR_DB_PROVIDERS);
|
|
1347
|
-
const llmProvider = readEnum(env, "LLM_PROVIDER", "openai", LLM_PROVIDERS);
|
|
1348
|
-
const embeddingProvider = readEnum(env, "EMBEDDING_PROVIDER", "openai", EMBEDDING_PROVIDERS);
|
|
1349
|
-
const embeddingDimensions = readNumber(env, "EMBEDDING_DIMENSIONS", 1536);
|
|
1350
|
-
const vectorDbOptions = {};
|
|
1351
|
-
if (vectorProvider === "pinecone") {
|
|
1352
|
-
vectorDbOptions.apiKey = (_c = readString(env, "PINECONE_API_KEY")) != null ? _c : "";
|
|
1353
|
-
vectorDbOptions.environment = (_d = readString(env, "PINECONE_ENVIRONMENT")) != null ? _d : "";
|
|
1354
|
-
} else if (vectorProvider === "pgvector") {
|
|
1355
|
-
vectorDbOptions.connectionString = (_e = readString(env, "PGVECTOR_CONNECTION_STRING")) != null ? _e : "";
|
|
1356
|
-
vectorDbOptions.dimensions = embeddingDimensions;
|
|
1357
|
-
} else if (vectorProvider === "rest") {
|
|
1358
|
-
vectorDbOptions.baseUrl = (_f = readString(env, "VECTOR_DB_REST_URL")) != null ? _f : "";
|
|
1359
|
-
vectorDbOptions.headers = readString(env, "VECTOR_DB_REST_API_KEY") ? { "api-key": readString(env, "VECTOR_DB_REST_API_KEY") } : {};
|
|
1360
|
-
} else if (vectorProvider === "universal_rest") {
|
|
1361
|
-
vectorDbOptions.baseUrl = (_h = (_g = readString(env, "VECTOR_BASE_URL")) != null ? _g : readString(env, "VECTOR_DB_REST_URL")) != null ? _h : "";
|
|
1362
|
-
vectorDbOptions.profile = readString(env, "VECTOR_UNIVERSAL_PROFILE");
|
|
1363
|
-
vectorDbOptions.headers = readString(env, "VECTOR_DB_REST_API_KEY") ? { Authorization: `Bearer ${readString(env, "VECTOR_DB_REST_API_KEY")}` } : {};
|
|
1364
|
-
} else if (vectorProvider === "mongodb") {
|
|
1365
|
-
vectorDbOptions.uri = (_i = readString(env, "MONGODB_URI")) != null ? _i : "";
|
|
1366
|
-
vectorDbOptions.database = (_j = readString(env, "MONGODB_DB")) != null ? _j : "";
|
|
1367
|
-
vectorDbOptions.collection = (_k = readString(env, "MONGODB_COLLECTION")) != null ? _k : "";
|
|
1368
|
-
vectorDbOptions.indexName = (_l = readString(env, "MONGODB_INDEX_NAME")) != null ? _l : "vector_index";
|
|
1369
|
-
}
|
|
1370
|
-
const llmApiKeyByProvider = {
|
|
1371
|
-
openai: readString(env, "OPENAI_API_KEY"),
|
|
1372
|
-
anthropic: readString(env, "ANTHROPIC_API_KEY"),
|
|
1373
|
-
ollama: void 0,
|
|
1374
|
-
universal_rest: readString(env, "LLM_API_KEY")
|
|
1375
|
-
};
|
|
1376
|
-
const embeddingApiKeyByProvider = {
|
|
1377
|
-
openai: readString(env, "OPENAI_API_KEY"),
|
|
1378
|
-
ollama: void 0,
|
|
1379
|
-
custom: (_m = readString(env, "EMBEDDING_API_KEY")) != null ? _m : readString(env, "OPENAI_API_KEY")
|
|
1380
|
-
};
|
|
1381
|
-
return {
|
|
1382
|
-
projectId,
|
|
1383
|
-
vectorDb: {
|
|
1384
|
-
provider: vectorProvider,
|
|
1385
|
-
indexName: (_n = readString(env, "VECTOR_DB_INDEX")) != null ? _n : "rag-index",
|
|
1386
|
-
options: vectorDbOptions
|
|
1387
|
-
},
|
|
1388
|
-
llm: {
|
|
1389
|
-
provider: llmProvider,
|
|
1390
|
-
model: (_o = readString(env, "LLM_MODEL")) != null ? _o : "gpt-4o",
|
|
1391
|
-
apiKey: (_p = llmApiKeyByProvider[llmProvider]) != null ? _p : "",
|
|
1392
|
-
baseUrl: readString(env, "LLM_BASE_URL"),
|
|
1393
|
-
systemPrompt: readString(env, "LLM_SYSTEM_PROMPT"),
|
|
1394
|
-
maxTokens: readNumber(env, "LLM_MAX_TOKENS", 1024),
|
|
1395
|
-
temperature: readNumber(env, "LLM_TEMPERATURE", 0.7),
|
|
1396
|
-
options: {
|
|
1397
|
-
profile: readString(env, "LLM_UNIVERSAL_PROFILE")
|
|
1398
|
-
}
|
|
1399
|
-
},
|
|
1400
|
-
embedding: {
|
|
1401
|
-
provider: embeddingProvider,
|
|
1402
|
-
model: (_q = readString(env, "EMBEDDING_MODEL")) != null ? _q : "text-embedding-3-small",
|
|
1403
|
-
apiKey: embeddingApiKeyByProvider[embeddingProvider],
|
|
1404
|
-
baseUrl: readString(env, "EMBEDDING_BASE_URL"),
|
|
1405
|
-
dimensions: embeddingDimensions,
|
|
1406
|
-
options: {
|
|
1407
|
-
profile: readString(env, "EMBEDDING_UNIVERSAL_PROFILE")
|
|
1408
|
-
}
|
|
1409
|
-
},
|
|
1410
|
-
ui: {
|
|
1411
|
-
title: (_s = (_r = readString(env, "NEXT_PUBLIC_UI_TITLE")) != null ? _r : readString(env, "UI_TITLE")) != null ? _s : "AI Assistant",
|
|
1412
|
-
subtitle: (_u = (_t = readString(env, "NEXT_PUBLIC_UI_SUBTITLE")) != null ? _t : readString(env, "UI_SUBTITLE")) != null ? _u : "Powered by RAG",
|
|
1413
|
-
primaryColor: (_w = (_v = readString(env, "NEXT_PUBLIC_PRIMARY_COLOR")) != null ? _v : readString(env, "UI_PRIMARY_COLOR")) != null ? _w : "#10b981",
|
|
1414
|
-
accentColor: (_y = (_x = readString(env, "NEXT_PUBLIC_ACCENT_COLOR")) != null ? _x : readString(env, "UI_ACCENT_COLOR")) != null ? _y : "#3b82f6",
|
|
1415
|
-
logoUrl: (_z = readString(env, "NEXT_PUBLIC_LOGO_URL")) != null ? _z : readString(env, "UI_LOGO_URL"),
|
|
1416
|
-
placeholder: (_B = (_A = readString(env, "NEXT_PUBLIC_PLACEHOLDER")) != null ? _A : readString(env, "UI_PLACEHOLDER")) != null ? _B : "Ask me anything\u2026",
|
|
1417
|
-
showSources: ((_D = (_C = readString(env, "NEXT_PUBLIC_SHOW_SOURCES")) != null ? _C : readString(env, "UI_SHOW_SOURCES")) != null ? _D : "true") !== "false",
|
|
1418
|
-
welcomeMessage: (_F = (_E = readString(env, "NEXT_PUBLIC_WELCOME_MESSAGE")) != null ? _E : readString(env, "UI_WELCOME_MESSAGE")) != null ? _F : "Hello! I'm your AI assistant. Ask me anything about your documents.",
|
|
1419
|
-
visualStyle: (_H = (_G = readString(env, "NEXT_PUBLIC_UI_VISUAL_STYLE")) != null ? _G : readString(env, "UI_VISUAL_STYLE")) != null ? _H : "glass",
|
|
1420
|
-
borderRadius: (_J = (_I = readString(env, "NEXT_PUBLIC_UI_BORDER_RADIUS")) != null ? _I : readString(env, "UI_BORDER_RADIUS")) != null ? _J : "xl"
|
|
1421
|
-
},
|
|
1422
|
-
rag: {
|
|
1423
|
-
topK: readNumber(env, "RAG_TOP_K", 5),
|
|
1424
|
-
scoreThreshold: readNumber(env, "RAG_SCORE_THRESHOLD", 0),
|
|
1425
|
-
chunkSize: readNumber(env, "RAG_CHUNK_SIZE", 1e3),
|
|
1426
|
-
chunkOverlap: readNumber(env, "RAG_CHUNK_OVERLAP", 200)
|
|
1427
|
-
}
|
|
1428
|
-
};
|
|
1429
|
-
}
|
|
1430
|
-
|
|
1431
|
-
// src/core/ConfigResolver.ts
|
|
1432
|
-
var ConfigResolver = class {
|
|
1433
|
-
/**
|
|
1434
|
-
* Resolves the final configuration by merging host-provided config with defaults.
|
|
1435
|
-
* @param hostConfig - Partial configuration passed from the host application.
|
|
1436
|
-
*/
|
|
1437
|
-
static resolve(hostConfig) {
|
|
1438
|
-
const envConfig = getRagConfig();
|
|
1439
|
-
if (!hostConfig) {
|
|
1440
|
-
return envConfig;
|
|
1441
|
-
}
|
|
1442
|
-
return __spreadProps(__spreadValues(__spreadValues({}, envConfig), hostConfig), {
|
|
1443
|
-
projectId: hostConfig.projectId || envConfig.projectId,
|
|
1444
|
-
vectorDb: hostConfig.vectorDb ? __spreadProps(__spreadValues(__spreadValues({}, envConfig.vectorDb), hostConfig.vectorDb), {
|
|
1445
|
-
options: __spreadValues(__spreadValues({}, envConfig.vectorDb.options), hostConfig.vectorDb.options || {})
|
|
1446
|
-
}) : envConfig.vectorDb,
|
|
1447
|
-
llm: hostConfig.llm ? __spreadProps(__spreadValues(__spreadValues({}, envConfig.llm), hostConfig.llm), {
|
|
1448
|
-
options: __spreadValues(__spreadValues({}, envConfig.llm.options || {}), hostConfig.llm.options || {})
|
|
1449
|
-
}) : envConfig.llm,
|
|
1450
|
-
embedding: hostConfig.embedding ? __spreadProps(__spreadValues(__spreadValues({}, envConfig.embedding), hostConfig.embedding), {
|
|
1451
|
-
options: __spreadValues(__spreadValues({}, envConfig.embedding.options || {}), hostConfig.embedding.options || {})
|
|
1452
|
-
}) : envConfig.embedding,
|
|
1453
|
-
ui: hostConfig.ui ? mergeDefined(envConfig.ui, hostConfig.ui) : envConfig.ui,
|
|
1454
|
-
rag: hostConfig.rag ? __spreadValues(__spreadValues({}, envConfig.rag), hostConfig.rag) : envConfig.rag
|
|
1455
|
-
});
|
|
1456
|
-
}
|
|
1457
|
-
/**
|
|
1458
|
-
* Validates the configuration for required fields.
|
|
1459
|
-
*/
|
|
1460
|
-
static validate(config) {
|
|
1461
|
-
if (!config.projectId) {
|
|
1462
|
-
throw new Error("[ConfigResolver] projectId is required");
|
|
1463
|
-
}
|
|
1464
|
-
if (!config.vectorDb.provider) {
|
|
1465
|
-
throw new Error("[ConfigResolver] vectorDb.provider is required");
|
|
1466
|
-
}
|
|
1467
|
-
if (!config.llm.provider) {
|
|
1468
|
-
throw new Error("[ConfigResolver] llm.provider is required");
|
|
1469
|
-
}
|
|
1470
|
-
}
|
|
1471
|
-
};
|
|
1472
|
-
|
|
1473
|
-
// src/rag/DocumentChunker.ts
|
|
1474
|
-
var DocumentChunker = class {
|
|
1475
|
-
constructor(chunkSize = 1e3, chunkOverlap = 200) {
|
|
1476
|
-
this.chunkSize = chunkSize;
|
|
1477
|
-
this.chunkOverlap = chunkOverlap;
|
|
1478
|
-
}
|
|
1479
|
-
/**
|
|
1480
|
-
* Split a single text string into overlapping chunks.
|
|
1481
|
-
*/
|
|
1482
|
-
chunk(text, options = {}) {
|
|
1483
|
-
const {
|
|
1484
|
-
chunkSize = this.chunkSize,
|
|
1485
|
-
chunkOverlap = this.chunkOverlap,
|
|
1486
|
-
docId = `doc_${Date.now()}`,
|
|
1487
|
-
metadata = {}
|
|
1488
|
-
} = options;
|
|
1489
|
-
const cleaned = text.replace(/\r\n/g, "\n").trim();
|
|
1490
|
-
if (!cleaned) return [];
|
|
1491
|
-
const sentences = cleaned.split(new RegExp("(?<=[.!?])\\s+|\\n{2,}")).map((s) => s.trim()).filter(Boolean);
|
|
1492
|
-
const chunks = [];
|
|
1493
|
-
let current = "";
|
|
1494
|
-
let chunkIndex = 0;
|
|
1495
|
-
for (const sentence of sentences) {
|
|
1496
|
-
if ((current + " " + sentence).trim().length <= chunkSize) {
|
|
1497
|
-
current = current ? `${current} ${sentence}` : sentence;
|
|
1498
|
-
} else {
|
|
1499
|
-
if (current) {
|
|
1500
|
-
chunks.push({
|
|
1501
|
-
id: `${docId}_chunk_${chunkIndex++}`,
|
|
1502
|
-
content: current.trim(),
|
|
1503
|
-
metadata: __spreadProps(__spreadValues({}, metadata), { docId, chunkIndex: chunkIndex - 1 })
|
|
1504
|
-
});
|
|
1505
|
-
}
|
|
1506
|
-
if (chunkOverlap > 0 && current.length > chunkOverlap) {
|
|
1507
|
-
current = current.slice(-chunkOverlap) + " " + sentence;
|
|
1508
|
-
} else {
|
|
1509
|
-
current = sentence;
|
|
1510
|
-
}
|
|
1511
|
-
}
|
|
1512
|
-
}
|
|
1513
|
-
if (current.trim()) {
|
|
1514
|
-
chunks.push({
|
|
1515
|
-
id: `${docId}_chunk_${chunkIndex}`,
|
|
1516
|
-
content: current.trim(),
|
|
1517
|
-
metadata: __spreadProps(__spreadValues({}, metadata), { docId, chunkIndex })
|
|
1518
|
-
});
|
|
1519
|
-
}
|
|
1520
|
-
return chunks;
|
|
1521
|
-
}
|
|
1522
|
-
/**
|
|
1523
|
-
* Chunk multiple documents at once.
|
|
1524
|
-
*/
|
|
1525
|
-
chunkMany(documents) {
|
|
1526
|
-
return documents.flatMap(
|
|
1527
|
-
(doc) => this.chunk(doc.content, { docId: doc.docId, metadata: doc.metadata })
|
|
1528
|
-
);
|
|
1529
|
-
}
|
|
1530
|
-
};
|
|
1531
|
-
|
|
1532
|
-
// src/core/ProviderRegistry.ts
|
|
1533
|
-
init_LLMFactory();
|
|
1534
|
-
var ProviderRegistry = class {
|
|
1535
|
-
/**
|
|
1536
|
-
* Register a custom vector provider.
|
|
1537
|
-
*/
|
|
1538
|
-
static registerVectorProvider(name, providerClass) {
|
|
1539
|
-
this.vectorProviders[name] = providerClass;
|
|
1540
|
-
}
|
|
1541
|
-
/**
|
|
1542
|
-
* Creates a vector database provider based on the configuration.
|
|
1543
|
-
*/
|
|
1544
|
-
static async createVectorProvider(config) {
|
|
1545
|
-
const { provider } = config;
|
|
1546
|
-
if (this.vectorProviders[provider]) {
|
|
1547
|
-
return new this.vectorProviders[provider](config);
|
|
1548
|
-
}
|
|
1549
|
-
switch (provider) {
|
|
1550
|
-
case "pinecone":
|
|
1551
|
-
const { PineconeProvider: PineconeProvider2 } = await Promise.resolve().then(() => (init_PineconeProvider(), PineconeProvider_exports));
|
|
1552
|
-
return new PineconeProvider2(config);
|
|
1553
|
-
case "pgvector":
|
|
1554
|
-
case "postgresql":
|
|
1555
|
-
const { PostgreSQLProvider: PostgreSQLProvider2 } = await Promise.resolve().then(() => (init_PostgreSQLProvider(), PostgreSQLProvider_exports));
|
|
1556
|
-
return new PostgreSQLProvider2(config);
|
|
1557
|
-
case "mongodb":
|
|
1558
|
-
const { MongoDBProvider: MongoDBProvider2 } = await Promise.resolve().then(() => (init_MongoDBProvider(), MongoDBProvider_exports));
|
|
1559
|
-
return new MongoDBProvider2(config);
|
|
1560
|
-
case "milvus":
|
|
1561
|
-
const { MilvusProvider: MilvusProvider2 } = await Promise.resolve().then(() => (init_MilvusProvider(), MilvusProvider_exports));
|
|
1562
|
-
return new MilvusProvider2(config);
|
|
1563
|
-
case "qdrant":
|
|
1564
|
-
const { QdrantProvider: QdrantProvider2 } = await Promise.resolve().then(() => (init_QdrantProvider(), QdrantProvider_exports));
|
|
1565
|
-
return new QdrantProvider2(config);
|
|
1566
|
-
case "chromadb":
|
|
1567
|
-
const { ChromaDBProvider: ChromaDBProvider2 } = await Promise.resolve().then(() => (init_ChromaDBProvider(), ChromaDBProvider_exports));
|
|
1568
|
-
return new ChromaDBProvider2(config);
|
|
1569
|
-
case "redis":
|
|
1570
|
-
const { RedisProvider: RedisProvider2 } = await Promise.resolve().then(() => (init_RedisProvider(), RedisProvider_exports));
|
|
1571
|
-
return new RedisProvider2(config);
|
|
1572
|
-
case "weaviate":
|
|
1573
|
-
const { WeaviateProvider: WeaviateProvider2 } = await Promise.resolve().then(() => (init_WeaviateProvider(), WeaviateProvider_exports));
|
|
1574
|
-
return new WeaviateProvider2(config);
|
|
1575
|
-
default:
|
|
1576
|
-
throw new Error(`[ProviderRegistry] Unsupported vector provider: ${provider}`);
|
|
1577
|
-
}
|
|
1578
|
-
}
|
|
1579
|
-
/**
|
|
1580
|
-
* Creates an LLM provider based on the configuration.
|
|
1581
|
-
*/
|
|
1582
|
-
static createLLMProvider(llmConfig, embeddingConfig) {
|
|
1583
|
-
return LLMFactory.create(llmConfig, embeddingConfig);
|
|
1584
|
-
}
|
|
1585
|
-
};
|
|
1586
|
-
ProviderRegistry.vectorProviders = {};
|
|
1587
|
-
|
|
1588
|
-
// src/core/Pipeline.ts
|
|
1589
|
-
var Pipeline = class {
|
|
1590
|
-
constructor(config) {
|
|
1591
|
-
this.initialised = false;
|
|
1592
|
-
var _a, _b, _c, _d;
|
|
1593
|
-
this.config = config;
|
|
1594
|
-
this.chunker = new DocumentChunker(
|
|
1595
|
-
(_b = (_a = config.rag) == null ? void 0 : _a.chunkSize) != null ? _b : 1e3,
|
|
1596
|
-
(_d = (_c = config.rag) == null ? void 0 : _c.chunkOverlap) != null ? _d : 200
|
|
1597
|
-
);
|
|
1598
|
-
}
|
|
1599
|
-
async initialize() {
|
|
1600
|
-
if (this.initialised) return;
|
|
1601
|
-
this.vectorDB = await ProviderRegistry.createVectorProvider(this.config.vectorDb);
|
|
1602
|
-
this.llmProvider = ProviderRegistry.createLLMProvider(this.config.llm, this.config.embedding);
|
|
1603
|
-
if (this.config.llm.provider === "anthropic") {
|
|
1604
|
-
const { LLMFactory: LLMFactory2 } = await Promise.resolve().then(() => (init_LLMFactory(), LLMFactory_exports));
|
|
1605
|
-
this.embeddingProvider = LLMFactory2.createEmbeddingProvider(this.config.embedding);
|
|
1606
|
-
} else {
|
|
1607
|
-
this.embeddingProvider = this.llmProvider;
|
|
1608
|
-
}
|
|
1609
|
-
await this.vectorDB.initialize();
|
|
1610
|
-
this.initialised = true;
|
|
1611
|
-
}
|
|
1612
|
-
async ingest(documents, namespace) {
|
|
1613
|
-
await this.initialize();
|
|
1614
|
-
const ns = namespace != null ? namespace : this.config.projectId;
|
|
1615
|
-
const results = [];
|
|
1616
|
-
for (const doc of documents) {
|
|
1617
|
-
const chunks = this.chunker.chunk(doc.content, {
|
|
1618
|
-
docId: doc.docId,
|
|
1619
|
-
metadata: doc.metadata
|
|
1620
|
-
});
|
|
1621
|
-
const vectors = await this.embeddingProvider.batchEmbed(chunks.map((c) => c.content));
|
|
1622
|
-
const upsertDocs = chunks.map((chunk, i) => ({
|
|
1623
|
-
id: chunk.id,
|
|
1624
|
-
vector: vectors[i],
|
|
1625
|
-
content: chunk.content,
|
|
1626
|
-
metadata: chunk.metadata
|
|
1627
|
-
}));
|
|
1628
|
-
await this.vectorDB.batchUpsert(upsertDocs, ns);
|
|
1629
|
-
results.push({ docId: doc.docId, chunksIngested: chunks.length });
|
|
1630
|
-
}
|
|
1631
|
-
return results;
|
|
1632
|
-
}
|
|
1633
|
-
async ask(question, history = [], namespace) {
|
|
1634
|
-
var _a, _b, _c, _d;
|
|
1635
|
-
await this.initialize();
|
|
1636
|
-
const ns = namespace != null ? namespace : this.config.projectId;
|
|
1637
|
-
const topK = (_b = (_a = this.config.rag) == null ? void 0 : _a.topK) != null ? _b : 5;
|
|
1638
|
-
const scoreThreshold = (_d = (_c = this.config.rag) == null ? void 0 : _c.scoreThreshold) != null ? _d : 0;
|
|
1639
|
-
const queryVector = await this.embeddingProvider.embed(question);
|
|
1640
|
-
const rawMatches = await this.vectorDB.query(queryVector, topK, ns);
|
|
1641
|
-
const sources = rawMatches.filter((m) => m.score >= scoreThreshold);
|
|
1642
|
-
const context = sources.length ? sources.map((m, i) => `[Source ${i + 1}]
|
|
1643
|
-
${m.content}`).join("\n\n---\n\n") : "No relevant context found.";
|
|
1644
|
-
const messages = [...history, { role: "user", content: question }];
|
|
1645
|
-
const reply = await this.llmProvider.chat(messages, context);
|
|
1646
|
-
return { reply, sources };
|
|
1647
|
-
}
|
|
1648
|
-
async health() {
|
|
1649
|
-
await this.initialize();
|
|
1650
|
-
const [vectorDB, llm] = await Promise.all([
|
|
1651
|
-
this.vectorDB.ping().catch(() => false),
|
|
1652
|
-
this.llmProvider.ping().catch(() => false)
|
|
1653
|
-
]);
|
|
1654
|
-
return { vectorDB, llm };
|
|
1655
|
-
}
|
|
1656
|
-
};
|
|
1657
|
-
|
|
1658
|
-
// src/core/VectorPlugin.ts
|
|
1659
|
-
var VectorPlugin = class {
|
|
1660
|
-
/**
|
|
1661
|
-
* Initializes the plugin with the host configuration.
|
|
1662
|
-
* @param hostConfig - Configuration object passed from the host application.
|
|
1663
|
-
*/
|
|
1664
|
-
constructor(hostConfig) {
|
|
1665
|
-
this.config = ConfigResolver.resolve(hostConfig);
|
|
1666
|
-
ConfigResolver.validate(this.config);
|
|
1667
|
-
this.pipeline = new Pipeline(this.config);
|
|
1668
|
-
}
|
|
1669
|
-
/**
|
|
1670
|
-
* Get the current resolved configuration.
|
|
1671
|
-
*/
|
|
1672
|
-
getConfig() {
|
|
1673
|
-
return this.config;
|
|
1674
|
-
}
|
|
1675
|
-
/**
|
|
1676
|
-
* Run a chat query.
|
|
1677
|
-
*/
|
|
1678
|
-
async chat(message, history = [], namespace) {
|
|
1679
|
-
return this.pipeline.ask(message, history, namespace);
|
|
1680
|
-
}
|
|
1681
|
-
/**
|
|
1682
|
-
* Ingest documents into the vector database.
|
|
1683
|
-
*/
|
|
1684
|
-
async ingest(documents, namespace) {
|
|
1685
|
-
return this.pipeline.ingest(documents, namespace);
|
|
1686
|
-
}
|
|
1687
|
-
/**
|
|
1688
|
-
* Check the health of the connected providers.
|
|
1689
|
-
*/
|
|
1690
|
-
async health() {
|
|
1691
|
-
return this.pipeline.health();
|
|
1692
|
-
}
|
|
1693
|
-
};
|
|
1694
|
-
|
|
1695
57
|
// src/components/ChatWidget.tsx
|
|
1696
58
|
var import_react6 = require("react");
|
|
1697
59
|
var import_lucide_react4 = require("lucide-react");
|
|
@@ -1803,7 +165,22 @@ function MessageBubble({
|
|
|
1803
165
|
|
|
1804
166
|
// src/components/ConfigProvider.tsx
|
|
1805
167
|
var import_react3 = require("react");
|
|
1806
|
-
|
|
168
|
+
|
|
169
|
+
// src/utils/templateUtils.ts
|
|
170
|
+
function mergeDefined(base, override) {
|
|
171
|
+
const merged = __spreadValues({}, base || {});
|
|
172
|
+
if (!override) {
|
|
173
|
+
return merged;
|
|
174
|
+
}
|
|
175
|
+
for (const [key, value] of Object.entries(override)) {
|
|
176
|
+
if (value !== void 0 && value !== null && value !== "") {
|
|
177
|
+
merged[key] = value;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return merged;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// src/components/ConfigProvider.tsx
|
|
1807
184
|
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
1808
185
|
var defaultConfig = {
|
|
1809
186
|
projectId: "default",
|
|
@@ -1839,7 +216,7 @@ function useConfig() {
|
|
|
1839
216
|
|
|
1840
217
|
// src/hooks/useRagChat.ts
|
|
1841
218
|
var import_react4 = require("react");
|
|
1842
|
-
var
|
|
219
|
+
var import_axios = __toESM(require("axios"));
|
|
1843
220
|
|
|
1844
221
|
// src/hooks/useStoredMessages.ts
|
|
1845
222
|
var React4 = __toESM(require("react"));
|
|
@@ -1910,7 +287,7 @@ function useRagChat(projectId, options = {}) {
|
|
|
1910
287
|
setIsLoading(true);
|
|
1911
288
|
try {
|
|
1912
289
|
const history = messagesRef.current.map(({ role, content }) => ({ role, content }));
|
|
1913
|
-
const { data } = await
|
|
290
|
+
const { data } = await import_axios.default.post(apiUrl, {
|
|
1914
291
|
message: trimmed,
|
|
1915
292
|
history,
|
|
1916
293
|
namespace: namespace != null ? namespace : projectId
|
|
@@ -1925,7 +302,7 @@ function useRagChat(projectId, options = {}) {
|
|
|
1925
302
|
setMessages((prev) => [...prev, assistantMessage]);
|
|
1926
303
|
onReply == null ? void 0 : onReply(assistantMessage);
|
|
1927
304
|
} catch (err) {
|
|
1928
|
-
const msg =
|
|
305
|
+
const msg = import_axios.default.isAxiosError(err) ? (_c = (_b = (_a = err.response) == null ? void 0 : _a.data) == null ? void 0 : _b.error) != null ? _c : err.message : "Something went wrong. Please try again.";
|
|
1929
306
|
setError(msg);
|
|
1930
307
|
onError == null ? void 0 : onError(msg);
|
|
1931
308
|
} finally {
|
|
@@ -2229,12 +606,8 @@ function ChatWidget({ position = "bottom-right" }) {
|
|
|
2229
606
|
ChatWidget,
|
|
2230
607
|
ChatWindow,
|
|
2231
608
|
ConfigProvider,
|
|
2232
|
-
ConfigResolver,
|
|
2233
609
|
MessageBubble,
|
|
2234
|
-
Pipeline,
|
|
2235
|
-
ProviderRegistry,
|
|
2236
610
|
SourceCard,
|
|
2237
|
-
VectorPlugin,
|
|
2238
611
|
useConfig,
|
|
2239
612
|
useRagChat
|
|
2240
613
|
});
|