mem0ai 2.0.1 → 2.0.2
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/oss/index.d.mts +6 -2
- package/dist/oss/index.d.ts +6 -2
- package/dist/oss/index.js +88 -30
- package/dist/oss/index.js.map +1 -1
- package/dist/oss/index.mjs +88 -30
- package/dist/oss/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/oss/index.mjs
CHANGED
|
@@ -203,10 +203,50 @@ var GroqLLM = class {
|
|
|
203
203
|
};
|
|
204
204
|
|
|
205
205
|
// src/oss/src/vector_stores/memory.ts
|
|
206
|
+
import sqlite3 from "sqlite3";
|
|
207
|
+
import path from "path";
|
|
206
208
|
var MemoryVectorStore = class {
|
|
207
209
|
constructor(config) {
|
|
208
|
-
this.vectors = /* @__PURE__ */ new Map();
|
|
209
210
|
this.dimension = config.dimension || 1536;
|
|
211
|
+
this.dbPath = path.join(process.cwd(), "vector_store.db");
|
|
212
|
+
if (config.dbPath) {
|
|
213
|
+
this.dbPath = config.dbPath;
|
|
214
|
+
}
|
|
215
|
+
this.db = new sqlite3.Database(this.dbPath);
|
|
216
|
+
this.init().catch(console.error);
|
|
217
|
+
}
|
|
218
|
+
async init() {
|
|
219
|
+
await this.run(`
|
|
220
|
+
CREATE TABLE IF NOT EXISTS vectors (
|
|
221
|
+
id TEXT PRIMARY KEY,
|
|
222
|
+
vector BLOB NOT NULL,
|
|
223
|
+
payload TEXT NOT NULL
|
|
224
|
+
)
|
|
225
|
+
`);
|
|
226
|
+
}
|
|
227
|
+
async run(sql, params = []) {
|
|
228
|
+
return new Promise((resolve, reject) => {
|
|
229
|
+
this.db.run(sql, params, (err) => {
|
|
230
|
+
if (err) reject(err);
|
|
231
|
+
else resolve();
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
async all(sql, params = []) {
|
|
236
|
+
return new Promise((resolve, reject) => {
|
|
237
|
+
this.db.all(sql, params, (err, rows) => {
|
|
238
|
+
if (err) reject(err);
|
|
239
|
+
else resolve(rows);
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
async getOne(sql, params = []) {
|
|
244
|
+
return new Promise((resolve, reject) => {
|
|
245
|
+
this.db.get(sql, params, (err, row) => {
|
|
246
|
+
if (err) reject(err);
|
|
247
|
+
else resolve(row);
|
|
248
|
+
});
|
|
249
|
+
});
|
|
210
250
|
}
|
|
211
251
|
cosineSimilarity(a, b) {
|
|
212
252
|
let dotProduct = 0;
|
|
@@ -232,11 +272,11 @@ var MemoryVectorStore = class {
|
|
|
232
272
|
`Vector dimension mismatch. Expected ${this.dimension}, got ${vectors[i].length}`
|
|
233
273
|
);
|
|
234
274
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
275
|
+
const vectorBuffer = Buffer.from(new Float32Array(vectors[i]).buffer);
|
|
276
|
+
await this.run(
|
|
277
|
+
`INSERT OR REPLACE INTO vectors (id, vector, payload) VALUES (?, ?, ?)`,
|
|
278
|
+
[ids[i], vectorBuffer, JSON.stringify(payloads[i])]
|
|
279
|
+
);
|
|
240
280
|
}
|
|
241
281
|
}
|
|
242
282
|
async search(query, limit = 10, filters) {
|
|
@@ -245,13 +285,21 @@ var MemoryVectorStore = class {
|
|
|
245
285
|
`Query dimension mismatch. Expected ${this.dimension}, got ${query.length}`
|
|
246
286
|
);
|
|
247
287
|
}
|
|
288
|
+
const rows = await this.all(`SELECT * FROM vectors`);
|
|
248
289
|
const results = [];
|
|
249
|
-
for (const
|
|
250
|
-
|
|
251
|
-
|
|
290
|
+
for (const row of rows) {
|
|
291
|
+
const vector = new Float32Array(row.vector.buffer);
|
|
292
|
+
const payload = JSON.parse(row.payload);
|
|
293
|
+
const memoryVector = {
|
|
294
|
+
id: row.id,
|
|
295
|
+
vector: Array.from(vector),
|
|
296
|
+
payload
|
|
297
|
+
};
|
|
298
|
+
if (this.filterVector(memoryVector, filters)) {
|
|
299
|
+
const score = this.cosineSimilarity(query, Array.from(vector));
|
|
252
300
|
results.push({
|
|
253
|
-
id:
|
|
254
|
-
payload:
|
|
301
|
+
id: memoryVector.id,
|
|
302
|
+
payload: memoryVector.payload,
|
|
255
303
|
score
|
|
256
304
|
});
|
|
257
305
|
}
|
|
@@ -260,11 +308,14 @@ var MemoryVectorStore = class {
|
|
|
260
308
|
return results.slice(0, limit);
|
|
261
309
|
}
|
|
262
310
|
async get(vectorId) {
|
|
263
|
-
const
|
|
264
|
-
|
|
311
|
+
const row = await this.getOne(`SELECT * FROM vectors WHERE id = ?`, [
|
|
312
|
+
vectorId
|
|
313
|
+
]);
|
|
314
|
+
if (!row) return null;
|
|
315
|
+
const payload = JSON.parse(row.payload);
|
|
265
316
|
return {
|
|
266
|
-
id:
|
|
267
|
-
payload
|
|
317
|
+
id: row.id,
|
|
318
|
+
payload
|
|
268
319
|
};
|
|
269
320
|
}
|
|
270
321
|
async update(vectorId, vector, payload) {
|
|
@@ -273,27 +324,34 @@ var MemoryVectorStore = class {
|
|
|
273
324
|
`Vector dimension mismatch. Expected ${this.dimension}, got ${vector.length}`
|
|
274
325
|
);
|
|
275
326
|
}
|
|
276
|
-
const
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
});
|
|
327
|
+
const vectorBuffer = Buffer.from(new Float32Array(vector).buffer);
|
|
328
|
+
await this.run(`UPDATE vectors SET vector = ?, payload = ? WHERE id = ?`, [
|
|
329
|
+
vectorBuffer,
|
|
330
|
+
JSON.stringify(payload),
|
|
331
|
+
vectorId
|
|
332
|
+
]);
|
|
283
333
|
}
|
|
284
334
|
async delete(vectorId) {
|
|
285
|
-
this.vectors
|
|
335
|
+
await this.run(`DELETE FROM vectors WHERE id = ?`, [vectorId]);
|
|
286
336
|
}
|
|
287
337
|
async deleteCol() {
|
|
288
|
-
this.vectors
|
|
338
|
+
await this.run(`DROP TABLE IF EXISTS vectors`);
|
|
339
|
+
await this.init();
|
|
289
340
|
}
|
|
290
341
|
async list(filters, limit = 100) {
|
|
342
|
+
const rows = await this.all(`SELECT * FROM vectors`);
|
|
291
343
|
const results = [];
|
|
292
|
-
for (const
|
|
293
|
-
|
|
344
|
+
for (const row of rows) {
|
|
345
|
+
const payload = JSON.parse(row.payload);
|
|
346
|
+
const memoryVector = {
|
|
347
|
+
id: row.id,
|
|
348
|
+
vector: Array.from(new Float32Array(row.vector.buffer)),
|
|
349
|
+
payload
|
|
350
|
+
};
|
|
351
|
+
if (this.filterVector(memoryVector, filters)) {
|
|
294
352
|
results.push({
|
|
295
|
-
id:
|
|
296
|
-
payload:
|
|
353
|
+
id: memoryVector.id,
|
|
354
|
+
payload: memoryVector.payload
|
|
297
355
|
});
|
|
298
356
|
}
|
|
299
357
|
}
|
|
@@ -1171,10 +1229,10 @@ function removeCodeBlocks(text) {
|
|
|
1171
1229
|
}
|
|
1172
1230
|
|
|
1173
1231
|
// src/oss/src/storage/SQLiteManager.ts
|
|
1174
|
-
import
|
|
1232
|
+
import sqlite32 from "sqlite3";
|
|
1175
1233
|
var SQLiteManager = class {
|
|
1176
1234
|
constructor(dbPath) {
|
|
1177
|
-
this.db = new
|
|
1235
|
+
this.db = new sqlite32.Database(dbPath);
|
|
1178
1236
|
this.init().catch(console.error);
|
|
1179
1237
|
}
|
|
1180
1238
|
async init() {
|