mem0ai 2.1.7 → 2.1.9

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.
@@ -68,6 +68,60 @@ var OpenAIEmbedder = class {
68
68
  }
69
69
  };
70
70
 
71
+ // src/oss/src/embeddings/ollama.ts
72
+ import { Ollama } from "ollama";
73
+
74
+ // src/oss/src/utils/logger.ts
75
+ var logger = {
76
+ info: (message) => console.log(`[INFO] ${message}`),
77
+ error: (message) => console.error(`[ERROR] ${message}`),
78
+ debug: (message) => console.debug(`[DEBUG] ${message}`),
79
+ warn: (message) => console.warn(`[WARN] ${message}`)
80
+ };
81
+
82
+ // src/oss/src/embeddings/ollama.ts
83
+ var OllamaEmbedder = class {
84
+ constructor(config) {
85
+ // Using this variable to avoid calling the Ollama server multiple times
86
+ this.initialized = false;
87
+ this.ollama = new Ollama({
88
+ host: config.url || "http://localhost:11434"
89
+ });
90
+ this.model = config.model || "nomic-embed-text:latest";
91
+ this.ensureModelExists().catch((err) => {
92
+ logger.error(`Error ensuring model exists: ${err}`);
93
+ });
94
+ }
95
+ async embed(text) {
96
+ try {
97
+ await this.ensureModelExists();
98
+ } catch (err) {
99
+ logger.error(`Error ensuring model exists: ${err}`);
100
+ }
101
+ const response = await this.ollama.embeddings({
102
+ model: this.model,
103
+ prompt: text
104
+ });
105
+ return response.embedding;
106
+ }
107
+ async embedBatch(texts) {
108
+ const response = await Promise.all(texts.map((text) => this.embed(text)));
109
+ return response;
110
+ }
111
+ async ensureModelExists() {
112
+ if (this.initialized) {
113
+ return true;
114
+ }
115
+ const local_models = await this.ollama.list();
116
+ if (!local_models.models.find((m) => m.name === this.model)) {
117
+ logger.info(`Pulling model ${this.model}...`);
118
+ await this.ollama.pull({ model: this.model });
119
+ }
120
+ this.initialized = true;
121
+ return true;
122
+ }
123
+ };
124
+
71
125
  // src/oss/src/llms/openai.ts
72
126
  import OpenAI2 from "openai";
73
127
  var OpenAILLM = class {
@@ -1025,12 +1079,243 @@ var RedisDB = class {
1025
1079
  }
1026
1080
  };
1027
1081
 
1082
+ // src/oss/src/llms/ollama.ts
1083
+ import { Ollama as Ollama2 } from "ollama";
1084
+ var OllamaLLM = class {
1085
+ constructor(config) {
1086
+ // Using this variable to avoid calling the Ollama server multiple times
1087
+ this.initialized = false;
1088
+ var _a;
1089
+ this.ollama = new Ollama2({
1090
+ host: ((_a = config.config) == null ? void 0 : _a.url) || "http://localhost:11434"
1091
+ });
1092
+ this.model = config.model || "llama3.1:8b";
1093
+ this.ensureModelExists().catch((err) => {
1094
+ logger.error(`Error ensuring model exists: ${err}`);
1095
+ });
1096
+ }
1097
+ async generateResponse(messages, responseFormat, tools) {
1098
+ try {
1099
+ await this.ensureModelExists();
1100
+ } catch (err) {
1101
+ logger.error(`Error ensuring model exists: ${err}`);
1102
+ }
1103
+ const completion = await this.ollama.chat({
1104
+ model: this.model,
1105
+ messages: messages.map((msg) => {
1106
+ const role = msg.role;
1107
+ return {
1108
+ role,
1109
+ content: typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content)
1110
+ };
1111
+ }),
1112
+ ...(responseFormat == null ? void 0 : responseFormat.type) === "json_object" && { format: "json" },
1113
+ ...tools && { tools, tool_choice: "auto" }
1114
+ });
1115
+ const response = completion.message;
1116
+ if (response.tool_calls) {
1117
+ return {
1118
+ content: response.content || "",
1119
+ role: response.role,
1120
+ toolCalls: response.tool_calls.map((call) => ({
1121
+ name: call.function.name,
1122
+ arguments: JSON.stringify(call.function.arguments)
1123
+ }))
1124
+ };
1125
+ }
1126
+ return response.content || "";
1127
+ }
1128
+ async generateChat(messages) {
1129
+ try {
1130
+ await this.ensureModelExists();
1131
+ } catch (err) {
1132
+ logger.error(`Error ensuring model exists: ${err}`);
1133
+ }
1134
+ const completion = await this.ollama.chat({
1135
+ messages: messages.map((msg) => {
1136
+ const role = msg.role;
1137
+ return {
1138
+ role,
1139
+ content: typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content)
1140
+ };
1141
+ }),
1142
+ model: this.model
1143
+ });
1144
+ const response = completion.message;
1145
+ return {
1146
+ content: response.content || "",
1147
+ role: response.role
1148
+ };
1149
+ }
1150
+ async ensureModelExists() {
1151
+ if (this.initialized) {
1152
+ return true;
1153
+ }
1154
+ const local_models = await this.ollama.list();
1155
+ if (!local_models.models.find((m) => m.name === this.model)) {
1156
+ logger.info(`Pulling model ${this.model}...`);
1157
+ await this.ollama.pull({ model: this.model });
1158
+ }
1159
+ this.initialized = true;
1160
+ return true;
1161
+ }
1162
+ };
1163
+
1164
+ // src/oss/src/vector_stores/supabase.ts
1165
+ import { createClient as createClient2 } from "@supabase/supabase-js";
1166
+ var SupabaseDB = class {
1167
+ constructor(config) {
1168
+ this.client = createClient2(config.supabaseUrl, config.supabaseKey);
1169
+ this.tableName = config.tableName;
1170
+ this.embeddingColumnName = config.embeddingColumnName || "embedding";
1171
+ this.metadataColumnName = config.metadataColumnName || "metadata";
1172
+ this.initialize().catch((err) => {
1173
+ console.error("Failed to initialize Supabase:", err);
1174
+ throw err;
1175
+ });
1176
+ }
1177
+ async initialize() {
1178
+ try {
1179
+ const testVector = Array(1536).fill(0);
1180
+ const { error: testError } = await this.client.from(this.tableName).insert({
1181
+ id: "test_vector",
1182
+ [this.embeddingColumnName]: testVector,
1183
+ [this.metadataColumnName]: {}
1184
+ }).select();
1185
+ if (testError) {
1186
+ console.error("Test insert error:", testError);
1187
+ throw new Error(
1188
+ `Vector operations failed. Please ensure:
1189
+ 1. The vector extension is enabled
1190
+ 2. The table "${this.tableName}" exists with correct schema
1191
+ 3. The match_vectors function is created
1192
+ See the SQL migration instructions in the code comments.`
1193
+ );
1194
+ }
1195
+ await this.client.from(this.tableName).delete().eq("id", "test_vector");
1196
+ console.log("Connected to Supabase successfully");
1197
+ } catch (error) {
1198
+ console.error("Error during Supabase initialization:", error);
1199
+ throw error;
1200
+ }
1201
+ }
1202
+ async insert(vectors, ids, payloads) {
1203
+ try {
1204
+ const data = vectors.map((vector, idx) => ({
1205
+ id: ids[idx],
1206
+ [this.embeddingColumnName]: vector,
1207
+ [this.metadataColumnName]: {
1208
+ ...payloads[idx],
1209
+ created_at: (/* @__PURE__ */ new Date()).toISOString()
1210
+ }
1211
+ }));
1212
+ const { error } = await this.client.from(this.tableName).insert(data);
1213
+ if (error) throw error;
1214
+ } catch (error) {
1215
+ console.error("Error during vector insert:", error);
1216
+ throw error;
1217
+ }
1218
+ }
1219
+ async search(query, limit = 5, filters) {
1220
+ try {
1221
+ const rpcQuery = {
1222
+ query_embedding: query,
1223
+ match_count: limit
1224
+ };
1225
+ if (filters) {
1226
+ rpcQuery.filter = filters;
1227
+ }
1228
+ const { data, error } = await this.client.rpc("match_vectors", rpcQuery);
1229
+ if (error) throw error;
1230
+ if (!data) return [];
1231
+ const results = data;
1232
+ return results.map((result) => ({
1233
+ id: result.id,
1234
+ payload: result.metadata,
1235
+ score: result.similarity
1236
+ }));
1237
+ } catch (error) {
1238
+ console.error("Error during vector search:", error);
1239
+ throw error;
1240
+ }
1241
+ }
1242
+ async get(vectorId) {
1243
+ try {
1244
+ const { data, error } = await this.client.from(this.tableName).select("*").eq("id", vectorId).single();
1245
+ if (error) throw error;
1246
+ if (!data) return null;
1247
+ return {
1248
+ id: data.id,
1249
+ payload: data[this.metadataColumnName]
1250
+ };
1251
+ } catch (error) {
1252
+ console.error("Error getting vector:", error);
1253
+ throw error;
1254
+ }
1255
+ }
1256
+ async update(vectorId, vector, payload) {
1257
+ try {
1258
+ const { error } = await this.client.from(this.tableName).update({
1259
+ [this.embeddingColumnName]: vector,
1260
+ [this.metadataColumnName]: {
1261
+ ...payload,
1262
+ updated_at: (/* @__PURE__ */ new Date()).toISOString()
1263
+ }
1264
+ }).eq("id", vectorId);
1265
+ if (error) throw error;
1266
+ } catch (error) {
1267
+ console.error("Error during vector update:", error);
1268
+ throw error;
1269
+ }
1270
+ }
1271
+ async delete(vectorId) {
1272
+ try {
1273
+ const { error } = await this.client.from(this.tableName).delete().eq("id", vectorId);
1274
+ if (error) throw error;
1275
+ } catch (error) {
1276
+ console.error("Error deleting vector:", error);
1277
+ throw error;
1278
+ }
1279
+ }
1280
+ async deleteCol() {
1281
+ try {
1282
+ const { error } = await this.client.from(this.tableName).delete().neq("id", "");
1283
+ if (error) throw error;
1284
+ } catch (error) {
1285
+ console.error("Error deleting collection:", error);
1286
+ throw error;
1287
+ }
1288
+ }
1289
+ async list(filters, limit = 100) {
1290
+ try {
1291
+ let query = this.client.from(this.tableName).select("*", { count: "exact" }).limit(limit);
1292
+ if (filters) {
1293
+ Object.entries(filters).forEach(([key, value]) => {
1294
+ query = query.eq(`${this.metadataColumnName}->>${key}`, value);
1295
+ });
1296
+ }
1297
+ const { data, error, count } = await query;
1298
+ if (error) throw error;
1299
+ const results = data.map((item) => ({
1300
+ id: item.id,
1301
+ payload: item[this.metadataColumnName]
1302
+ }));
1303
+ return [results, count || 0];
1304
+ } catch (error) {
1305
+ console.error("Error listing vectors:", error);
1306
+ throw error;
1307
+ }
1308
+ }
1309
+ };
1310
+
1028
1311
  // src/oss/src/utils/factory.ts
1029
1312
  var EmbedderFactory = class {
1030
1313
  static create(provider, config) {
1031
1314
  switch (provider.toLowerCase()) {
1032
1315
  case "openai":
1033
1316
  return new OpenAIEmbedder(config);
1317
+ case "ollama":
1318
+ return new OllamaEmbedder(config);
1034
1319
  default:
1035
1320
  throw new Error(`Unsupported embedder provider: ${provider}`);
1036
1321
  }
@@ -1047,6 +1332,8 @@ var LLMFactory = class {
1047
1332
  return new AnthropicLLM(config);
1048
1333
  case "groq":
1049
1334
  return new GroqLLM(config);
1335
+ case "ollama":
1336
+ return new OllamaLLM(config);
1050
1337
  default:
1051
1338
  throw new Error(`Unsupported LLM provider: ${provider}`);
1052
1339
  }
@@ -1063,6 +1350,9 @@ var VectorStoreFactory = class {
1063
1350
  case "redis":
1064
1351
  return new RedisDB(config);
1065
1352
  // Type assertion needed as config is extended
1353
+ case "supabase":
1354
+ return new SupabaseDB(config);
1355
+ // Type assertion needed as config is extended
1066
1356
  default:
1067
1357
  throw new Error(`Unsupported vector store provider: ${provider}`);
1068
1358
  }
@@ -1659,14 +1949,6 @@ function getDeleteMessages(existingMemoriesString, data, userId) {
1659
1949
  ];
1660
1950
  }
1661
1951
 
1662
- // src/oss/src/utils/logger.ts
1663
- var logger = {
1664
- info: (message) => console.log(`[INFO] ${message}`),
1665
- error: (message) => console.error(`[ERROR] ${message}`),
1666
- debug: (message) => console.debug(`[DEBUG] ${message}`),
1667
- warn: (message) => console.warn(`[WARN] ${message}`)
1668
- };
1669
-
1670
1952
  // src/oss/src/memory/graph_memory.ts
1671
1953
  var MemoryGraph = class {
1672
1954
  constructor(config) {
@@ -2612,6 +2894,8 @@ export {
2612
2894
  Memory,
2613
2895
  MemoryConfigSchema,
2614
2896
  MemoryVectorStore,
2897
+ OllamaEmbedder,
2898
+ OllamaLLM,
2615
2899
  OpenAIEmbedder,
2616
2900
  OpenAILLM,
2617
2901
  OpenAIStructuredLLM,