Package Exports
- mem0ai
- mem0ai/src/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mem0ai) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
mem0ai
Get started with Mem0 Platform in minutes using the Node.js client.
1. Installation
Install the Mem0 Node.js package:
npm install mem0ai2. API Key Setup
- Sign in to Mem0 Platform
- Copy your API Key from the dashboard
3. Instantiate Client
const MemoryClient = require('mem0ai');
const client = new MemoryClient('your-api-key');Alternatively, you can set the MEM0_API_KEY environment variable and instantiate the client without passing the API key:
const MemoryClient = require('mem0ai');
const client = new MemoryClient();4. Memory Operations
Mem0 provides a simple and customizable interface for performing CRUD operations on memory.
4.1 Create Memories
You can create long-term and short-term memories for your users, AI Agents, etc. Here are some examples:
Long-term memory for a user
const messages = [
{ role: "user", content: "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts." },
{ 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." }
];
client.add(messages, { user_id: "alex" })
.then(result => console.log(result))
.catch(error => console.error(error));Short-term memory for a user session
const messages = [
{ role: "user", content: "I'm planning a trip to Japan next month." },
{ 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?" },
{ role: "user", content: "Yes, please! Especially in Tokyo." },
{ 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." }
];
client.add(messages, { user_id: "alex123", session_id: "trip-planning-2024" })
.then(result => console.log(result))
.catch(error => console.error(error));Long-term memory for agents
const messages = [
{ role: "system", content: "You are a personalized travel assistant. Remember user preferences and provide tailored recommendations." },
{ 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." }
];
client.add(messages, { agent_id: "travel-assistant" })
.then(result => console.log(result))
.catch(error => console.error(error));4.2 Search Relevant Memories
You can get related memories for a given natural language question using our search method.
const query = "What do you know about me?";
client.search(query, { user_id: "alex" })
.then(results => console.log(results))
.catch(error => console.error(error));Similarly, you can search for agent memories by passing the agent_id option:
client.search("What are the learnings from previous runs?", { agent_id: "travel-assistant" })
.then(results => console.log(results))
.catch(error => console.error(error));4.3 Get All Memories
Fetch all memories for a user, agent, or session using the getAll() method.
Get all memories of an AI Agent
client.getAll({ agent_id: "travel-assistant" })
.then(memories => console.log(memories))
.catch(error => console.error(error));Get all memories of a user
client.getAll({ user_id: "alex" })
.then(memories => console.log(memories))
.catch(error => console.error(error));Get short-term memories for a session
client.getAll({ user_id: "alex123", session_id: "trip-planning-2024" })
.then(memories => console.log(memories))
.catch(error => console.error(error));Get specific memory
client.get("memory-id-here")
.then(memory => console.log(memory))
.catch(error => console.error(error));4.4 Get all users
Get all users for which you have memories.
client.users()
.then(users => console.log(users))
.catch(error => console.error(error));4.5 Memory History
Get history of how a memory has changed over time:
// Add some message to create history
let messages = [{ role: "user", content: "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.." }];
client.add(messages, { user_id: "alex" })
.then(result => {
// Add second message to update history
messages.push({ role: 'user', content: 'I turned vegetarian now.' });
return client.add(messages, { user_id: "alex" });
})
.then(result => {
// Get history of how memory changed over time
const memoryId = result.id; // Assuming the API returns the memory ID
return client.history(memoryId);
})
.then(history => console.log(history))
.catch(error => console.error(error));4.6 Delete Memory
Delete specific memory:
client.delete("memory-id-here")
.then(result => console.log(result))
.catch(error => console.error(error));Delete all memories of a user:
client.deleteAll({ user_id: "alex" })
.then(result => console.log(result))
.catch(error => console.error(error));Fun fact: You can also delete the memory using the add() method by passing a natural language command:
client.add("Delete all of my food preferences", { user_id: "alex" })
.then(result => console.log(result))
.catch(error => console.error(error));5. Error Handling
The MemoryClient throws APIError for any API-related errors. You can catch and handle these errors as follows:
client.add(messages, { user_id: "alex" })
.then(result => console.log(result))
.catch(error => {
if (error.name === 'APIError') {
console.error('API Error:', error.message);
} else {
console.error('Unexpected error:', error);
}
});6. Using with async/await
All methods of the MemoryClient return promises, so you can use them with async/await:
async function addMemory() {
try {
const result = await client.add(messages, { user_id: "alex" });
console.log(result);
} catch (error) {
console.error('Error adding memory:', error);
}
}
addMemory();Getting Help
If you have any questions or need assistance, please reach out to us:
- Email: founders@mem0.ai
- Join our discord community
- Join our slack community
- GitHub Issues: Report bugs or request features