mem0ai 2.4.6 → 3.0.0-beta.0
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/index.d.mts +92 -120
- package/dist/index.d.ts +92 -120
- package/dist/index.js +93 -169
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +93 -169
- package/dist/index.mjs.map +1 -1
- package/dist/oss/index.d.mts +31 -36
- package/dist/oss/index.d.ts +31 -36
- package/dist/oss/index.js +114 -74
- package/dist/oss/index.js.map +1 -1
- package/dist/oss/index.mjs +114 -74
- package/dist/oss/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/oss/index.mjs
CHANGED
|
@@ -37,8 +37,7 @@ var MemoryConfigSchema = z.object({
|
|
|
37
37
|
})
|
|
38
38
|
}),
|
|
39
39
|
historyDbPath: z.string().optional(),
|
|
40
|
-
|
|
41
|
-
enableGraph: z.boolean().optional(),
|
|
40
|
+
customInstructions: z.string().optional(),
|
|
42
41
|
graphStore: z.object({
|
|
43
42
|
provider: z.string(),
|
|
44
43
|
config: z.object({
|
|
@@ -50,7 +49,7 @@ var MemoryConfigSchema = z.object({
|
|
|
50
49
|
provider: z.string(),
|
|
51
50
|
config: z.record(z.string(), z.any())
|
|
52
51
|
}).optional(),
|
|
53
|
-
|
|
52
|
+
customInstructions: z.string().optional()
|
|
54
53
|
}).optional(),
|
|
55
54
|
historyStore: z.object({
|
|
56
55
|
provider: z.string(),
|
|
@@ -562,7 +561,7 @@ var MemoryVectorStore = class {
|
|
|
562
561
|
);
|
|
563
562
|
insertMany(vectors, ids, payloads);
|
|
564
563
|
}
|
|
565
|
-
async search(query,
|
|
564
|
+
async search(query, topK = 10, filters) {
|
|
566
565
|
if (query.length !== this.dimension) {
|
|
567
566
|
throw new Error(
|
|
568
567
|
`Query dimension mismatch. Expected ${this.dimension}, got ${query.length}`
|
|
@@ -592,7 +591,7 @@ var MemoryVectorStore = class {
|
|
|
592
591
|
}
|
|
593
592
|
}
|
|
594
593
|
results.sort((a, b) => (b.score || 0) - (a.score || 0));
|
|
595
|
-
return results.slice(0,
|
|
594
|
+
return results.slice(0, topK);
|
|
596
595
|
}
|
|
597
596
|
async get(vectorId) {
|
|
598
597
|
const row = this.db.prepare(`SELECT * FROM vectors WHERE id = ?`).get(vectorId);
|
|
@@ -619,7 +618,7 @@ var MemoryVectorStore = class {
|
|
|
619
618
|
this.db.exec(`DROP TABLE IF EXISTS vectors`);
|
|
620
619
|
this.init();
|
|
621
620
|
}
|
|
622
|
-
async list(filters,
|
|
621
|
+
async list(filters, topK = 100) {
|
|
623
622
|
const rows = this.db.prepare(`SELECT * FROM vectors`).all();
|
|
624
623
|
const results = [];
|
|
625
624
|
for (const row of rows) {
|
|
@@ -642,7 +641,7 @@ var MemoryVectorStore = class {
|
|
|
642
641
|
});
|
|
643
642
|
}
|
|
644
643
|
}
|
|
645
|
-
return [results.slice(0,
|
|
644
|
+
return [results.slice(0, topK), results.length];
|
|
646
645
|
}
|
|
647
646
|
async getUserId() {
|
|
648
647
|
const row = this.db.prepare(`SELECT user_id FROM memory_migrations LIMIT 1`).get();
|
|
@@ -734,12 +733,12 @@ var Qdrant = class {
|
|
|
734
733
|
points
|
|
735
734
|
});
|
|
736
735
|
}
|
|
737
|
-
async search(query,
|
|
736
|
+
async search(query, topK = 5, filters) {
|
|
738
737
|
const queryFilter = this.createFilter(filters);
|
|
739
738
|
const results = await this.client.search(this.collectionName, {
|
|
740
739
|
vector: query,
|
|
741
740
|
filter: queryFilter,
|
|
742
|
-
limit
|
|
741
|
+
limit: topK
|
|
743
742
|
});
|
|
744
743
|
return results.map((hit) => ({
|
|
745
744
|
id: String(hit.id),
|
|
@@ -776,9 +775,9 @@ var Qdrant = class {
|
|
|
776
775
|
async deleteCol() {
|
|
777
776
|
await this.client.deleteCollection(this.collectionName);
|
|
778
777
|
}
|
|
779
|
-
async list(filters,
|
|
778
|
+
async list(filters, topK = 100) {
|
|
780
779
|
const scrollRequest = {
|
|
781
|
-
limit,
|
|
780
|
+
limit: topK,
|
|
782
781
|
filter: this.createFilter(filters),
|
|
783
782
|
with_payload: true,
|
|
784
783
|
with_vectors: false
|
|
@@ -948,7 +947,7 @@ var VectorizeDB = class {
|
|
|
948
947
|
);
|
|
949
948
|
}
|
|
950
949
|
}
|
|
951
|
-
async search(query,
|
|
950
|
+
async search(query, topK = 5, filters) {
|
|
952
951
|
var _a2, _b;
|
|
953
952
|
try {
|
|
954
953
|
const result = await ((_a2 = this.client) == null ? void 0 : _a2.vectorize.indexes.query(
|
|
@@ -958,7 +957,7 @@ var VectorizeDB = class {
|
|
|
958
957
|
vector: query,
|
|
959
958
|
filter: filters,
|
|
960
959
|
returnMetadata: "all",
|
|
961
|
-
topK
|
|
960
|
+
topK
|
|
962
961
|
}
|
|
963
962
|
));
|
|
964
963
|
return ((_b = result == null ? void 0 : result.matches) == null ? void 0 : _b.map((match) => ({
|
|
@@ -1055,7 +1054,7 @@ var VectorizeDB = class {
|
|
|
1055
1054
|
);
|
|
1056
1055
|
}
|
|
1057
1056
|
}
|
|
1058
|
-
async list(filters,
|
|
1057
|
+
async list(filters, topK = 20) {
|
|
1059
1058
|
var _a2, _b;
|
|
1060
1059
|
try {
|
|
1061
1060
|
const result = await ((_a2 = this.client) == null ? void 0 : _a2.vectorize.indexes.query(
|
|
@@ -1065,7 +1064,7 @@ var VectorizeDB = class {
|
|
|
1065
1064
|
vector: Array(this.dimensions).fill(0),
|
|
1066
1065
|
// Dummy vector for listing
|
|
1067
1066
|
filter: filters,
|
|
1068
|
-
topK
|
|
1067
|
+
topK,
|
|
1069
1068
|
returnMetadata: "all"
|
|
1070
1069
|
}
|
|
1071
1070
|
));
|
|
@@ -1506,7 +1505,7 @@ var RedisDB = class {
|
|
|
1506
1505
|
throw error;
|
|
1507
1506
|
}
|
|
1508
1507
|
}
|
|
1509
|
-
async search(query,
|
|
1508
|
+
async search(query, topK = 5, filters) {
|
|
1510
1509
|
const snakeFilters = filters ? toSnakeCase(filters) : void 0;
|
|
1511
1510
|
const filterExpr = snakeFilters ? Object.entries(snakeFilters).filter(([_, value]) => value !== null).map(([key, value]) => `@${key}:{${value}}`).join(" ") : "*";
|
|
1512
1511
|
const queryVector = new Float32Array(query).buffer;
|
|
@@ -1529,13 +1528,13 @@ var RedisDB = class {
|
|
|
1529
1528
|
DIALECT: 2,
|
|
1530
1529
|
LIMIT: {
|
|
1531
1530
|
from: 0,
|
|
1532
|
-
size:
|
|
1531
|
+
size: topK
|
|
1533
1532
|
}
|
|
1534
1533
|
};
|
|
1535
1534
|
try {
|
|
1536
1535
|
const results = await this.client.ft.search(
|
|
1537
1536
|
this.indexName,
|
|
1538
|
-
`${filterExpr} =>[KNN ${
|
|
1537
|
+
`${filterExpr} =>[KNN ${topK} @embedding $vec AS __vector_score]`,
|
|
1539
1538
|
searchOptions
|
|
1540
1539
|
);
|
|
1541
1540
|
return results.documents.map((doc) => {
|
|
@@ -1700,7 +1699,7 @@ var RedisDB = class {
|
|
|
1700
1699
|
async deleteCol() {
|
|
1701
1700
|
await this.client.ft.dropIndex(this.indexName);
|
|
1702
1701
|
}
|
|
1703
|
-
async list(filters,
|
|
1702
|
+
async list(filters, topK = 100) {
|
|
1704
1703
|
const snakeFilters = filters ? toSnakeCase(filters) : void 0;
|
|
1705
1704
|
const filterExpr = snakeFilters ? Object.entries(snakeFilters).filter(([_, value]) => value !== null).map(([key, value]) => `@${key}:{${value}}`).join(" ") : "*";
|
|
1706
1705
|
const searchOptions = {
|
|
@@ -1708,7 +1707,7 @@ var RedisDB = class {
|
|
|
1708
1707
|
SORTDIR: "DESC",
|
|
1709
1708
|
LIMIT: {
|
|
1710
1709
|
from: 0,
|
|
1711
|
-
size:
|
|
1710
|
+
size: topK
|
|
1712
1711
|
}
|
|
1713
1712
|
};
|
|
1714
1713
|
const results = await this.client.ft.search(
|
|
@@ -1991,11 +1990,11 @@ See the SQL migration instructions in the code comments.`
|
|
|
1991
1990
|
throw error;
|
|
1992
1991
|
}
|
|
1993
1992
|
}
|
|
1994
|
-
async search(query,
|
|
1993
|
+
async search(query, topK = 5, filters) {
|
|
1995
1994
|
try {
|
|
1996
1995
|
const rpcQuery = {
|
|
1997
1996
|
query_embedding: query,
|
|
1998
|
-
match_count:
|
|
1997
|
+
match_count: topK
|
|
1999
1998
|
};
|
|
2000
1999
|
if (filters) {
|
|
2001
2000
|
rpcQuery.filter = filters;
|
|
@@ -2061,9 +2060,9 @@ See the SQL migration instructions in the code comments.`
|
|
|
2061
2060
|
throw error;
|
|
2062
2061
|
}
|
|
2063
2062
|
}
|
|
2064
|
-
async list(filters,
|
|
2063
|
+
async list(filters, topK = 100) {
|
|
2065
2064
|
try {
|
|
2066
|
-
let query = this.client.from(this.tableName).select("*", { count: "exact" }).limit(
|
|
2065
|
+
let query = this.client.from(this.tableName).select("*", { count: "exact" }).limit(topK);
|
|
2067
2066
|
if (filters) {
|
|
2068
2067
|
Object.entries(filters).forEach(([key, value]) => {
|
|
2069
2068
|
query = query.eq(`${this.metadataColumnName}->>${key}`, value);
|
|
@@ -3130,7 +3129,7 @@ var LangchainVectorStore = class {
|
|
|
3130
3129
|
await this.lcStore.addVectors(vectors, documents);
|
|
3131
3130
|
}
|
|
3132
3131
|
}
|
|
3133
|
-
async search(query,
|
|
3132
|
+
async search(query, topK = 5, filters) {
|
|
3134
3133
|
if (this.dimension && query.length !== this.dimension) {
|
|
3135
3134
|
throw new Error(
|
|
3136
3135
|
`Query vector dimension mismatch. Expected ${this.dimension}, got ${query.length}`
|
|
@@ -3138,7 +3137,7 @@ var LangchainVectorStore = class {
|
|
|
3138
3137
|
}
|
|
3139
3138
|
const results = await this.lcStore.similaritySearchVectorWithScore(
|
|
3140
3139
|
query,
|
|
3141
|
-
|
|
3140
|
+
topK
|
|
3142
3141
|
// Do not pass lcFilter here
|
|
3143
3142
|
);
|
|
3144
3143
|
return results.map(([doc, score]) => ({
|
|
@@ -3186,7 +3185,7 @@ var LangchainVectorStore = class {
|
|
|
3186
3185
|
);
|
|
3187
3186
|
}
|
|
3188
3187
|
}
|
|
3189
|
-
async list(filters,
|
|
3188
|
+
async list(filters, topK = 100) {
|
|
3190
3189
|
console.error(
|
|
3191
3190
|
`LangchainVectorStore: The 'list' method is not supported by the generic LangchainVectorStore wrapper.`
|
|
3192
3191
|
);
|
|
@@ -3411,12 +3410,12 @@ var AzureAISearch = class {
|
|
|
3411
3410
|
/**
|
|
3412
3411
|
* Search for similar vectors
|
|
3413
3412
|
*/
|
|
3414
|
-
async search(query,
|
|
3413
|
+
async search(query, topK = 5, filters) {
|
|
3415
3414
|
const filterExpression = filters ? this.buildFilterExpression(filters) : void 0;
|
|
3416
3415
|
const vectorQuery = {
|
|
3417
3416
|
kind: "vector",
|
|
3418
3417
|
vector: query,
|
|
3419
|
-
kNearestNeighborsCount:
|
|
3418
|
+
kNearestNeighborsCount: topK,
|
|
3420
3419
|
fields: ["vector"]
|
|
3421
3420
|
};
|
|
3422
3421
|
let searchResults;
|
|
@@ -3427,7 +3426,7 @@ var AzureAISearch = class {
|
|
|
3427
3426
|
filterMode: this.vectorFilterMode
|
|
3428
3427
|
},
|
|
3429
3428
|
filter: filterExpression,
|
|
3430
|
-
top:
|
|
3429
|
+
top: topK,
|
|
3431
3430
|
searchFields: ["payload"]
|
|
3432
3431
|
});
|
|
3433
3432
|
} else {
|
|
@@ -3437,7 +3436,7 @@ var AzureAISearch = class {
|
|
|
3437
3436
|
filterMode: this.vectorFilterMode
|
|
3438
3437
|
},
|
|
3439
3438
|
filter: filterExpression,
|
|
3440
|
-
top:
|
|
3439
|
+
top: topK
|
|
3441
3440
|
});
|
|
3442
3441
|
}
|
|
3443
3442
|
const results = [];
|
|
@@ -3543,11 +3542,11 @@ var AzureAISearch = class {
|
|
|
3543
3542
|
/**
|
|
3544
3543
|
* List all vectors in the index
|
|
3545
3544
|
*/
|
|
3546
|
-
async list(filters,
|
|
3545
|
+
async list(filters, topK = 100) {
|
|
3547
3546
|
const filterExpression = filters ? this.buildFilterExpression(filters) : void 0;
|
|
3548
3547
|
const searchResults = await this.searchClient.search("*", {
|
|
3549
3548
|
filter: filterExpression,
|
|
3550
|
-
top:
|
|
3549
|
+
top: topK
|
|
3551
3550
|
});
|
|
3552
3551
|
const results = [];
|
|
3553
3552
|
for await (const result of searchResults.results) {
|
|
@@ -3777,10 +3776,10 @@ var PGVector = class {
|
|
|
3777
3776
|
)
|
|
3778
3777
|
);
|
|
3779
3778
|
}
|
|
3780
|
-
async search(query,
|
|
3779
|
+
async search(query, topK = 5, filters) {
|
|
3781
3780
|
const filterConditions = [];
|
|
3782
3781
|
const queryVector = `[${query.join(",")}]`;
|
|
3783
|
-
const filterValues = [queryVector,
|
|
3782
|
+
const filterValues = [queryVector, topK];
|
|
3784
3783
|
let filterIndex = 3;
|
|
3785
3784
|
if (filters) {
|
|
3786
3785
|
for (const [key, value] of Object.entries(filters)) {
|
|
@@ -3843,7 +3842,7 @@ var PGVector = class {
|
|
|
3843
3842
|
`);
|
|
3844
3843
|
return result.rows.map((row) => row.table_name);
|
|
3845
3844
|
}
|
|
3846
|
-
async list(filters,
|
|
3845
|
+
async list(filters, topK = 100) {
|
|
3847
3846
|
const filterConditions = [];
|
|
3848
3847
|
const filterValues = [];
|
|
3849
3848
|
let paramIndex = 1;
|
|
@@ -3866,7 +3865,7 @@ var PGVector = class {
|
|
|
3866
3865
|
FROM ${this.collectionName}
|
|
3867
3866
|
${filterClause}
|
|
3868
3867
|
`;
|
|
3869
|
-
filterValues.push(
|
|
3868
|
+
filterValues.push(topK);
|
|
3870
3869
|
const [listResult, countResult] = await Promise.all([
|
|
3871
3870
|
this.client.query(listQuery, filterValues),
|
|
3872
3871
|
this.client.query(countQuery, filterValues.slice(0, -1))
|
|
@@ -3904,6 +3903,38 @@ var PGVector = class {
|
|
|
3904
3903
|
}
|
|
3905
3904
|
};
|
|
3906
3905
|
|
|
3906
|
+
// src/oss/src/llms/deepseek.ts
|
|
3907
|
+
var DeepSeekLLM = class extends OpenAILLM {
|
|
3908
|
+
constructor(config) {
|
|
3909
|
+
const apiKey = config.apiKey || process.env.DEEPSEEK_API_KEY;
|
|
3910
|
+
if (!apiKey) {
|
|
3911
|
+
throw new Error("DeepSeek API key is required");
|
|
3912
|
+
}
|
|
3913
|
+
super({
|
|
3914
|
+
...config,
|
|
3915
|
+
apiKey,
|
|
3916
|
+
baseURL: config.baseURL || process.env.DEEPSEEK_API_BASE || "https://api.deepseek.com",
|
|
3917
|
+
model: config.model || "deepseek-chat"
|
|
3918
|
+
});
|
|
3919
|
+
}
|
|
3920
|
+
async generateResponse(messages, responseFormat, tools) {
|
|
3921
|
+
try {
|
|
3922
|
+
return await super.generateResponse(messages, responseFormat, tools);
|
|
3923
|
+
} catch (err) {
|
|
3924
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
3925
|
+
throw new Error(`DeepSeek LLM failed: ${message}`);
|
|
3926
|
+
}
|
|
3927
|
+
}
|
|
3928
|
+
async generateChat(messages) {
|
|
3929
|
+
try {
|
|
3930
|
+
return await super.generateChat(messages);
|
|
3931
|
+
} catch (err) {
|
|
3932
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
3933
|
+
throw new Error(`DeepSeek LLM failed: ${message}`);
|
|
3934
|
+
}
|
|
3935
|
+
}
|
|
3936
|
+
};
|
|
3937
|
+
|
|
3907
3938
|
// src/oss/src/utils/factory.ts
|
|
3908
3939
|
var EmbedderFactory = class {
|
|
3909
3940
|
static create(provider, config) {
|
|
@@ -3950,6 +3981,8 @@ var LLMFactory = class {
|
|
|
3950
3981
|
return new MistralLLM(config);
|
|
3951
3982
|
case "langchain":
|
|
3952
3983
|
return new LangchainLLM(config);
|
|
3984
|
+
case "deepseek":
|
|
3985
|
+
return new DeepSeekLLM(config);
|
|
3953
3986
|
default:
|
|
3954
3987
|
throw new Error(`Unsupported LLM provider: ${provider}`);
|
|
3955
3988
|
}
|
|
@@ -4043,15 +4076,6 @@ var DEFAULT_MEMORY_CONFIG = {
|
|
|
4043
4076
|
modelProperties: void 0
|
|
4044
4077
|
}
|
|
4045
4078
|
},
|
|
4046
|
-
enableGraph: false,
|
|
4047
|
-
graphStore: {
|
|
4048
|
-
provider: "neo4j",
|
|
4049
|
-
config: {
|
|
4050
|
-
url: process.env.NEO4J_URL || "neo4j://localhost:7687",
|
|
4051
|
-
username: process.env.NEO4J_USERNAME || "neo4j",
|
|
4052
|
-
password: process.env.NEO4J_PASSWORD || "password"
|
|
4053
|
-
}
|
|
4054
|
-
},
|
|
4055
4079
|
historyStore: {
|
|
4056
4080
|
provider: "sqlite",
|
|
4057
4081
|
config: {
|
|
@@ -4140,11 +4164,8 @@ var ConfigManager = class {
|
|
|
4140
4164
|
})()
|
|
4141
4165
|
},
|
|
4142
4166
|
historyDbPath: userConfig.historyDbPath || ((_e = (_d = userConfig.historyStore) == null ? void 0 : _d.config) == null ? void 0 : _e.historyDbPath) || ((_g = (_f = DEFAULT_MEMORY_CONFIG.historyStore) == null ? void 0 : _f.config) == null ? void 0 : _g.historyDbPath),
|
|
4143
|
-
|
|
4144
|
-
graphStore: {
|
|
4145
|
-
...DEFAULT_MEMORY_CONFIG.graphStore,
|
|
4146
|
-
...userConfig.graphStore
|
|
4147
|
-
},
|
|
4167
|
+
customInstructions: userConfig.customInstructions,
|
|
4168
|
+
graphStore: userConfig.graphStore ? { ...userConfig.graphStore } : void 0,
|
|
4148
4169
|
historyStore: (() => {
|
|
4149
4170
|
var _a3, _b2;
|
|
4150
4171
|
const defaultHistoryStore = DEFAULT_MEMORY_CONFIG.historyStore;
|
|
@@ -4161,8 +4182,7 @@ var ConfigManager = class {
|
|
|
4161
4182
|
}
|
|
4162
4183
|
};
|
|
4163
4184
|
})(),
|
|
4164
|
-
disableHistory: userConfig.disableHistory || DEFAULT_MEMORY_CONFIG.disableHistory
|
|
4165
|
-
enableGraph: userConfig.enableGraph || DEFAULT_MEMORY_CONFIG.enableGraph
|
|
4185
|
+
disableHistory: userConfig.disableHistory || DEFAULT_MEMORY_CONFIG.disableHistory
|
|
4166
4186
|
};
|
|
4167
4187
|
return MemoryConfigSchema.parse(mergedConfig);
|
|
4168
4188
|
}
|
|
@@ -4345,7 +4365,7 @@ var MemoryGraph = class {
|
|
|
4345
4365
|
relations: toBeAdded
|
|
4346
4366
|
};
|
|
4347
4367
|
}
|
|
4348
|
-
async search(query, filters,
|
|
4368
|
+
async search(query, filters, topK = 100) {
|
|
4349
4369
|
const entityTypeMap = await this._retrieveNodesFromData(query, filters);
|
|
4350
4370
|
const searchOutput = await this._searchGraphDb(
|
|
4351
4371
|
Object.keys(entityTypeMap),
|
|
@@ -4380,7 +4400,7 @@ var MemoryGraph = class {
|
|
|
4380
4400
|
await session.close();
|
|
4381
4401
|
}
|
|
4382
4402
|
}
|
|
4383
|
-
async getAll(filters,
|
|
4403
|
+
async getAll(filters, topK = 100) {
|
|
4384
4404
|
const session = this.graph.session();
|
|
4385
4405
|
try {
|
|
4386
4406
|
const result = await session.run(
|
|
@@ -4389,7 +4409,7 @@ var MemoryGraph = class {
|
|
|
4389
4409
|
RETURN n.name AS source, type(r) AS relationship, m.name AS target
|
|
4390
4410
|
LIMIT toInteger($limit)
|
|
4391
4411
|
`,
|
|
4392
|
-
{ user_id: filters["userId"], limit: Math.floor(Number(
|
|
4412
|
+
{ user_id: filters["userId"], limit: Math.floor(Number(topK)) }
|
|
4393
4413
|
);
|
|
4394
4414
|
const finalResults = result.records.map((record) => ({
|
|
4395
4415
|
source: record.get("source"),
|
|
@@ -4442,7 +4462,7 @@ var MemoryGraph = class {
|
|
|
4442
4462
|
async _establishNodesRelationsFromData(data, filters, entityTypeMap) {
|
|
4443
4463
|
var _a2;
|
|
4444
4464
|
let messages;
|
|
4445
|
-
if ((_a2 = this.config.graphStore) == null ? void 0 : _a2.
|
|
4465
|
+
if ((_a2 = this.config.graphStore) == null ? void 0 : _a2.customInstructions) {
|
|
4446
4466
|
messages = [
|
|
4447
4467
|
{
|
|
4448
4468
|
role: "system",
|
|
@@ -4451,7 +4471,7 @@ var MemoryGraph = class {
|
|
|
4451
4471
|
filters["userId"]
|
|
4452
4472
|
).replace(
|
|
4453
4473
|
"CUSTOM_PROMPT",
|
|
4454
|
-
`4. ${this.config.graphStore.
|
|
4474
|
+
`4. ${this.config.graphStore.customInstructions}`
|
|
4455
4475
|
) + "\nPlease provide your response in JSON format."
|
|
4456
4476
|
},
|
|
4457
4477
|
{ role: "user", content: data }
|
|
@@ -4488,7 +4508,7 @@ Text: ${data}`
|
|
|
4488
4508
|
logger.debug(`Extracted entities: ${JSON.stringify(entities)}`);
|
|
4489
4509
|
return entities;
|
|
4490
4510
|
}
|
|
4491
|
-
async _searchGraphDb(nodeList, filters,
|
|
4511
|
+
async _searchGraphDb(nodeList, filters, topK = 100) {
|
|
4492
4512
|
const resultRelations = [];
|
|
4493
4513
|
const session = this.graph.session();
|
|
4494
4514
|
try {
|
|
@@ -4521,7 +4541,7 @@ Text: ${data}`
|
|
|
4521
4541
|
n_embedding: nEmbedding,
|
|
4522
4542
|
threshold: this.threshold,
|
|
4523
4543
|
user_id: filters["userId"],
|
|
4524
|
-
limit: Math.floor(Number(
|
|
4544
|
+
limit: Math.floor(Number(topK))
|
|
4525
4545
|
});
|
|
4526
4546
|
resultRelations.push(
|
|
4527
4547
|
...result.records.map((record) => ({
|
|
@@ -4837,6 +4857,22 @@ try {
|
|
|
4837
4857
|
}
|
|
4838
4858
|
var POSTHOG_API_KEY = "phc_hgJkUVJFYtmaJqrvf6CYN67TIQ8yhXAkWzUn9AMU4yX";
|
|
4839
4859
|
var POSTHOG_HOST = "https://us.i.posthog.com/i/v0/e/";
|
|
4860
|
+
var DEFAULT_SAMPLE_RATE = 0.1;
|
|
4861
|
+
var MEM0_TELEMETRY_SAMPLE_RATE = (() => {
|
|
4862
|
+
var _a2;
|
|
4863
|
+
try {
|
|
4864
|
+
const raw = (_a2 = process == null ? void 0 : process.env) == null ? void 0 : _a2.MEM0_TELEMETRY_SAMPLE_RATE;
|
|
4865
|
+
if (raw !== void 0) {
|
|
4866
|
+
const parsed = Number(raw);
|
|
4867
|
+
if (Number.isFinite(parsed) && parsed >= 0 && parsed <= 1) {
|
|
4868
|
+
return parsed;
|
|
4869
|
+
}
|
|
4870
|
+
}
|
|
4871
|
+
} catch (e) {
|
|
4872
|
+
}
|
|
4873
|
+
return DEFAULT_SAMPLE_RATE;
|
|
4874
|
+
})();
|
|
4875
|
+
var LIFECYCLE_EVENTS = /* @__PURE__ */ new Set(["init", "reset"]);
|
|
4840
4876
|
var UnifiedTelemetry = class {
|
|
4841
4877
|
constructor(projectApiKey, host) {
|
|
4842
4878
|
this.apiKey = projectApiKey;
|
|
@@ -4881,6 +4917,10 @@ async function captureClientEvent(eventName, instance, additionalData = {}) {
|
|
|
4881
4917
|
console.warn("No telemetry ID found for instance");
|
|
4882
4918
|
return;
|
|
4883
4919
|
}
|
|
4920
|
+
const isLifecycle = LIFECYCLE_EVENTS.has(eventName);
|
|
4921
|
+
if (!isLifecycle && Math.random() >= MEM0_TELEMETRY_SAMPLE_RATE) {
|
|
4922
|
+
return;
|
|
4923
|
+
}
|
|
4884
4924
|
const eventData = {
|
|
4885
4925
|
function: `${instance.constructor.name}`,
|
|
4886
4926
|
method: eventName,
|
|
@@ -4888,7 +4928,9 @@ async function captureClientEvent(eventName, instance, additionalData = {}) {
|
|
|
4888
4928
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
4889
4929
|
client_version: version,
|
|
4890
4930
|
client_source: "nodejs",
|
|
4891
|
-
...additionalData
|
|
4931
|
+
...additionalData,
|
|
4932
|
+
// sample_rate set AFTER the spread so callers can never override it
|
|
4933
|
+
sample_rate: isLifecycle ? 1 : MEM0_TELEMETRY_SAMPLE_RATE
|
|
4892
4934
|
};
|
|
4893
4935
|
await telemetry.captureEvent(
|
|
4894
4936
|
instance.telemetryId,
|
|
@@ -4901,7 +4943,7 @@ async function captureClientEvent(eventName, instance, additionalData = {}) {
|
|
|
4901
4943
|
var Memory = class _Memory {
|
|
4902
4944
|
constructor(config = {}) {
|
|
4903
4945
|
this.config = ConfigManager.mergeConfig(config);
|
|
4904
|
-
this.
|
|
4946
|
+
this.customInstructions = this.config.customInstructions;
|
|
4905
4947
|
this.embedder = EmbedderFactory.create(
|
|
4906
4948
|
this.config.embedder.provider,
|
|
4907
4949
|
this.config.embedder.config
|
|
@@ -4920,9 +4962,8 @@ var Memory = class _Memory {
|
|
|
4920
4962
|
}
|
|
4921
4963
|
this.collectionName = this.config.vectorStore.config.collectionName;
|
|
4922
4964
|
this.apiVersion = this.config.version || "v1.0";
|
|
4923
|
-
this.enableGraph = this.config.enableGraph || false;
|
|
4924
4965
|
this.telemetryId = "anonymous";
|
|
4925
|
-
if (this.
|
|
4966
|
+
if (this.config.graphStore) {
|
|
4926
4967
|
this.graphMemory = new MemoryGraph(this.config);
|
|
4927
4968
|
}
|
|
4928
4969
|
this._initPromise = this._autoInitialize().catch((error) => {
|
|
@@ -4977,8 +5018,7 @@ var Memory = class _Memory {
|
|
|
4977
5018
|
await captureClientEvent("init", this, {
|
|
4978
5019
|
api_version: this.apiVersion,
|
|
4979
5020
|
client_type: "Memory",
|
|
4980
|
-
collection_name: this.collectionName
|
|
4981
|
-
enable_graph: this.enableGraph
|
|
5021
|
+
collection_name: this.collectionName
|
|
4982
5022
|
});
|
|
4983
5023
|
} catch (error) {
|
|
4984
5024
|
}
|
|
@@ -5084,8 +5124,8 @@ var Memory = class _Memory {
|
|
|
5084
5124
|
return returnedMemories;
|
|
5085
5125
|
}
|
|
5086
5126
|
const parsedMessages = messages.map((m) => m.content).join("\n");
|
|
5087
|
-
const [systemPrompt, userPrompt] = this.
|
|
5088
|
-
this.
|
|
5127
|
+
const [systemPrompt, userPrompt] = this.customInstructions ? [
|
|
5128
|
+
this.customInstructions.toLowerCase().includes("json") ? this.customInstructions : `${this.customInstructions}
|
|
5089
5129
|
|
|
5090
5130
|
You MUST return a valid JSON object with a 'facts' key containing an array of strings.`,
|
|
5091
5131
|
`Input:
|
|
@@ -5239,10 +5279,10 @@ ${parsedMessages}`
|
|
|
5239
5279
|
await this._ensureInitialized();
|
|
5240
5280
|
await this._captureEvent("search", {
|
|
5241
5281
|
query_length: query.length,
|
|
5242
|
-
|
|
5282
|
+
topK: config.topK,
|
|
5243
5283
|
has_filters: !!config.filters
|
|
5244
5284
|
});
|
|
5245
|
-
const { userId, agentId, runId,
|
|
5285
|
+
const { userId, agentId, runId, topK = 100, filters = {} } = config;
|
|
5246
5286
|
if (userId) filters.userId = userId;
|
|
5247
5287
|
if (agentId) filters.agentId = agentId;
|
|
5248
5288
|
if (runId) filters.runId = runId;
|
|
@@ -5254,7 +5294,7 @@ ${parsedMessages}`
|
|
|
5254
5294
|
const queryEmbedding = await this.embedder.embed(query);
|
|
5255
5295
|
const memories = await this.vectorStore.search(
|
|
5256
5296
|
queryEmbedding,
|
|
5257
|
-
|
|
5297
|
+
topK,
|
|
5258
5298
|
filters
|
|
5259
5299
|
);
|
|
5260
5300
|
let graphResults;
|
|
@@ -5370,17 +5410,17 @@ ${parsedMessages}`
|
|
|
5370
5410
|
async getAll(config) {
|
|
5371
5411
|
await this._ensureInitialized();
|
|
5372
5412
|
await this._captureEvent("get_all", {
|
|
5373
|
-
|
|
5413
|
+
topK: config.topK,
|
|
5374
5414
|
has_user_id: !!config.userId,
|
|
5375
5415
|
has_agent_id: !!config.agentId,
|
|
5376
5416
|
has_run_id: !!config.runId
|
|
5377
5417
|
});
|
|
5378
|
-
const { userId, agentId, runId,
|
|
5418
|
+
const { userId, agentId, runId, topK = 100 } = config;
|
|
5379
5419
|
const filters = {};
|
|
5380
5420
|
if (userId) filters.userId = userId;
|
|
5381
5421
|
if (agentId) filters.agentId = agentId;
|
|
5382
5422
|
if (runId) filters.runId = runId;
|
|
5383
|
-
const [memories] = await this.vectorStore.list(filters,
|
|
5423
|
+
const [memories] = await this.vectorStore.list(filters, topK);
|
|
5384
5424
|
const excludedKeys = /* @__PURE__ */ new Set([
|
|
5385
5425
|
"userId",
|
|
5386
5426
|
"agentId",
|