Update docs and README (#1546)

This commit is contained in:
Deshraj Yadav
2024-07-23 01:02:55 -07:00
committed by GitHub
parent ef706ad976
commit 04b0297ae4
4 changed files with 258 additions and 186 deletions

View File

@@ -1,5 +1,5 @@
<p align="center"> <p align="center">
<img src="docs/images/mem0-bg.png" width="500px" alt="Mem0 Logo"> <img src="docs/images/banner.png" width="800px" alt="Mem0 Logo">
</p> </p>
<p align="center"> <p align="center">
@@ -12,6 +12,9 @@
<a href="https://x.com/mem0ai"> <a href="https://x.com/mem0ai">
<img src="https://img.shields.io/twitter/follow/mem0ai" alt="Mem0 Twitter"> <img src="https://img.shields.io/twitter/follow/mem0ai" alt="Mem0 Twitter">
</a> </a>
<a href="https://www.ycombinator.com/companies/mem0"><img src="https://img.shields.io/badge/Y%20Combinator-S24-orange?style=flat-square" alt="Y Combinator S24"></a>
<a href="https://www.npmjs.com/package/mem0ai"><img src="https://img.shields.io/npm/v/mem0ai?style=flat-square&label=npm+mem0ai" alt="mem0ai npm package"></a>
<a href="https://pypi.python.org/pypi/mem0ai"><img src="https://img.shields.io/pypi/v/mem0ai.svg?style=flat-square&label=pypi+mem0ai" alt="mem0ai Python package on PyPi"></a>
</p> </p>
# Mem0: The Memory Layer for Personalized AI # Mem0: The Memory Layer for Personalized AI

BIN
docs/images/banner.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 177 KiB

View File

@@ -16,11 +16,14 @@ description: 'Welcome to the Mem0 docs!'
If you are looking to quick start, jump to one of the following links: If you are looking to quick start, jump to one of the following links:
<CardGroup cols={2}> <CardGroup cols={2}>
<Card title="Quickstart" icon="square-1" href="/quickstart/"> <Card title="Open Source Quickstart" icon="square-1" href="/quickstart/">
Jump to quickstart section to get started Get started with Mem0 open source
</Card> </Card>
<Card title="Examples" icon="square-2" href="/examples/overview/"> <Card title="Mem0 Platform Quickstart" icon="square-2" href="/platform/quickstart/">
Checkout curated examples Begin with Mem0 Platform
</Card>
<Card title="Examples" icon="square-3" href="/examples/overview/">
Explore practical use cases
</Card> </Card>
</CardGroup> </CardGroup>

View File

@@ -5,12 +5,17 @@ description: 'Get started with Mem0 Platform in minutes'
## 1. Installation ## 1. Installation
Install the Mem0 Python package: <CodeGroup>
```bash pip
```bash
pip install mem0ai pip install mem0ai
``` ```
```bash npm
npm install mem0ai
```
</CodeGroup>
## 2. API Key Setup ## 2. API Key Setup
1. Sign in to [Mem0 Platform](https://app.mem0.ai/dashboard/api-keys) 1. Sign in to [Mem0 Platform](https://app.mem0.ai/dashboard/api-keys)
@@ -20,26 +25,32 @@ pip install mem0ai
## 3. Instantiate Client ## 3. Instantiate Client
```python <CodeGroup>
```python Python
from mem0 import MemoryClient from mem0 import MemoryClient
client = MemoryClient(api_key="your-api-key") client = MemoryClient(api_key="your-api-key")
``` ```
```javascript JavaScript
const MemoryClient = require('mem0ai');
const client = new MemoryClient('your-api-key');
```
</CodeGroup>
## 4. Memory Operations ## 4. Memory Operations
Mem0 provides a simple and customizable interface for performing CRUD operations on memory. Mem0 provides a simple and customizable interface for performing CRUD operations on memory.
### 4.1 Create Memories ### 4.1 Create Memories
You can create long-term and short-term memories for your users, AI Agents, etc. Here are some examples: 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 #### Long-term memory for a user
```python Code <CodeGroup>
from mem0 import MemoryClient
client = MemoryClient(api_key="your-api-key")
```python Python
messages = [ messages = [
{"role": "user", "content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts."}, {"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."} {"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."}
@@ -47,12 +58,35 @@ messages = [
client.add(messages, user_id="alex") client.add(messages, user_id="alex")
``` ```
```javascript JavaScript
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(response => console.log(response))
.catch(error => console.error(error));
```
```bash cURL
curl -X POST "https://api.mem0.ai/v1/memories/" \
-H "Authorization: Token your-api-key" \
-H "Content-Type: application/json" \
-d '{
"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."}
],
"user_id": "alex"
}'
```
</CodeGroup>
#### Short-term memory for a user session #### Short-term memory for a user session
```python Code <CodeGroup>
from mem0 import MemoryClient
client = MemoryClient(api_key="your-api-key")
```python Python
messages = [ messages = [
{"role": "user", "content": "I'm planning a trip to Japan next month."}, {"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": "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?"},
@@ -62,12 +96,40 @@ messages = [
client.add(messages, user_id="alex123", session_id="trip-planning-2024") client.add(messages, user_id="alex123", session_id="trip-planning-2024")
``` ```
```javascript JavaScript
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(response => console.log(response))
.catch(error => console.error(error));
```
```bash cURL
curl -X POST "https://api.mem0.ai/v1/memories/" \
-H "Authorization: Token your-api-key" \
-H "Content-Type: application/json" \
-d '{
"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."}
],
"user_id": "alex123",
"session_id": "trip-planning-2024"
}'
```
</CodeGroup>
#### Long-term memory for agents #### Long-term memory for agents
```python Code <CodeGroup>
from mem0 import MemoryClient
client = MemoryClient(api_key="your-api-key")
```python Python
messages = [ messages = [
{"role": "system", "content": "You are a personalized travel assistant. Remember user preferences and provide tailored recommendations."}, {"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."} {"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."}
@@ -75,21 +137,62 @@ messages = [
client.add(messages, agent_id="travel-assistant") client.add(messages, agent_id="travel-assistant")
``` ```
```javascript JavaScript
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(response => console.log(response))
.catch(error => console.error(error));
```
```bash cURL
curl -X POST "https://api.mem0.ai/v1/memories/" \
-H "Authorization: Token your-api-key" \
-H "Content-Type: application/json" \
-d '{
"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."}
],
"agent_id": "travel-assistant"
}'
```
</CodeGroup>
You can monitor memory operations on the platform: You can monitor memory operations on the platform:
![Mem0 Platform Activity](/images/platform/activity.png) ![Mem0 Platform Activity](/images/platform/activity.png)
### 4.2 Search Relevant Memories ### 4.2 Search Relevant Memories
You can also get related memories for a given natural language question using our search method. You can also get related memories for a given natural language question using our search method.
<CodeGroup> <CodeGroup>
```python Code
```python Python
query = "What do you know about me?" query = "What do you know about me?"
client.search(query, user_id="alex") client.search(query, user_id="alex")
``` ```
```javascript JavaScript
const query = "What do you know about me?";
client.search(query, { user_id: "alex" })
.then(results => console.log(results))
.catch(error => console.error(error));
```
```bash cURL
curl -X POST "https://api.mem0.ai/v1/memories/search/" \
-H "Authorization: Token your-api-key" \
-H "Content-Type: application/json" \
-d '{
"query": "What do you know about me?",
"user_id": "alex"
}'
```
```json Output ```json Output
[ [
{ {
@@ -115,78 +218,46 @@ client.search(query, user_id="alex")
``` ```
</CodeGroup> </CodeGroup>
Similarly, you can search for agent memories by passing the `agent_id` argument.
```python Code
client.search("What are the learnings from previous runs?", agent_id="travel-assistant")
```
### 4.3 Get All Memories ### 4.3 Get All Memories
Fetch all memories for a user, agent, or session using the get_all() method. Fetch all memories for a user, agent, or session using the getAll() method.
#### Get all memories of an AI Agent #### Get all memories of an AI Agent
<CodeGroup> <CodeGroup>
```python Python
```python Code
client.get_all(agent_id="travel-assistant") client.get_all(agent_id="travel-assistant")
``` ```
```json Output ```javascript JavaScript
[ client.getAll({ agent_id: "travel-assistant" })
{ .then(memories => console.log(memories))
"id": "48677fae-16c4-4d57-9c27-f984f290722a", .catch(error => console.error(error));
"memory": "Will remember personalized travel preferences for each user.", ```
"input": [
{ ```bash cURL
"role": "system", curl -X GET "https://api.mem0.ai/v1/memories/?agent_id=travel-assistant" \
"content": "You are a personalized travel assistant. Remember user preferences and provide tailored recommendations." -H "Authorization: Token your-api-key"
},
{
"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."
}
],
"agent_id": "travel-assistant",
"hash": "644d5f40af13d0525305d8dfadc00fd1",
"metadata": null,
"created_at": "2024-07-20T01:25:22.122299-07:00",
"updated_at": "2024-07-20T01:25:22.122327-07:00"
}
]
``` ```
</CodeGroup> </CodeGroup>
#### Get all memories of user #### Get all memories of user
<CodeGroup> <CodeGroup>
```python Code
```python Python
user_memories = client.get_all(user_id="alex") user_memories = client.get_all(user_id="alex")
``` ```
```json Output ```javascript JavaScript
[ client.getAll({ user_id: "alex" })
{ .then(memories => console.log(memories))
"id": "7f165f7e-b411-4afe-b7e5-35789b72c4a5", .catch(error => console.error(error));
"memory": "Name: Alex. Vegetarian. Allergic to nuts.", ```
"input": [
{ ```bash cURL
"role": "user", curl -X GET "https://api.mem0.ai/v1/memories/?user_id=alex" \
"content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts." -H "Authorization: Token your-api-key"
},
{
"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."
}
],
"user_id": "alex",
"hash": "9ee7e1455e84d1dab700ed8749aed75a",
"metadata": null,
"created_at": "2024-07-20T01:30:36.275141-07:00",
"updated_at": "2024-07-20T01:30:36.275172-07:00"
}
]
``` ```
</CodeGroup> </CodeGroup>
@@ -194,80 +265,39 @@ user_memories = client.get_all(user_id="alex")
<CodeGroup> <CodeGroup>
```python Code ```python Python
short_term_memories = client.get_all(user_id="alex123", session_id="trip-planning-2024") short_term_memories = client.get_all(user_id="alex123", session_id="trip-planning-2024")
``` ```
```python Output ```javascript JavaScript
[ client.getAll({ user_id: "alex123", session_id: "trip-planning-2024" })
{ .then(memories => console.log(memories))
"id": "582bbe6d-506b-48c6-a4c6-5df3b1e63428", .catch(error => console.error(error));
"memory": "Planning a trip to Japan next month. Interested in vegetarian restaurants in Tokyo.", ```
"input": [
{ ```bash cURL
"role": "user", curl -X GET "https://api.mem0.ai/v1/memories/?user_id=alex123&session_id=trip-planning-2024" \
"content": "I'm planning a trip to Japan next month." -H "Authorization: Token your-api-key"
},
{
"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."
}
],
"user_id": "alex123",
"hash": "d2088c936e259f2f5d2d75543d31401c",
"metadata": null,
"created_at": "2024-07-20T01:34:09.748379-07:00",
"updated_at": "2024-07-20T01:34:09.748391-07:00"
}
]
``` ```
</CodeGroup> </CodeGroup>
#### Get specific memory #### Get specific memory
<CodeGroup> <CodeGroup>
```python Code ```python Python
memory = client.get(memory_id="582bbe6d-506b-48c6-a4c6-5df3b1e63428") memory = client.get(memory_id="582bbe6d-506b-48c6-a4c6-5df3b1e63428")
``` ```
```python Output ```javascript JavaScript
{ client.get("582bbe6d-506b-48c6-a4c6-5df3b1e63428")
"id": "582bbe6d-506b-48c6-a4c6-5df3b1e63428", .then(memory => console.log(memory))
"memory": "Planning a trip to Japan next month. Interested in vegetarian restaurants in Tokyo.", .catch(error => console.error(error));
"input": [ ```
{
"role": "user", ```bash cURL
"content": "I'm planning a trip to Japan next month." curl -X GET "https://api.mem0.ai/v1/memories/582bbe6d-506b-48c6-a4c6-5df3b1e63428" \
}, -H "Authorization: Token your-api-key"
{
"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."
}
],
"user_id": "alex123",
"hash": "d2088c936e259f2f5d2d75543d31401c",
"metadata": null,
"created_at": "2024-07-20T01:34:09.748379-07:00",
"updated_at": "2024-07-20T01:34:09.748391-07:00"
}
``` ```
</CodeGroup> </CodeGroup>
@@ -277,7 +307,7 @@ Get history of how a memory has changed over time
<CodeGroup> <CodeGroup>
```python Code ```python Python
# Add some message to create history # Add some message to create history
messages = [{"role": "user", "content": "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.."}] 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") client.add(messages, user_id="alex")
@@ -292,82 +322,118 @@ history = client.history(memory_id)
print(history) print(history)
``` ```
```json Output ```javascript JavaScript
[ // 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.." }];
'id': '5a718c7e-7ed2-4c72-8c55-d7896dc61ca3', client.add(messages, { user_id: "alex" })
'memory_id': 'c6c5da66-d851-4653-8a09-ec7b2c1c9cc9', .then(result => {
'input': [ // Add second message to update history
{ messages.push({ role: 'user', content: 'I turned vegetarian now.' });
'role': 'user', return client.add(messages, { user_id: "alex" });
'content': "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.." })
} .then(result => {
], // Get history of how memory changed over time
'old_memory': None, const memoryId = result.id; // Assuming the API returns the memory ID
'new_memory': 'Recently tried chicken and loved it. Interested in trying more non-vegetarian dishes.', return client.history(memoryId);
'event': 'ADD', })
'metadata': None, .then(history => console.log(history))
'created_at': '2024-07-20T02:13:35.842720-07:00', .catch(error => console.error(error));
'updated_at': '2024-07-20T02:13:35.818065-07:00' ```
},
{ ```bash cURL
'id': 'e52e2c73-4b97-4154-9844-aa44c6ee57f5', # First, add the initial memory
'memory_id': 'c6c5da66-d851-4653-8a09-ec7b2c1c9cc9', curl -X POST "https://api.mem0.ai/v1/memories/" \
'input': [ -H "Authorization: Token your-api-key" \
{ -H "Content-Type: application/json" \
'role': 'user', -d '{
'content': "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.." "messages": [{"role": "user", "content": "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.."}],
}, "user_id": "alex"
{ }'
'role': 'user',
'content': 'I turned vegetarian now.' # Then, update the memory
} curl -X POST "https://api.mem0.ai/v1/memories/" \
], -H "Authorization: Token your-api-key" \
'old_memory': 'Recently tried chicken and loved it. Interested in trying more non-vegetarian dishes.', -H "Content-Type: application/json" \
'new_memory': '', -d '{
'event': 'DELETE', "messages": [
'metadata': None, {"role": "user", "content": "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.."},
'created_at': '2024-07-20T02:13:38.070492-07:00', {"role": "user", "content": "I turned vegetarian now."}
'updated_at': '2024-07-20T02:13:38.060219-07:00' ],
} "user_id": "alex"
] }'
# Finally, get the history (replace <memory-id-here> with the actual memory ID)
curl -X GET "https://api.mem0.ai/v1/memories/<memory-id-here>/history/" \
-H "Authorization: Token your-api-key"
``` ```
</CodeGroup> </CodeGroup>
### 4.5 Delete Memory ### 4.5 Delete Memory
Delete specific memory: Delete specific memory:
<CodeGroup> <CodeGroup>
```python Code ```python Python
client.delete(memory_id) client.delete(memory_id)
``` ```
```python Output ```javascript JavaScript
{'message': 'Memory deleted successfully!'} client.delete("memory-id-here")
.then(result => console.log(result))
.catch(error => console.error(error));
```
```bash cURL
curl -X DELETE "https://api.mem0.ai/v1/memories/memory-id-here" \
-H "Authorization: Token your-api-key"
``` ```
</CodeGroup> </CodeGroup>
Delete all memories of a user: Delete all memories of a user:
<CodeGroup> <CodeGroup>
```python Code ```python Python
client.delete_all(user_id="alex") client.delete_all(user_id="alex")
``` ```
```python Output ```javascript JavaScript
{'message': 'Memories deleted successfully!'} client.deleteAll({ user_id: "alex" })
.then(result => console.log(result))
.catch(error => console.error(error));
```
```bash cURL
curl -X DELETE "https://api.mem0.ai/v1/memories/?user_id=alex" \
-H "Authorization: Token your-api-key"
``` ```
</CodeGroup> </CodeGroup>
Fun fact: You can also delete the memory using the `add()` method by passing natural language command. Fun fact: You can also delete the memory using the `add()` method by passing a natural language command:
```python Code <CodeGroup>
```python Python
client.add("Delete all of my food preferences", user_id="alex") client.add("Delete all of my food preferences", user_id="alex")
```
```javascript JavaScript
client.add("Delete all of my food preferences", { user_id: "alex" })
.then(result => console.log(result))
.catch(error => console.error(error));
```
```bash cURL
curl -X POST "https://api.mem0.ai/v1/memories/" \
-H "Authorization: Token your-api-key" \
-H "Content-Type: application/json" \
-d '{
"messages": [{"role": "user", "content": "Delete all of my food preferences"}],
"user_id": "alex"
}'
```
</CodeGroup>
If you have any questions, please feel free to reach out to us using one of the following methods: If you have any questions, please feel free to reach out to us using one of the following methods: