mem0ai 1.0.2-beta → 1.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/README.md +9 -208
- package/examples/basic-usage.js +39 -0
- package/package.json +7 -18
- package/src/index.js +120 -0
- package/examples/basic-usage.ts +0 -40
- package/src/index.ts +0 -143
package/README.md
CHANGED
|
@@ -17,8 +17,6 @@ npm install mem0ai
|
|
|
17
17
|
|
|
18
18
|
## 3. Instantiate Client
|
|
19
19
|
|
|
20
|
-
### JavaScript
|
|
21
|
-
|
|
22
20
|
```javascript
|
|
23
21
|
const MemoryClient = require('mem0ai');
|
|
24
22
|
const client = new MemoryClient('your-api-key');
|
|
@@ -31,20 +29,6 @@ const MemoryClient = require('mem0ai');
|
|
|
31
29
|
const client = new MemoryClient();
|
|
32
30
|
```
|
|
33
31
|
|
|
34
|
-
### TypeScript
|
|
35
|
-
|
|
36
|
-
```typescript
|
|
37
|
-
import MemoryClient from 'mem0ai';
|
|
38
|
-
const client = new MemoryClient('your-api-key');
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
Alternatively, you can set the `MEM0_API_KEY` environment variable and instantiate the client without passing the API key:
|
|
42
|
-
|
|
43
|
-
```typescript
|
|
44
|
-
import MemoryClient from 'mem0ai';
|
|
45
|
-
const client = new MemoryClient();
|
|
46
|
-
```
|
|
47
|
-
|
|
48
32
|
## 4. Memory Operations
|
|
49
33
|
|
|
50
34
|
Mem0 provides a simple and customizable interface for performing CRUD operations on memory.
|
|
@@ -55,8 +39,6 @@ You can create long-term and short-term memories for your users, AI Agents, etc.
|
|
|
55
39
|
|
|
56
40
|
#### Long-term memory for a user
|
|
57
41
|
|
|
58
|
-
### JavaScript
|
|
59
|
-
|
|
60
42
|
```javascript
|
|
61
43
|
const messages = [
|
|
62
44
|
{ role: "user", content: "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts." },
|
|
@@ -68,23 +50,8 @@ client.add(messages, { user_id: "alex" })
|
|
|
68
50
|
.catch(error => console.error(error));
|
|
69
51
|
```
|
|
70
52
|
|
|
71
|
-
### TypeScript
|
|
72
|
-
|
|
73
|
-
```typescript
|
|
74
|
-
const messages: { role: string, content: string }[] = [
|
|
75
|
-
{ role: "user", content: "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts." },
|
|
76
|
-
{ role: "assistant", content: "Hello Alex! I've noted that you're a vegetarian and have a nut allergy. I'll keep this in mind for any food-related recommendations or discussions." }
|
|
77
|
-
];
|
|
78
|
-
|
|
79
|
-
client.add(messages, { user_id: "alex" })
|
|
80
|
-
.then(result => console.log(result))
|
|
81
|
-
.catch(error => console.error(error));
|
|
82
|
-
```
|
|
83
|
-
|
|
84
53
|
#### Short-term memory for a user session
|
|
85
54
|
|
|
86
|
-
### JavaScript
|
|
87
|
-
|
|
88
55
|
```javascript
|
|
89
56
|
const messages = [
|
|
90
57
|
{ role: "user", content: "I'm planning a trip to Japan next month." },
|
|
@@ -98,25 +65,8 @@ client.add(messages, { user_id: "alex123", session_id: "trip-planning-2024" })
|
|
|
98
65
|
.catch(error => console.error(error));
|
|
99
66
|
```
|
|
100
67
|
|
|
101
|
-
### TypeScript
|
|
102
|
-
|
|
103
|
-
```typescript
|
|
104
|
-
const messages: { role: string, content: string }[] = [
|
|
105
|
-
{ role: "user", content: "I'm planning a trip to Japan next month." },
|
|
106
|
-
{ role: "assistant", content: "That's exciting, Alex! A trip to Japan next month sounds wonderful. Would you like some recommendations for vegetarian-friendly restaurants in Japan?" },
|
|
107
|
-
{ role: "user", content: "Yes, please! Especially in Tokyo." },
|
|
108
|
-
{ role: "assistant", content: "Great! I'll remember that you're interested in vegetarian restaurants in Tokyo for your upcoming trip. I'll prepare a list for you in our next interaction." }
|
|
109
|
-
];
|
|
110
|
-
|
|
111
|
-
client.add(messages, { user_id: "alex123", session_id: "trip-planning-2024" })
|
|
112
|
-
.then(result => console.log(result))
|
|
113
|
-
.catch(error => console.error(error));
|
|
114
|
-
```
|
|
115
|
-
|
|
116
68
|
#### Long-term memory for agents
|
|
117
69
|
|
|
118
|
-
### JavaScript
|
|
119
|
-
|
|
120
70
|
```javascript
|
|
121
71
|
const messages = [
|
|
122
72
|
{ role: "system", content: "You are a personalized travel assistant. Remember user preferences and provide tailored recommendations." },
|
|
@@ -128,25 +78,10 @@ client.add(messages, { agent_id: "travel-assistant" })
|
|
|
128
78
|
.catch(error => console.error(error));
|
|
129
79
|
```
|
|
130
80
|
|
|
131
|
-
### TypeScript
|
|
132
|
-
|
|
133
|
-
```typescript
|
|
134
|
-
const messages: { role: string, content: string }[] = [
|
|
135
|
-
{ role: "system", content: "You are a personalized travel assistant. Remember user preferences and provide tailored recommendations." },
|
|
136
|
-
{ role: "assistant", content: "Understood. I'll maintain personalized travel preferences for each user and provide customized recommendations based on their dietary restrictions, interests, and past interactions." }
|
|
137
|
-
];
|
|
138
|
-
|
|
139
|
-
client.add(messages, { agent_id: "travel-assistant" })
|
|
140
|
-
.then(result => console.log(result))
|
|
141
|
-
.catch(error => console.error(error));
|
|
142
|
-
```
|
|
143
|
-
|
|
144
81
|
### 4.2 Search Relevant Memories
|
|
145
82
|
|
|
146
83
|
You can get related memories for a given natural language question using our search method.
|
|
147
84
|
|
|
148
|
-
### JavaScript
|
|
149
|
-
|
|
150
85
|
```javascript
|
|
151
86
|
const query = "What do you know about me?";
|
|
152
87
|
client.search(query, { user_id: "alex" })
|
|
@@ -154,115 +89,64 @@ client.search(query, { user_id: "alex" })
|
|
|
154
89
|
.catch(error => console.error(error));
|
|
155
90
|
```
|
|
156
91
|
|
|
157
|
-
### TypeScript
|
|
158
|
-
|
|
159
|
-
```typescript
|
|
160
|
-
const query: string = "What do you know about me?";
|
|
161
|
-
client.search(query, { user_id: "alex" })
|
|
162
|
-
.then(results => console.log(results))
|
|
163
|
-
.catch(error => console.error(error));
|
|
164
|
-
```
|
|
165
|
-
|
|
166
92
|
Similarly, you can search for agent memories by passing the `agent_id` option:
|
|
167
93
|
|
|
168
|
-
### JavaScript
|
|
169
|
-
|
|
170
94
|
```javascript
|
|
171
95
|
client.search("What are the learnings from previous runs?", { agent_id: "travel-assistant" })
|
|
172
96
|
.then(results => console.log(results))
|
|
173
97
|
.catch(error => console.error(error));
|
|
174
98
|
```
|
|
175
99
|
|
|
176
|
-
### TypeScript
|
|
177
|
-
|
|
178
|
-
```typescript
|
|
179
|
-
client.search("What are the learnings from previous runs?", { agent_id: "travel-assistant" })
|
|
180
|
-
.then(results => console.log(results))
|
|
181
|
-
.catch(error => console.error(error));
|
|
182
|
-
```
|
|
183
|
-
|
|
184
100
|
### 4.3 Get All Memories
|
|
185
101
|
|
|
186
102
|
Fetch all memories for a user, agent, or session using the getAll() method.
|
|
187
103
|
|
|
188
104
|
#### Get all memories of an AI Agent
|
|
189
105
|
|
|
190
|
-
### JavaScript
|
|
191
|
-
|
|
192
106
|
```javascript
|
|
193
107
|
client.getAll({ agent_id: "travel-assistant" })
|
|
194
108
|
.then(memories => console.log(memories))
|
|
195
109
|
.catch(error => console.error(error));
|
|
196
110
|
```
|
|
197
111
|
|
|
198
|
-
### TypeScript
|
|
199
|
-
|
|
200
|
-
```typescript
|
|
201
|
-
client.getAll({ agent_id: "travel-assistant" })
|
|
202
|
-
.then(memories => console.log(memories))
|
|
203
|
-
.catch(error => console.error(error));
|
|
204
|
-
```
|
|
205
|
-
|
|
206
112
|
#### Get all memories of a user
|
|
207
113
|
|
|
208
|
-
### JavaScript
|
|
209
|
-
|
|
210
114
|
```javascript
|
|
211
115
|
client.getAll({ user_id: "alex" })
|
|
212
116
|
.then(memories => console.log(memories))
|
|
213
117
|
.catch(error => console.error(error));
|
|
214
118
|
```
|
|
215
119
|
|
|
216
|
-
### TypeScript
|
|
217
|
-
|
|
218
|
-
```typescript
|
|
219
|
-
client.getAll({ user_id: "alex" })
|
|
220
|
-
.then(memories => console.log(memories))
|
|
221
|
-
.catch(error => console.error(error));
|
|
222
|
-
```
|
|
223
|
-
|
|
224
120
|
#### Get short-term memories for a session
|
|
225
121
|
|
|
226
|
-
### JavaScript
|
|
227
|
-
|
|
228
122
|
```javascript
|
|
229
123
|
client.getAll({ user_id: "alex123", session_id: "trip-planning-2024" })
|
|
230
124
|
.then(memories => console.log(memories))
|
|
231
125
|
.catch(error => console.error(error));
|
|
232
126
|
```
|
|
233
127
|
|
|
234
|
-
### TypeScript
|
|
235
|
-
|
|
236
|
-
```typescript
|
|
237
|
-
client.getAll({ user_id: "alex123", session_id: "trip-planning-2024" })
|
|
238
|
-
.then(memories => console.log(memories))
|
|
239
|
-
.catch(error => console.error(error));
|
|
240
|
-
```
|
|
241
|
-
|
|
242
128
|
#### Get specific memory
|
|
243
129
|
|
|
244
|
-
### JavaScript
|
|
245
|
-
|
|
246
130
|
```javascript
|
|
247
131
|
client.get("memory-id-here")
|
|
248
132
|
.then(memory => console.log(memory))
|
|
249
133
|
.catch(error => console.error(error));
|
|
250
134
|
```
|
|
251
135
|
|
|
252
|
-
###
|
|
136
|
+
### 4.4 Get all users
|
|
253
137
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
138
|
+
Get all users for which you have memories.
|
|
139
|
+
|
|
140
|
+
```javascript
|
|
141
|
+
client.users()
|
|
142
|
+
.then(users => console.log(users))
|
|
257
143
|
.catch(error => console.error(error));
|
|
258
144
|
```
|
|
259
145
|
|
|
260
|
-
### 4.
|
|
146
|
+
### 4.5 Memory History
|
|
261
147
|
|
|
262
148
|
Get history of how a memory has changed over time:
|
|
263
149
|
|
|
264
|
-
### JavaScript
|
|
265
|
-
|
|
266
150
|
```javascript
|
|
267
151
|
// Add some message to create history
|
|
268
152
|
let messages = [{ role: "user", content: "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.." }];
|
|
@@ -281,88 +165,36 @@ client.add(messages, { user_id: "alex" })
|
|
|
281
165
|
.catch(error => console.error(error));
|
|
282
166
|
```
|
|
283
167
|
|
|
284
|
-
###
|
|
285
|
-
|
|
286
|
-
```typescript
|
|
287
|
-
// Add some message to create history
|
|
288
|
-
let messages: { role: string, content: string }[] = [{ role: "user", content: "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.." }];
|
|
289
|
-
client.add(messages, { user_id: "alex" })
|
|
290
|
-
.then(result => {
|
|
291
|
-
// Add second message to update history
|
|
292
|
-
messages.push({ role: 'user', content: 'I turned vegetarian now.' });
|
|
293
|
-
return client.add(messages, { user_id: "alex" });
|
|
294
|
-
})
|
|
295
|
-
.then(result => {
|
|
296
|
-
// Get history of how memory changed over time
|
|
297
|
-
const memoryId = result.id; // Assuming the API returns the memory ID
|
|
298
|
-
return client.history(memoryId);
|
|
299
|
-
})
|
|
300
|
-
.then(history => console.log(history))
|
|
301
|
-
.catch(error => console.error(error));
|
|
302
|
-
```
|
|
303
|
-
|
|
304
|
-
### 4.5 Delete Memory
|
|
168
|
+
### 4.6 Delete Memory
|
|
305
169
|
|
|
306
170
|
Delete specific memory:
|
|
307
171
|
|
|
308
|
-
### JavaScript
|
|
309
|
-
|
|
310
172
|
```javascript
|
|
311
173
|
client.delete("memory-id-here")
|
|
312
174
|
.then(result => console.log(result))
|
|
313
175
|
.catch(error => console.error(error));
|
|
314
176
|
```
|
|
315
177
|
|
|
316
|
-
### TypeScript
|
|
317
|
-
|
|
318
|
-
```typescript
|
|
319
|
-
client.delete("memory-id-here")
|
|
320
|
-
.then(result => console.log(result))
|
|
321
|
-
.catch(error => console.error(error));
|
|
322
|
-
```
|
|
323
|
-
|
|
324
178
|
Delete all memories of a user:
|
|
325
179
|
|
|
326
|
-
### JavaScript
|
|
327
|
-
|
|
328
180
|
```javascript
|
|
329
181
|
client.deleteAll({ user_id: "alex" })
|
|
330
182
|
.then(result => console.log(result))
|
|
331
183
|
.catch(error => console.error(error));
|
|
332
184
|
```
|
|
333
185
|
|
|
334
|
-
### TypeScript
|
|
335
|
-
|
|
336
|
-
```typescript
|
|
337
|
-
client.deleteAll({ user_id: "alex" })
|
|
338
|
-
.then(result => console.log(result))
|
|
339
|
-
.catch(error => console.error(error));
|
|
340
|
-
```
|
|
341
|
-
|
|
342
186
|
Fun fact: You can also delete the memory using the `add()` method by passing a natural language command:
|
|
343
187
|
|
|
344
|
-
### JavaScript
|
|
345
|
-
|
|
346
188
|
```javascript
|
|
347
189
|
client.add("Delete all of my food preferences", { user_id: "alex" })
|
|
348
190
|
.then(result => console.log(result))
|
|
349
191
|
.catch(error => console.error(error));
|
|
350
192
|
```
|
|
351
193
|
|
|
352
|
-
### TypeScript
|
|
353
|
-
|
|
354
|
-
```typescript
|
|
355
|
-
client.add("Delete all of my food preferences", { user_id: "alex" })
|
|
356
|
-
.then(result => console.log(result))
|
|
357
|
-
.catch(error => console.error(error));
|
|
358
|
-
```
|
|
359
|
-
|
|
360
194
|
## 5. Error Handling
|
|
361
195
|
|
|
362
196
|
The MemoryClient throws `APIError` for any API-related errors. You can catch and handle these errors as follows:
|
|
363
197
|
|
|
364
|
-
### JavaScript
|
|
365
|
-
|
|
366
198
|
```javascript
|
|
367
199
|
client.add(messages, { user_id: "alex" })
|
|
368
200
|
.then(result => console.log(result))
|
|
@@ -375,26 +207,10 @@ client.add(messages, { user_id: "alex" })
|
|
|
375
207
|
});
|
|
376
208
|
```
|
|
377
209
|
|
|
378
|
-
### TypeScript
|
|
379
|
-
|
|
380
|
-
```typescript
|
|
381
|
-
client.add(messages, { user_id: "alex" })
|
|
382
|
-
.then(result => console.log(result))
|
|
383
|
-
.catch(error => {
|
|
384
|
-
if (error.name === 'APIError') {
|
|
385
|
-
console.error('API Error:', error.message);
|
|
386
|
-
} else {
|
|
387
|
-
console.error('Unexpected error:', error);
|
|
388
|
-
}
|
|
389
|
-
});
|
|
390
|
-
```
|
|
391
|
-
|
|
392
210
|
## 6. Using with async/await
|
|
393
211
|
|
|
394
212
|
All methods of the MemoryClient return promises, so you can use them with async/await:
|
|
395
213
|
|
|
396
|
-
### JavaScript
|
|
397
|
-
|
|
398
214
|
```javascript
|
|
399
215
|
async function addMemory() {
|
|
400
216
|
try {
|
|
@@ -408,21 +224,6 @@ async function addMemory() {
|
|
|
408
224
|
addMemory();
|
|
409
225
|
```
|
|
410
226
|
|
|
411
|
-
### TypeScript
|
|
412
|
-
|
|
413
|
-
```typescript
|
|
414
|
-
async function addMemory() {
|
|
415
|
-
try {
|
|
416
|
-
const result = await client.add(messages, { user_id: "alex" });
|
|
417
|
-
console.log(result);
|
|
418
|
-
} catch (error) {
|
|
419
|
-
console.error('Error adding memory:', error);
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
addMemory();
|
|
424
|
-
```
|
|
425
|
-
|
|
426
227
|
## Getting Help
|
|
427
228
|
|
|
428
229
|
If you have any questions or need assistance, please reach out to us:
|
|
@@ -430,4 +231,4 @@ If you have any questions or need assistance, please reach out to us:
|
|
|
430
231
|
- Email: founders@mem0.ai
|
|
431
232
|
- [Join our discord community](https://mem0.ai/discord)
|
|
432
233
|
- [Join our slack community](https://mem0.ai/slack)
|
|
433
|
-
- GitHub Issues: [Report bugs or request features](https://github.com/mem0ai/mem0ai-node/issues)
|
|
234
|
+
- GitHub Issues: [Report bugs or request features](https://github.com/mem0ai/mem0ai-node/issues)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const MemoryClient = require('../src/index');
|
|
2
|
+
|
|
3
|
+
async function main() {
|
|
4
|
+
const apiKey = 'm0-UlqGB4SLcLgVcZ7YmYNlzHum3BxEqS0TzaYd09U0';
|
|
5
|
+
const client = new MemoryClient(apiKey);
|
|
6
|
+
|
|
7
|
+
try {
|
|
8
|
+
// Add a new memory
|
|
9
|
+
const messages = [
|
|
10
|
+
{ "role": "user", "content": "Hey, I am Alex. I'm a vegetarian."},
|
|
11
|
+
{"role": "assistant", "content": "Hello Alex! I am vegetarian too!"},
|
|
12
|
+
];
|
|
13
|
+
const addResult = await client.add(messages, { user_id: "alex" });
|
|
14
|
+
console.log('Memory added:', addResult);
|
|
15
|
+
|
|
16
|
+
// Get all memories
|
|
17
|
+
const allMemories = await client.getAll({ user_id: "alex" });
|
|
18
|
+
console.log('All memories:', allMemories);
|
|
19
|
+
|
|
20
|
+
// Search memories
|
|
21
|
+
const searchResult = await client.search('What do you know about me?', { user_id: "alex"});
|
|
22
|
+
console.log('Search results:', searchResult);
|
|
23
|
+
|
|
24
|
+
// Get all users
|
|
25
|
+
const allUsers = await client.users();
|
|
26
|
+
console.log('All users:', allUsers);
|
|
27
|
+
|
|
28
|
+
// // Delete a memory (assuming we have a memory ID from the add result)
|
|
29
|
+
if (addResult && addResult.id) {
|
|
30
|
+
const deleteResult = await client.delete(addResult.id);
|
|
31
|
+
console.log('Memory deleted:', deleteResult);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.error('Error:', error.message);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
main();
|
package/package.json
CHANGED
|
@@ -1,26 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mem0ai",
|
|
3
|
-
"version": "1.0.2
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "The memory layer for personalized AI",
|
|
5
|
-
"main": "src/index.
|
|
5
|
+
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
8
|
},
|
|
9
|
-
"keywords": [
|
|
10
|
-
|
|
11
|
-
"api",
|
|
12
|
-
"client",
|
|
13
|
-
"memory",
|
|
14
|
-
"llm",
|
|
15
|
-
"long-term-memory",
|
|
16
|
-
"ai"
|
|
17
|
-
],
|
|
18
|
-
"author": "Team Mem0",
|
|
9
|
+
"keywords": ["mem0", "api", "client", "memory", "llm", "long-term-memory", "ai"],
|
|
10
|
+
"author": "Deshraj Yadav",
|
|
19
11
|
"license": "Apache-2.0",
|
|
20
12
|
"dependencies": {
|
|
21
|
-
|
|
22
|
-
},
|
|
23
|
-
"devDependencies": {
|
|
24
|
-
"@types/node": "^22.1.0"
|
|
13
|
+
"axios": "^0.21.1"
|
|
25
14
|
}
|
|
26
|
-
}
|
|
15
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
const axios = require('axios');
|
|
2
|
+
|
|
3
|
+
class APIError extends Error {
|
|
4
|
+
constructor(message) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = 'APIError';
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function apiErrorHandler(fn) {
|
|
11
|
+
return async function (...args) {
|
|
12
|
+
try {
|
|
13
|
+
return await fn.apply(this, args);
|
|
14
|
+
} catch (error) {
|
|
15
|
+
if (error.response) {
|
|
16
|
+
throw new APIError(`API request failed: ${error.response.data}`);
|
|
17
|
+
} else if (error.request) {
|
|
18
|
+
throw new APIError(`Request failed: ${error.message}`);
|
|
19
|
+
} else {
|
|
20
|
+
throw error;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
class MemoryClient {
|
|
27
|
+
constructor(apiKey, host = 'https://api.mem0.ai/v1') {
|
|
28
|
+
this.apiKey = apiKey || process.env.MEM0_API_KEY;
|
|
29
|
+
this.host = host;
|
|
30
|
+
|
|
31
|
+
if (!this.apiKey) {
|
|
32
|
+
throw new Error('API Key not provided. Please provide an API Key.');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
this.client = axios.create({
|
|
36
|
+
baseURL: this.host,
|
|
37
|
+
headers: { Authorization: `Token ${this.apiKey}` },
|
|
38
|
+
timeout: 60000,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
this._validateApiKey();
|
|
42
|
+
|
|
43
|
+
// Apply error handler to methods
|
|
44
|
+
this.add = apiErrorHandler(this.add.bind(this));
|
|
45
|
+
this.get = apiErrorHandler(this.get.bind(this));
|
|
46
|
+
this.getAll = apiErrorHandler(this.getAll.bind(this));
|
|
47
|
+
this.search = apiErrorHandler(this.search.bind(this));
|
|
48
|
+
this.delete = apiErrorHandler(this.delete.bind(this));
|
|
49
|
+
this.deleteAll = apiErrorHandler(this.deleteAll.bind(this));
|
|
50
|
+
this.history = apiErrorHandler(this.history.bind(this));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async _validateApiKey() {
|
|
54
|
+
try {
|
|
55
|
+
await this.client.get('/memories/', { params: { user_id: 'test' } });
|
|
56
|
+
} catch (error) {
|
|
57
|
+
throw new Error('Invalid API Key. Please get a valid API Key from https://app.mem0.ai');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async add(messages, options = {}) {
|
|
62
|
+
const payload = this._preparePayload(messages, options);
|
|
63
|
+
const response = await this.client.post('/memories/', payload);
|
|
64
|
+
return response.data;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async get(memoryId) {
|
|
68
|
+
const response = await this.client.get(`/memories/${memoryId}/`);
|
|
69
|
+
return response.data;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async getAll(options = {}) {
|
|
73
|
+
const params = this._prepareParams(options);
|
|
74
|
+
const response = await this.client.get('/memories/', { params });
|
|
75
|
+
return response.data;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async search(query, options = {}) {
|
|
79
|
+
const payload = { query, ...options };
|
|
80
|
+
const response = await this.client.post('/memories/search/', payload);
|
|
81
|
+
return response.data;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async delete(memoryId) {
|
|
85
|
+
const response = await this.client.delete(`/memories/${memoryId}/`);
|
|
86
|
+
return response.data;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async deleteAll(options = {}) {
|
|
90
|
+
const params = this._prepareParams(options);
|
|
91
|
+
const response = await this.client.delete('/memories/', { params });
|
|
92
|
+
return response.data;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async history(memoryId) {
|
|
96
|
+
const response = await this.client.get(`/memories/${memoryId}/history/`);
|
|
97
|
+
return response.data;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async users() {
|
|
101
|
+
const response = await this.client.get('/entities/');
|
|
102
|
+
return response.data;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
_preparePayload(messages, options) {
|
|
106
|
+
const payload = {};
|
|
107
|
+
if (typeof messages === 'string') {
|
|
108
|
+
payload.messages = [{ role: 'user', content: messages }];
|
|
109
|
+
} else if (Array.isArray(messages)) {
|
|
110
|
+
payload.messages = messages;
|
|
111
|
+
}
|
|
112
|
+
return { ...payload, ...options };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
_prepareParams(options) {
|
|
116
|
+
return Object.fromEntries(Object.entries(options).filter(([_, v]) => v != null));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
module.exports = MemoryClient;
|
package/examples/basic-usage.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import MemoryClient from 'mem0ai';
|
|
2
|
-
|
|
3
|
-
async function main() {
|
|
4
|
-
const client = new MemoryClient();
|
|
5
|
-
|
|
6
|
-
try {
|
|
7
|
-
// Add a new memory
|
|
8
|
-
const addResult = await client.add('This is a test memory');
|
|
9
|
-
console.log('Memory added:', addResult);
|
|
10
|
-
|
|
11
|
-
// Get all memories
|
|
12
|
-
const allMemories = await client.getAll();
|
|
13
|
-
console.log('All memories:', allMemories);
|
|
14
|
-
|
|
15
|
-
// Search memories
|
|
16
|
-
const searchResult = await client.search('test');
|
|
17
|
-
console.log('Search results:', searchResult);
|
|
18
|
-
|
|
19
|
-
// Delete a memory (assuming we have a memory ID from the add result)
|
|
20
|
-
if (addResult && addResult.id) {
|
|
21
|
-
const deleteResult = await client.delete(addResult.id);
|
|
22
|
-
console.log('Memory deleted:', deleteResult);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// Simple example from README
|
|
26
|
-
const simpleAddResult = await client.add('Simple example memory');
|
|
27
|
-
console.log('Simple memory added:', simpleAddResult);
|
|
28
|
-
|
|
29
|
-
const simpleGetResult = await client.get(simpleAddResult.id);
|
|
30
|
-
console.log('Simple memory retrieved:', simpleGetResult);
|
|
31
|
-
|
|
32
|
-
const simpleDeleteResult = await client.delete(simpleAddResult.id);
|
|
33
|
-
console.log('Simple memory deleted:', simpleDeleteResult);
|
|
34
|
-
|
|
35
|
-
} catch (error: any) {
|
|
36
|
-
console.error('Error:', error.message);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
main();
|
package/src/index.ts
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import axios, { AxiosInstance, AxiosResponse } from 'axios';
|
|
2
|
-
|
|
3
|
-
class APIError extends Error {
|
|
4
|
-
constructor(message: string) {
|
|
5
|
-
super(message);
|
|
6
|
-
this.name = 'APIError';
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
function apiErrorHandler<T extends (...args: any[]) => Promise<any>>(fn: T): T {
|
|
11
|
-
return (async function (...args: any[]) {
|
|
12
|
-
try {
|
|
13
|
-
return await fn.apply(this, args);
|
|
14
|
-
} catch (error: any) {
|
|
15
|
-
if (error.response) {
|
|
16
|
-
throw new APIError(`API request failed: ${error.response.data}`);
|
|
17
|
-
} else if (error.request) {
|
|
18
|
-
throw new APIError(`Request failed: ${error.message}`);
|
|
19
|
-
} else {
|
|
20
|
-
throw error;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}) as T;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
interface Message {
|
|
27
|
-
role: string;
|
|
28
|
-
content: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
interface AddOptions {
|
|
32
|
-
[key: string]: any;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
interface SearchOptions {
|
|
36
|
-
[key: string]: any;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
interface GetAllOptions {
|
|
40
|
-
[key: string]: any;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
interface DeleteAllOptions {
|
|
44
|
-
[key: string]: any;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export class MemoryClient {
|
|
48
|
-
private apiKey: string;
|
|
49
|
-
private host: string;
|
|
50
|
-
private client: AxiosInstance;
|
|
51
|
-
|
|
52
|
-
constructor(apiKey?: string, host: string = 'https://api.mem0.ai/v1') {
|
|
53
|
-
this.apiKey = apiKey || process.env.MEM0_API_KEY!;
|
|
54
|
-
this.host = host;
|
|
55
|
-
|
|
56
|
-
if (!this.apiKey) {
|
|
57
|
-
throw new Error('API Key not provided. Please provide an API Key.');
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
this.client = axios.create({
|
|
61
|
-
baseURL: this.host,
|
|
62
|
-
headers: { Authorization: `Token ${this.apiKey}` },
|
|
63
|
-
timeout: 60000,
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
this._validateApiKey();
|
|
67
|
-
|
|
68
|
-
// Apply error handler to methods
|
|
69
|
-
this.add = apiErrorHandler(this.add.bind(this));
|
|
70
|
-
this.get = apiErrorHandler(this.get.bind(this));
|
|
71
|
-
this.getAll = apiErrorHandler(this.getAll.bind(this));
|
|
72
|
-
this.search = apiErrorHandler(this.search.bind(this));
|
|
73
|
-
this.delete = apiErrorHandler(this.delete.bind(this));
|
|
74
|
-
this.deleteAll = apiErrorHandler(this.deleteAll.bind(this));
|
|
75
|
-
this.history = apiErrorHandler(this.history.bind(this));
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
private async _validateApiKey(): Promise<void> {
|
|
79
|
-
try {
|
|
80
|
-
await this.client.get('/memories/', { params: { user_id: 'test' } });
|
|
81
|
-
} catch (error) {
|
|
82
|
-
throw new Error('Invalid API Key. Please get a valid API Key from https://app.mem0.ai');
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
async add(messages: string | Message[], options: AddOptions = {}): Promise<any> {
|
|
87
|
-
const payload = this._preparePayload(messages, options);
|
|
88
|
-
const response: AxiosResponse = await this.client.post('/memories/', payload);
|
|
89
|
-
return response.data;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
async get(memoryId: string): Promise<any> {
|
|
93
|
-
const response: AxiosResponse = await this.client.get(`/memories/${memoryId}/`);
|
|
94
|
-
return response.data;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
async getAll(options: GetAllOptions = {}): Promise<any> {
|
|
98
|
-
const params = this._prepareParams(options);
|
|
99
|
-
const response: AxiosResponse = await this.client.get('/memories/', { params });
|
|
100
|
-
return response.data;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
async search(query: string, options: SearchOptions = {}): Promise<any> {
|
|
104
|
-
const payload = { query, ...options };
|
|
105
|
-
const response: AxiosResponse = await this.client.post('/memories/search/', payload);
|
|
106
|
-
return response.data;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
async delete(memoryId: string): Promise<any> {
|
|
110
|
-
const response: AxiosResponse = await this.client.delete(`/memories/${memoryId}/`);
|
|
111
|
-
return response.data;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
async deleteAll(options: DeleteAllOptions = {}): Promise<any> {
|
|
115
|
-
const params = this._prepareParams(options);
|
|
116
|
-
const response: AxiosResponse = await this.client.delete('/memories/', { params });
|
|
117
|
-
return response.data;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
async history(memoryId: string): Promise<any> {
|
|
121
|
-
const response: AxiosResponse = await this.client.get(`/memories/${memoryId}/history/`);
|
|
122
|
-
return response.data;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
async users(): Promise<any> {
|
|
126
|
-
const response: AxiosResponse = await this.client.get('/entities/');
|
|
127
|
-
return response.data;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
private _preparePayload(messages: string | Message[], options: AddOptions): any {
|
|
131
|
-
const payload: { messages?: Message[] } = {};
|
|
132
|
-
if (typeof messages === 'string') {
|
|
133
|
-
payload.messages = [{ role: 'user', content: messages }];
|
|
134
|
-
} else if (Array.isArray(messages)) {
|
|
135
|
-
payload.messages = messages;
|
|
136
|
-
}
|
|
137
|
-
return { ...payload, ...options };
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
private _prepareParams(options: { [key: string]: any }): { [key: string]: any } {
|
|
141
|
-
return Object.fromEntries(Object.entries(options).filter(([_, v]) => v != null));
|
|
142
|
-
}
|
|
143
|
-
}
|