mem0ai 1.0.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/README.md +224 -0
- package/examples/basic-usage.js +30 -0
- package/package.json +15 -0
- package/src/index.js +113 -0
package/README.md
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
# mem0ai
|
|
2
|
+
|
|
3
|
+
Get started with Mem0 Platform in minutes using the Node.js client.
|
|
4
|
+
|
|
5
|
+
## 1. Installation
|
|
6
|
+
|
|
7
|
+
Install the Mem0 Node.js package:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install mem0ai
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 2. API Key Setup
|
|
14
|
+
|
|
15
|
+
1. Sign in to [Mem0 Platform](https://app.mem0.ai/dashboard/api-keys)
|
|
16
|
+
2. Copy your API Key from the dashboard
|
|
17
|
+
|
|
18
|
+
## 3. Instantiate Client
|
|
19
|
+
|
|
20
|
+
```javascript
|
|
21
|
+
const MemoryClient = require('mem0ai');
|
|
22
|
+
const client = new MemoryClient('your-api-key');
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Alternatively, you can set the `MEM0_API_KEY` environment variable and instantiate the client without passing the API key:
|
|
26
|
+
|
|
27
|
+
```javascript
|
|
28
|
+
const MemoryClient = require('mem0ai');
|
|
29
|
+
const client = new MemoryClient();
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## 4. Memory Operations
|
|
33
|
+
|
|
34
|
+
Mem0 provides a simple and customizable interface for performing CRUD operations on memory.
|
|
35
|
+
|
|
36
|
+
### 4.1 Create Memories
|
|
37
|
+
|
|
38
|
+
You can create long-term and short-term memories for your users, AI Agents, etc. Here are some examples:
|
|
39
|
+
|
|
40
|
+
#### Long-term memory for a user
|
|
41
|
+
|
|
42
|
+
```javascript
|
|
43
|
+
const messages = [
|
|
44
|
+
{ role: "user", content: "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts." },
|
|
45
|
+
{ 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." }
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
client.add(messages, { user_id: "alex" })
|
|
49
|
+
.then(result => console.log(result))
|
|
50
|
+
.catch(error => console.error(error));
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
#### Short-term memory for a user session
|
|
54
|
+
|
|
55
|
+
```javascript
|
|
56
|
+
const messages = [
|
|
57
|
+
{ role: "user", content: "I'm planning a trip to Japan next month." },
|
|
58
|
+
{ 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?" },
|
|
59
|
+
{ role: "user", content: "Yes, please! Especially in Tokyo." },
|
|
60
|
+
{ 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." }
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
client.add(messages, { user_id: "alex123", session_id: "trip-planning-2024" })
|
|
64
|
+
.then(result => console.log(result))
|
|
65
|
+
.catch(error => console.error(error));
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
#### Long-term memory for agents
|
|
69
|
+
|
|
70
|
+
```javascript
|
|
71
|
+
const messages = [
|
|
72
|
+
{ role: "system", content: "You are a personalized travel assistant. Remember user preferences and provide tailored recommendations." },
|
|
73
|
+
{ 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." }
|
|
74
|
+
];
|
|
75
|
+
|
|
76
|
+
client.add(messages, { agent_id: "travel-assistant" })
|
|
77
|
+
.then(result => console.log(result))
|
|
78
|
+
.catch(error => console.error(error));
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### 4.2 Search Relevant Memories
|
|
82
|
+
|
|
83
|
+
You can get related memories for a given natural language question using our search method.
|
|
84
|
+
|
|
85
|
+
```javascript
|
|
86
|
+
const query = "What do you know about me?";
|
|
87
|
+
client.search(query, { user_id: "alex" })
|
|
88
|
+
.then(results => console.log(results))
|
|
89
|
+
.catch(error => console.error(error));
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Similarly, you can search for agent memories by passing the `agent_id` option:
|
|
93
|
+
|
|
94
|
+
```javascript
|
|
95
|
+
client.search("What are the learnings from previous runs?", { agent_id: "travel-assistant" })
|
|
96
|
+
.then(results => console.log(results))
|
|
97
|
+
.catch(error => console.error(error));
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### 4.3 Get All Memories
|
|
101
|
+
|
|
102
|
+
Fetch all memories for a user, agent, or session using the getAll() method.
|
|
103
|
+
|
|
104
|
+
#### Get all memories of an AI Agent
|
|
105
|
+
|
|
106
|
+
```javascript
|
|
107
|
+
client.getAll({ agent_id: "travel-assistant" })
|
|
108
|
+
.then(memories => console.log(memories))
|
|
109
|
+
.catch(error => console.error(error));
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
#### Get all memories of a user
|
|
113
|
+
|
|
114
|
+
```javascript
|
|
115
|
+
client.getAll({ user_id: "alex" })
|
|
116
|
+
.then(memories => console.log(memories))
|
|
117
|
+
.catch(error => console.error(error));
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
#### Get short-term memories for a session
|
|
121
|
+
|
|
122
|
+
```javascript
|
|
123
|
+
client.getAll({ user_id: "alex123", session_id: "trip-planning-2024" })
|
|
124
|
+
.then(memories => console.log(memories))
|
|
125
|
+
.catch(error => console.error(error));
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
#### Get specific memory
|
|
129
|
+
|
|
130
|
+
```javascript
|
|
131
|
+
client.get("memory-id-here")
|
|
132
|
+
.then(memory => console.log(memory))
|
|
133
|
+
.catch(error => console.error(error));
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### 4.4 Memory History
|
|
137
|
+
|
|
138
|
+
Get history of how a memory has changed over time:
|
|
139
|
+
|
|
140
|
+
```javascript
|
|
141
|
+
// Add some message to create history
|
|
142
|
+
let messages = [{ role: "user", content: "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.." }];
|
|
143
|
+
client.add(messages, { user_id: "alex" })
|
|
144
|
+
.then(result => {
|
|
145
|
+
// Add second message to update history
|
|
146
|
+
messages.push({ role: 'user', content: 'I turned vegetarian now.' });
|
|
147
|
+
return client.add(messages, { user_id: "alex" });
|
|
148
|
+
})
|
|
149
|
+
.then(result => {
|
|
150
|
+
// Get history of how memory changed over time
|
|
151
|
+
const memoryId = result.id; // Assuming the API returns the memory ID
|
|
152
|
+
return client.history(memoryId);
|
|
153
|
+
})
|
|
154
|
+
.then(history => console.log(history))
|
|
155
|
+
.catch(error => console.error(error));
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### 4.5 Delete Memory
|
|
159
|
+
|
|
160
|
+
Delete specific memory:
|
|
161
|
+
|
|
162
|
+
```javascript
|
|
163
|
+
client.delete("memory-id-here")
|
|
164
|
+
.then(result => console.log(result))
|
|
165
|
+
.catch(error => console.error(error));
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Delete all memories of a user:
|
|
169
|
+
|
|
170
|
+
```javascript
|
|
171
|
+
client.deleteAll({ user_id: "alex" })
|
|
172
|
+
.then(result => console.log(result))
|
|
173
|
+
.catch(error => console.error(error));
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Fun fact: You can also delete the memory using the `add()` method by passing a natural language command:
|
|
177
|
+
|
|
178
|
+
```javascript
|
|
179
|
+
client.add("Delete all of my food preferences", { user_id: "alex" })
|
|
180
|
+
.then(result => console.log(result))
|
|
181
|
+
.catch(error => console.error(error));
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## 5. Error Handling
|
|
185
|
+
|
|
186
|
+
The MemoryClient throws `APIError` for any API-related errors. You can catch and handle these errors as follows:
|
|
187
|
+
|
|
188
|
+
```javascript
|
|
189
|
+
client.add(messages, { user_id: "alex" })
|
|
190
|
+
.then(result => console.log(result))
|
|
191
|
+
.catch(error => {
|
|
192
|
+
if (error.name === 'APIError') {
|
|
193
|
+
console.error('API Error:', error.message);
|
|
194
|
+
} else {
|
|
195
|
+
console.error('Unexpected error:', error);
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## 6. Using with async/await
|
|
201
|
+
|
|
202
|
+
All methods of the MemoryClient return promises, so you can use them with async/await:
|
|
203
|
+
|
|
204
|
+
```javascript
|
|
205
|
+
async function addMemory() {
|
|
206
|
+
try {
|
|
207
|
+
const result = await client.add(messages, { user_id: "alex" });
|
|
208
|
+
console.log(result);
|
|
209
|
+
} catch (error) {
|
|
210
|
+
console.error('Error adding memory:', error);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
addMemory();
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## Getting Help
|
|
218
|
+
|
|
219
|
+
If you have any questions or need assistance, please reach out to us:
|
|
220
|
+
|
|
221
|
+
- Email: support@mem0.ai
|
|
222
|
+
- [Join our discord community](https://mem0.ai/discord)
|
|
223
|
+
- [Join our slack community](https://mem0.ai/slack)
|
|
224
|
+
- GitHub Issues: [Report bugs or request features](https://github.com/mem0ai/mem0ai-node/issues)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const MemoryClient = require('../src/index');
|
|
2
|
+
|
|
3
|
+
async function main() {
|
|
4
|
+
const apiKey = 'your_api_key_here';
|
|
5
|
+
const client = new MemoryClient(apiKey);
|
|
6
|
+
|
|
7
|
+
try {
|
|
8
|
+
// Add a new memory
|
|
9
|
+
const addResult = await client.add('This is a test memory');
|
|
10
|
+
console.log('Memory added:', addResult);
|
|
11
|
+
|
|
12
|
+
// Get all memories
|
|
13
|
+
const allMemories = await client.getAll();
|
|
14
|
+
console.log('All memories:', allMemories);
|
|
15
|
+
|
|
16
|
+
// Search memories
|
|
17
|
+
const searchResult = await client.search('test');
|
|
18
|
+
console.log('Search results:', searchResult);
|
|
19
|
+
|
|
20
|
+
// Delete a memory (assuming we have a memory ID from the add result)
|
|
21
|
+
if (addResult && addResult.id) {
|
|
22
|
+
const deleteResult = await client.delete(addResult.id);
|
|
23
|
+
console.log('Memory deleted:', deleteResult);
|
|
24
|
+
}
|
|
25
|
+
} catch (error) {
|
|
26
|
+
console.error('Error:', error.message);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
main();
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mem0ai",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "The memory layer for personalized AI",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": ["mem0", "api", "client", "memory", "llm", "long-term-memory", "ai"],
|
|
10
|
+
"author": "Deshraj Yadav",
|
|
11
|
+
"license": "Apache-2.0",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"axios": "^0.21.1"
|
|
14
|
+
}
|
|
15
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
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
|
+
|
|
44
|
+
async _validateApiKey() {
|
|
45
|
+
try {
|
|
46
|
+
await this.client.get('/memories/', { params: { user_id: 'test' } });
|
|
47
|
+
} catch (error) {
|
|
48
|
+
throw new Error('Invalid API Key. Please get a valid API Key from https://app.mem0.ai');
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
@apiErrorHandler
|
|
53
|
+
async add(messages, options = {}) {
|
|
54
|
+
const payload = this._preparePayload(messages, options);
|
|
55
|
+
const response = await this.client.post('/memories/', payload);
|
|
56
|
+
return response.data;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@apiErrorHandler
|
|
60
|
+
async get(memoryId) {
|
|
61
|
+
const response = await this.client.get(`/memories/${memoryId}/`);
|
|
62
|
+
return response.data;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
@apiErrorHandler
|
|
66
|
+
async getAll(options = {}) {
|
|
67
|
+
const params = this._prepareParams(options);
|
|
68
|
+
const response = await this.client.get('/memories/', { params });
|
|
69
|
+
return response.data;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
@apiErrorHandler
|
|
73
|
+
async search(query, options = {}) {
|
|
74
|
+
const payload = { query, ...options };
|
|
75
|
+
const response = await this.client.post('/memories/search/', payload);
|
|
76
|
+
return response.data;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
@apiErrorHandler
|
|
80
|
+
async delete(memoryId) {
|
|
81
|
+
const response = await this.client.delete(`/memories/${memoryId}/`);
|
|
82
|
+
return response.data;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
@apiErrorHandler
|
|
86
|
+
async deleteAll(options = {}) {
|
|
87
|
+
const params = this._prepareParams(options);
|
|
88
|
+
const response = await this.client.delete('/memories/', { params });
|
|
89
|
+
return response.data;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
@apiErrorHandler
|
|
93
|
+
async history(memoryId) {
|
|
94
|
+
const response = await this.client.get(`/memories/${memoryId}/history/`);
|
|
95
|
+
return response.data;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
_preparePayload(messages, options) {
|
|
99
|
+
const payload = {};
|
|
100
|
+
if (typeof messages === 'string') {
|
|
101
|
+
payload.messages = [{ role: 'user', content: messages }];
|
|
102
|
+
} else if (Array.isArray(messages)) {
|
|
103
|
+
payload.messages = messages;
|
|
104
|
+
}
|
|
105
|
+
return { ...payload, ...options };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
_prepareParams(options) {
|
|
109
|
+
return Object.fromEntries(Object.entries(options).filter(([_, v]) => v != null));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
module.exports = MemoryClient;
|