[Mem0] Update platform client, improve deduction logic and update client docs (#1510)
This commit is contained in:
@@ -27,162 +27,250 @@ client = MemoryClient(api_key="your-api-key")
|
|||||||
|
|
||||||
## 4. Memory Operations
|
## 4. Memory Operations
|
||||||
|
|
||||||
We provide a simple yet customizable interface for performing CRUD operations on memory. Here is how you can create and get memories:
|
Mem0 provides a simple and customizable interface for performing CRUD operations on memory.
|
||||||
|
|
||||||
|
|
||||||
### 4.1 Create Memories
|
### 4.1 Create Memories
|
||||||
|
|
||||||
For users (long-term memory):
|
You can create long-term and short-term memories for your users, AI Agents, etc. Here are some examples:
|
||||||
|
|
||||||
```python
|
#### Long-term memory for a user
|
||||||
# create long-term memory for users
|
|
||||||
client.add("Remember my name is Deshraj Yadav.", user_id="deshraj")
|
```python Code
|
||||||
client.add("I like to eat pizza and go out on weekends.", user_id="deshraj")
|
from mem0 import MemoryClient
|
||||||
client.add("Oh I am actually allergic to cheese to cannot eat pizza anymore.", user_id="deshraj")
|
client = MemoryClient(api_key="your-api-key")
|
||||||
|
|
||||||
|
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")
|
||||||
```
|
```
|
||||||
|
|
||||||
Output:
|
#### Short-term memory for a user session
|
||||||
```python
|
|
||||||
{'message': 'Memory added successfully!'}
|
```python Code
|
||||||
|
from mem0 import MemoryClient
|
||||||
|
client = MemoryClient(api_key="your-api-key")
|
||||||
|
|
||||||
|
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")
|
||||||
```
|
```
|
||||||
|
|
||||||
You can see all the memory operations happening on the platform itself.
|
#### Long-term memory for agents
|
||||||
|
|
||||||
|
```python Code
|
||||||
|
from mem0 import MemoryClient
|
||||||
|
client = MemoryClient(api_key="your-api-key")
|
||||||
|
|
||||||
|
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")
|
||||||
|
```
|
||||||
|
|
||||||
|
You can monitor memory operations on the platform:
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|
||||||
You can also add memories for a particular session or for an AI agent that you are building:
|
### 4.2 Search Relevant Memories
|
||||||
|
|
||||||
- For user sessions (short-term memory):
|
|
||||||
|
|
||||||
```python
|
|
||||||
client.add("Deshraj is building Gmail AI agent", user_id="deshraj", session_id="session-1")
|
|
||||||
```
|
|
||||||
|
|
||||||
- For agents (long-term memory):
|
|
||||||
|
|
||||||
```python
|
|
||||||
client.add("Return short responses when responding to emails", agent_id="gmail-agent")
|
|
||||||
```
|
|
||||||
|
|
||||||
### 4.2 Retrieve Memories
|
|
||||||
|
|
||||||
|
You can also get related memories for a given natural language question using our search method.
|
||||||
|
|
||||||
<CodeGroup>
|
<CodeGroup>
|
||||||
|
|
||||||
```python Code
|
```python Code
|
||||||
client.get_all(user_id="deshraj")
|
query = "What do you know about me?"
|
||||||
|
client.search(query, user_id="alex")
|
||||||
```
|
```
|
||||||
|
|
||||||
```python Output
|
```json Output
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
'id': 'dbce6e06-6adf-40b8-9187-3d30bd13b741',
|
"id": "7f165f7e-b411-4afe-b7e5-35789b72c4a5",
|
||||||
'agent': None,
|
"memory": "Name: Alex. Vegetarian. Allergic to nuts.",
|
||||||
'consumer': {
|
"input": [
|
||||||
'id': 8,
|
{
|
||||||
'user_id': 'deshraj',
|
"role": "user",
|
||||||
'metadata': None,
|
"content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts."
|
||||||
'created_at': '2024-07-17T16:47:23.899900-07:00',
|
},
|
||||||
'updated_at': '2024-07-17T16:47:23.899918-07:00'
|
{
|
||||||
},
|
"role": "assistant",
|
||||||
'app': None,
|
"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."
|
||||||
'run': None,
|
}
|
||||||
'hash': '57288ac8a87c4ac8d3ac7f2075d264ca',
|
],
|
||||||
'input': 'Remember my name is Deshraj Yadav.',
|
"user_id": "alex",
|
||||||
'text': 'My name is Deshraj Yadav.',
|
"hash": "9ee7e1455e84d1dab700ed8749aed75a",
|
||||||
'metadata': None,
|
"metadata": null,
|
||||||
'created_at': '2024-07-17T16:47:25.670180-07:00',
|
"created_at": "2024-07-20T01:30:36.275141-07:00",
|
||||||
'updated_at': '2024-07-17T16:47:25.670197-07:00'
|
"updated_at": "2024-07-20T01:30:36.275172-07:00"
|
||||||
},
|
}
|
||||||
{
|
|
||||||
'id': 'f6dec5d1-b5db-45f5-a2fb-3979a0f27d30',
|
|
||||||
'agent': None,
|
|
||||||
'consumer': {
|
|
||||||
'id': 8,
|
|
||||||
'user_id': 'deshraj',
|
|
||||||
# ... other consumer fields ...
|
|
||||||
},
|
|
||||||
# ... other fields ...
|
|
||||||
'text': 'I am allergic to cheese so I cannot eat pizza anymore.',
|
|
||||||
# ... remaining fields ...
|
|
||||||
},
|
|
||||||
# ... additional memory entries ...
|
|
||||||
]
|
]
|
||||||
```
|
```
|
||||||
</CodeGroup>
|
</CodeGroup>
|
||||||
|
|
||||||
Similarly, you can get all memories for an agent:
|
Similarly, you can search for agent memories by passing the `agent_id` argument.
|
||||||
|
|
||||||
```python
|
```python Code
|
||||||
agent_memories = client.get_all(agent_id="gmail-agent")
|
client.search("What are the learnings from previous runs?", agent_id="travel-assistant")
|
||||||
```
|
```
|
||||||
|
|
||||||
Get specific memory:
|
### 4.3 Get All Memories
|
||||||
|
|
||||||
|
Fetch all memories for a user, agent, or session using the get_all() method.
|
||||||
|
|
||||||
|
#### Get all memories of an AI Agent
|
||||||
|
|
||||||
<CodeGroup>
|
<CodeGroup>
|
||||||
|
|
||||||
```python Code
|
```python Code
|
||||||
memory = client.get(memory_id="dbce6e06-6adf-40b8-9187-3d30bd13b741")
|
client.get_all(agent_id="travel-assistant")
|
||||||
```
|
```
|
||||||
|
|
||||||
```python Output
|
```json Output
|
||||||
{
|
[
|
||||||
'id': 'dbce6e06-6adf-40b8-9187-3d30bd13b741',
|
{
|
||||||
'agent': None,
|
"id": "48677fae-16c4-4d57-9c27-f984f290722a",
|
||||||
'consumer': {
|
"memory": "Will remember personalized travel preferences for each user.",
|
||||||
'id': 8,
|
"input": [
|
||||||
'user_id': 'deshraj',
|
{
|
||||||
'metadata': None,
|
"role": "system",
|
||||||
'created_at': '2024-07-17T16:47:23.899900-07:00',
|
"content": "You are a personalized travel assistant. Remember user preferences and provide tailored recommendations."
|
||||||
'updated_at': '2024-07-17T16:47:23.899918-07:00'
|
},
|
||||||
},
|
{
|
||||||
'app': None,
|
"role": "assistant",
|
||||||
'run': None,
|
"content": "Understood. I'll maintain personalized travel preferences for each user and provide customized recommendations based on their dietary restrictions, interests, and past interactions."
|
||||||
'hash': '57288ac8a87c4ac8d3ac7f2075d264ca',
|
}
|
||||||
'input': 'Remember my name is Deshraj Yadav.',
|
],
|
||||||
'text': 'My name is Deshraj Yadav.',
|
"agent_id": "travel-assistant",
|
||||||
'metadata': None,
|
"hash": "644d5f40af13d0525305d8dfadc00fd1",
|
||||||
'created_at': '2024-07-17T16:47:25.670180-07:00',
|
"metadata": null,
|
||||||
'updated_at': '2024-07-17T16:47:25.670197-07:00'
|
"created_at": "2024-07-20T01:25:22.122299-07:00",
|
||||||
}
|
"updated_at": "2024-07-20T01:25:22.122327-07:00"
|
||||||
|
}
|
||||||
|
]
|
||||||
```
|
```
|
||||||
</CodeGroup>
|
</CodeGroup>
|
||||||
|
|
||||||
### 4.3 Update Memory
|
#### Get all memories of user
|
||||||
|
|
||||||
You can also update specific memory by using the following method:
|
<CodeGroup>
|
||||||
|
```python Code
|
||||||
|
user_memories = client.get_all(user_id="alex")
|
||||||
|
```
|
||||||
|
|
||||||
|
```json Output
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"id": "7f165f7e-b411-4afe-b7e5-35789b72c4a5",
|
||||||
|
"memory": "Name: Alex. Vegetarian. Allergic to nuts.",
|
||||||
|
"input": [
|
||||||
|
{
|
||||||
|
"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",
|
||||||
|
"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>
|
||||||
|
|
||||||
|
#### Get short-term memories for a session
|
||||||
|
|
||||||
<CodeGroup>
|
<CodeGroup>
|
||||||
|
|
||||||
```python Code
|
```python Code
|
||||||
client.update(memory_id, data="Updated name is Deshraj Kumar")
|
short_term_memories = client.get_all(user_id="alex123", session_id="trip-planning-2024")
|
||||||
|
```
|
||||||
|
|
||||||
|
```python Output
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"id": "582bbe6d-506b-48c6-a4c6-5df3b1e63428",
|
||||||
|
"memory": "Planning a trip to Japan next month. Interested in vegetarian restaurants in Tokyo.",
|
||||||
|
"input": [
|
||||||
|
{
|
||||||
|
"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",
|
||||||
|
"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>
|
||||||
|
|
||||||
|
|
||||||
|
#### Get specific memory
|
||||||
|
|
||||||
|
<CodeGroup>
|
||||||
|
|
||||||
|
```python Code
|
||||||
|
memory = client.get(memory_id="582bbe6d-506b-48c6-a4c6-5df3b1e63428")
|
||||||
```
|
```
|
||||||
|
|
||||||
```python Output
|
```python Output
|
||||||
{
|
{
|
||||||
'id': 'dbce6e06-6adf-40b8-9187-3d30bd13b741',
|
"id": "582bbe6d-506b-48c6-a4c6-5df3b1e63428",
|
||||||
'agent': None,
|
"memory": "Planning a trip to Japan next month. Interested in vegetarian restaurants in Tokyo.",
|
||||||
'consumer': {
|
"input": [
|
||||||
'id': 8,
|
{
|
||||||
'user_id': 'deshraj',
|
"role": "user",
|
||||||
'metadata': None,
|
"content": "I'm planning a trip to Japan next month."
|
||||||
'created_at': '2024-07-17T16:47:23.899900-07:00',
|
},
|
||||||
'updated_at': '2024-07-17T16:47:23.899918-07:00'
|
{
|
||||||
},
|
"role": "assistant",
|
||||||
'app': None,
|
"content": "That's exciting, Alex! A trip to Japan next month sounds wonderful. Would you like some recommendations for vegetarian-friendly restaurants in Japan?"
|
||||||
'run': None,
|
},
|
||||||
'hash': '57288ac8a87c4ac8d3ac7f2075d264ca',
|
{
|
||||||
'input': 'Updated name is Deshraj Kumar.',
|
"role": "user",
|
||||||
'text': 'Name is Deshraj Kumar.',
|
"content": "Yes, please! Especially in Tokyo."
|
||||||
'metadata': None,
|
},
|
||||||
'created_at': '2024-07-17T16:47:25.670180-07:00',
|
{
|
||||||
'updated_at': '2024-07-17T16:47:25.670197-07:00'
|
"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>
|
||||||
|
|
||||||
|
|
||||||
### 4.4 Memory History
|
### 4.4 Memory History
|
||||||
|
|
||||||
Get history of how a memory has changed over time
|
Get history of how a memory has changed over time
|
||||||
@@ -190,145 +278,64 @@ Get history of how a memory has changed over time
|
|||||||
<CodeGroup>
|
<CodeGroup>
|
||||||
|
|
||||||
```python Code
|
```python Code
|
||||||
|
# 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.."}]
|
||||||
|
client.add(messages, user_id="alex")
|
||||||
|
|
||||||
|
# Add second message to update history
|
||||||
|
messages.append({'role': 'user', 'content': 'I turned vegetarian now.'})
|
||||||
|
client.add(messages, user_id="alex")
|
||||||
|
|
||||||
|
# Get history of how memory changed over time
|
||||||
|
memory_id = "<memory-id-here>"
|
||||||
history = client.history(memory_id)
|
history = client.history(memory_id)
|
||||||
|
print(history)
|
||||||
```
|
```
|
||||||
|
|
||||||
```python Output
|
```json Output
|
||||||
[
|
|
||||||
{
|
|
||||||
'id': '51193804-2ee6-4f81-b4e7-497e98b70858',
|
|
||||||
'memory': {
|
|
||||||
'id': 'dbce6e06-6adf-40b8-9187-3d30bd13b741',
|
|
||||||
'agent': None,
|
|
||||||
'consumer': {
|
|
||||||
'id': 8,
|
|
||||||
'user_id': 'deshraj',
|
|
||||||
'metadata': None,
|
|
||||||
'created_at': '2024-07-17T16:47:23.899900-07:00',
|
|
||||||
'updated_at': '2024-07-17T16:47:23.899918-07:00'
|
|
||||||
},
|
|
||||||
'app': None,
|
|
||||||
'run': None,
|
|
||||||
'hash': '57288ac8a87c4ac8d3ac7f2075d264ca',
|
|
||||||
'input': 'Remember my name is Deshraj Yadav.',
|
|
||||||
'text': 'My name is Deshraj Yadav.',
|
|
||||||
'metadata': None,
|
|
||||||
'created_at': '2024-07-17T16:47:25.670180-07:00',
|
|
||||||
'updated_at': '2024-07-17T16:47:25.670197-07:00'
|
|
||||||
},
|
|
||||||
'hash': '57288ac8a87c4ac8d3ac7f2075d264ca',
|
|
||||||
'event': 'ADD',
|
|
||||||
'input': 'Remember my name is Deshraj Yadav.',
|
|
||||||
'previous_text': None,
|
|
||||||
'text': 'My name is Deshraj Yadav.',
|
|
||||||
'metadata': None,
|
|
||||||
'created_at': '2024-07-17T16:47:25.686899-07:00',
|
|
||||||
'updated_at': '2024-07-17T16:47:25.670197-07:00',
|
|
||||||
'change_description': 'Memory ADD event'
|
|
||||||
}
|
|
||||||
]
|
|
||||||
```
|
|
||||||
</CodeGroup>
|
|
||||||
|
|
||||||
### 4.5 Search for relevant memories
|
|
||||||
|
|
||||||
<CodeGroup>
|
|
||||||
|
|
||||||
```python Code
|
|
||||||
client.search("What does Deshraj like to eat?", user_id="deshraj", limit=3)
|
|
||||||
```
|
|
||||||
|
|
||||||
```python Output
|
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"id": "dbce6e06-6adf-40b8-9187-3d30bd13b741",
|
'id': '5a718c7e-7ed2-4c72-8c55-d7896dc61ca3',
|
||||||
"agent": null,
|
'memory_id': 'c6c5da66-d851-4653-8a09-ec7b2c1c9cc9',
|
||||||
"consumer": {
|
'input': [
|
||||||
"id": 8,
|
{
|
||||||
"user_id": "deshraj",
|
'role': 'user',
|
||||||
"metadata": null,
|
'content': "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.."
|
||||||
"created_at": "...",
|
}
|
||||||
"updated_at": "..."
|
],
|
||||||
},
|
'old_memory': None,
|
||||||
"app": null,
|
'new_memory': 'Recently tried chicken and loved it. Interested in trying more non-vegetarian dishes.',
|
||||||
"run": null,
|
'event': 'ADD',
|
||||||
"hash": "57288ac8a87c4ac8d3ac7f2075d264ca",
|
'metadata': None,
|
||||||
"input": "Remember my name is Deshraj Yadav.",
|
'created_at': '2024-07-20T02:13:35.842720-07:00',
|
||||||
"text": "My name is Deshraj Yadav.",
|
'updated_at': '2024-07-20T02:13:35.818065-07:00'
|
||||||
"metadata": null,
|
|
||||||
"created_at": "2024-07-17T16:47:25.670180-07:00",
|
|
||||||
"updated_at": "..."
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "091dbed6-74b4-4e15-b765-81be2abe0d6b",
|
'id': 'e52e2c73-4b97-4154-9844-aa44c6ee57f5',
|
||||||
"agent": null,
|
'memory_id': 'c6c5da66-d851-4653-8a09-ec7b2c1c9cc9',
|
||||||
"consumer": {
|
'input': [
|
||||||
"id": 8,
|
{
|
||||||
"user_id": "deshraj",
|
'role': 'user',
|
||||||
"metadata": null,
|
'content': "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.."
|
||||||
"created_at": "...",
|
},
|
||||||
"updated_at": "..."
|
{
|
||||||
},
|
'role': 'user',
|
||||||
"app": null,
|
'content': 'I turned vegetarian now.'
|
||||||
"run": null,
|
}
|
||||||
"hash": "622a5a24d5ac54136414a22ec12f9520",
|
],
|
||||||
"input": "Oh I am actually allergic to cheese to cannot eat pizza anymore.",
|
'old_memory': 'Recently tried chicken and loved it. Interested in trying more non-vegetarian dishes.',
|
||||||
"text": "I like to eat pizza and go out on weekends.",
|
'new_memory': '',
|
||||||
"metadata": null,
|
'event': 'DELETE',
|
||||||
"created_at": "2024-07-17T16:49:24.276695-07:00",
|
'metadata': None,
|
||||||
"updated_at": "..."
|
'created_at': '2024-07-20T02:13:38.070492-07:00',
|
||||||
},
|
'updated_at': '2024-07-20T02:13:38.060219-07:00'
|
||||||
{
|
|
||||||
"id": "5fb8f85d-3383-4bad-9d46-f171272478a4",
|
|
||||||
"agent": null,
|
|
||||||
"consumer": {
|
|
||||||
"id": 8,
|
|
||||||
"user_id": "deshraj",
|
|
||||||
"metadata": null,
|
|
||||||
"created_at": "...",
|
|
||||||
"updated_at": "..."
|
|
||||||
},
|
|
||||||
"app": null,
|
|
||||||
"run": {
|
|
||||||
"id": 1,
|
|
||||||
"run_id": "session-1",
|
|
||||||
"name": "",
|
|
||||||
"metadata": null,
|
|
||||||
"created_at": "...",
|
|
||||||
"updated_at": "..."
|
|
||||||
},
|
|
||||||
"hash": "179ced9649ac2b85350ece4946b1ee9b",
|
|
||||||
"input": "Deshraj is building Gmail AI agent",
|
|
||||||
"text": "Deshraj is building Gmail AI agent",
|
|
||||||
"metadata": null,
|
|
||||||
"created_at": "2024-07-17T16:52:41.278920-07:00",
|
|
||||||
"updated_at": "..."
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "f6dec5d1-b5db-45f5-a2fb-3979a0f27d30",
|
|
||||||
"agent": null,
|
|
||||||
"consumer": {
|
|
||||||
"id": 8,
|
|
||||||
"user_id": "deshraj",
|
|
||||||
"metadata": null,
|
|
||||||
"created_at": "...",
|
|
||||||
"updated_at": "..."
|
|
||||||
},
|
|
||||||
"app": null,
|
|
||||||
"run": null,
|
|
||||||
"hash": "19248f0766044b5973fc0ef1bf3955ef",
|
|
||||||
"input": "Oh I am actually allergic to cheese to cannot eat pizza anymore.",
|
|
||||||
"text": "I am allergic to cheese so I cannot eat pizza anymore.",
|
|
||||||
"metadata": null,
|
|
||||||
"created_at": "2024-07-17T16:49:38.622084-07:00",
|
|
||||||
"updated_at": "..."
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
```
|
```
|
||||||
</CodeGroup>
|
</CodeGroup>
|
||||||
|
|
||||||
|
|
||||||
### 4.6 Delete Memory
|
### 4.5 Delete Memory
|
||||||
|
|
||||||
Delete specific memory:
|
Delete specific memory:
|
||||||
|
|
||||||
@@ -356,3 +363,12 @@ client.delete_all(user_id="alex")
|
|||||||
{'message': 'Memories deleted successfully!'}
|
{'message': 'Memories deleted successfully!'}
|
||||||
```
|
```
|
||||||
</CodeGroup>
|
</CodeGroup>
|
||||||
|
|
||||||
|
Fun fact: You can also delete the memory using the `add()` method by passing natural language command.
|
||||||
|
|
||||||
|
```python Code
|
||||||
|
client.add("Delete all of my food preferences", user_id="alex")
|
||||||
|
|
||||||
|
If you have any questions, please feel free to reach out to us using one of the following methods:
|
||||||
|
|
||||||
|
<Snippet file="get-help.mdx" />
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
import httpx
|
|
||||||
import os
|
|
||||||
import logging
|
import logging
|
||||||
import warnings
|
import os
|
||||||
from typing import Optional, Dict, Any
|
from functools import wraps
|
||||||
|
from typing import Any, Dict, List, Optional, Union
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
from mem0.memory.setup import setup_config
|
from mem0.memory.setup import setup_config
|
||||||
from mem0.memory.telemetry import capture_client_event
|
from mem0.memory.telemetry import capture_client_event
|
||||||
|
|
||||||
@@ -12,232 +14,258 @@ logger = logging.getLogger(__name__)
|
|||||||
setup_config()
|
setup_config()
|
||||||
|
|
||||||
|
|
||||||
|
class APIError(Exception):
|
||||||
|
"""Exception raised for errors in the API."""
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def api_error_handler(func):
|
||||||
|
"""Decorator to handle API errors consistently."""
|
||||||
|
|
||||||
|
@wraps(func)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
try:
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
except httpx.HTTPStatusError as e:
|
||||||
|
logger.error(f"HTTP error occurred: {e}")
|
||||||
|
raise APIError(f"API request failed: {e.response.text}")
|
||||||
|
except httpx.RequestError as e:
|
||||||
|
logger.error(f"Request error occurred: {e}")
|
||||||
|
raise APIError(f"Request failed: {str(e)}")
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
class MemoryClient:
|
class MemoryClient:
|
||||||
|
"""Client for interacting with the Mem0 API.
|
||||||
|
|
||||||
|
This class provides methods to create, retrieve, search, and delete memories
|
||||||
|
using the Mem0 API.
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
api_key (str): The API key for authenticating with the Mem0 API.
|
||||||
|
host (str): The base URL for the Mem0 API.
|
||||||
|
client (httpx.Client): The HTTP client used for making API requests.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, api_key: Optional[str] = None, host: Optional[str] = None):
|
def __init__(self, api_key: Optional[str] = None, host: Optional[str] = None):
|
||||||
"""
|
"""Initialize the MemoryClient.
|
||||||
Initialize the Mem0 client.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
api_key (Optional[str]): API Key from Mem0 Platform. Defaults to environment variable 'MEM0_API_KEY' if not provided.
|
api_key: The API key for authenticating with the Mem0 API. If not provided,
|
||||||
host (Optional[str]): API host URL. Defaults to 'https://api.mem0.ai/v1'.
|
it will attempt to use the MEM0_API_KEY environment variable.
|
||||||
|
host: The base URL for the Mem0 API. Defaults to "https://api.mem0.ai/v1".
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: If no API key is provided or found in the environment.
|
||||||
"""
|
"""
|
||||||
self.api_key = api_key or os.getenv("MEM0_API_KEY")
|
self.api_key = api_key or os.getenv("MEM0_API_KEY")
|
||||||
self.host = host or "https://api.mem0.ai/v1"
|
self.host = host or "https://api.mem0.ai/v1"
|
||||||
|
|
||||||
|
if not self.api_key:
|
||||||
|
raise ValueError("API Key not provided. Please provide an API Key.")
|
||||||
|
|
||||||
self.client = httpx.Client(
|
self.client = httpx.Client(
|
||||||
base_url=self.host,
|
base_url=self.host,
|
||||||
headers={"Authorization": f"Token {self.api_key}"},
|
headers={"Authorization": f"Token {self.api_key}"},
|
||||||
|
timeout=60,
|
||||||
)
|
)
|
||||||
self._validate_api_key()
|
self._validate_api_key()
|
||||||
capture_client_event("client.init", self)
|
capture_client_event("client.init", self)
|
||||||
|
|
||||||
def _validate_api_key(self):
|
def _validate_api_key(self):
|
||||||
if not self.api_key:
|
"""Validate the API key by making a test request."""
|
||||||
warnings.warn("API Key not provided. Please provide an API Key.")
|
try:
|
||||||
response = self.client.get("/memories/", params={"user_id": "test"})
|
response = self.client.get("/memories/", params={"user_id": "test"})
|
||||||
if response.status_code != 200:
|
response.raise_for_status()
|
||||||
|
except httpx.HTTPStatusError:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Invalid API Key. Please get a valid API Key from https://app.mem0.ai"
|
"Invalid API Key. Please get a valid API Key from https://app.mem0.ai"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@api_error_handler
|
||||||
def add(
|
def add(
|
||||||
self,
|
self, messages: Union[str, List[Dict[str, str]]], **kwargs
|
||||||
data: str,
|
|
||||||
user_id: Optional[str] = None,
|
|
||||||
agent_id: Optional[str] = None,
|
|
||||||
session_id: Optional[str] = None,
|
|
||||||
metadata: Optional[Dict[str, Any]] = None,
|
|
||||||
filters: Optional[Dict[str, Any]] = None,
|
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
"""
|
"""Add a new memory.
|
||||||
Create a new memory.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
data (str): The data to be stored in the memory.
|
messages: Either a string message or a list of message dictionaries.
|
||||||
user_id (Optional[str]): User ID to save the memory specific to a user. Defaults to None.
|
**kwargs: Additional parameters such as user_id, agent_id, session_id, metadata, filters.
|
||||||
agent_id (Optional[str]): Agent ID for agent-specific memory. Defaults to None.
|
|
||||||
session_id (Optional[str]): Run ID to save memory for a specific session. Defaults to None.
|
|
||||||
metadata (Optional[Dict[str, Any]]): Metadata to be saved with the memory. Defaults to None.
|
|
||||||
filters (Optional[Dict[str, Any]]): Filters to apply to the memory. Defaults to None.
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict[str, Any]: The response from the server.
|
A dictionary containing the API response.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
APIError: If the API request fails.
|
||||||
"""
|
"""
|
||||||
|
payload = self._prepare_payload(messages, kwargs)
|
||||||
|
response = self.client.post("/memories/", json=payload)
|
||||||
|
response.raise_for_status()
|
||||||
capture_client_event("client.add", self)
|
capture_client_event("client.add", self)
|
||||||
payload = {"text": data}
|
|
||||||
if metadata:
|
|
||||||
payload["metadata"] = metadata
|
|
||||||
if filters:
|
|
||||||
payload["filters"] = filters
|
|
||||||
if user_id:
|
|
||||||
payload["user_id"] = user_id
|
|
||||||
if agent_id:
|
|
||||||
payload["agent_id"] = agent_id
|
|
||||||
if session_id:
|
|
||||||
payload["run_id"] = session_id
|
|
||||||
|
|
||||||
response = self.client.post("/memories/", json=payload, timeout=60)
|
|
||||||
if response.status_code != 200:
|
|
||||||
logger.error(response.json())
|
|
||||||
raise ValueError(f"Failed to add memory. Response: {response.json()}")
|
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
|
@api_error_handler
|
||||||
def get(self, memory_id: str) -> Dict[str, Any]:
|
def get(self, memory_id: str) -> Dict[str, Any]:
|
||||||
"""
|
"""Retrieve a specific memory by ID.
|
||||||
Get a memory by ID.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
memory_id (str): Memory ID.
|
memory_id: The ID of the memory to retrieve.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict[str, Any]: The memory data.
|
A dictionary containing the memory data.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
APIError: If the API request fails.
|
||||||
"""
|
"""
|
||||||
capture_client_event("client.get", self)
|
|
||||||
response = self.client.get(f"/memories/{memory_id}/")
|
response = self.client.get(f"/memories/{memory_id}/")
|
||||||
|
response.raise_for_status()
|
||||||
|
capture_client_event("client.get", self)
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
def get_all(
|
@api_error_handler
|
||||||
self,
|
def get_all(self, **kwargs) -> Dict[str, Any]:
|
||||||
user_id: Optional[str] = None,
|
"""Retrieve all memories, with optional filtering.
|
||||||
agent_id: Optional[str] = None,
|
|
||||||
session_id: Optional[str] = None,
|
|
||||||
limit: int = 100,
|
|
||||||
) -> Dict[str, Any]:
|
|
||||||
"""
|
|
||||||
Get all memories.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
user_id (Optional[str]): User ID to filter memories. Defaults to None.
|
**kwargs: Optional parameters for filtering (user_id, agent_id, session_id, limit).
|
||||||
agent_id (Optional[str]): Agent ID to filter memories. Defaults to None.
|
|
||||||
session_id (Optional[str]): Run ID to filter memories. Defaults to None.
|
|
||||||
limit (int): Number of memories to return. Defaults to 100.
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict[str, Any]: The list of memories.
|
A dictionary containing the list of memories.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
APIError: If the API request fails.
|
||||||
"""
|
"""
|
||||||
params = {
|
params = self._prepare_params(kwargs)
|
||||||
"user_id": user_id,
|
response = self.client.get("/memories/", params=params)
|
||||||
"agent_id": agent_id,
|
response.raise_for_status()
|
||||||
"run_id": session_id,
|
|
||||||
"limit": limit,
|
|
||||||
}
|
|
||||||
response = self.client.get(
|
|
||||||
"/memories/", params={k: v for k, v in params.items() if v is not None}
|
|
||||||
)
|
|
||||||
capture_client_event(
|
capture_client_event(
|
||||||
"client.get_all", self, {"filters": len(params), "limit": limit}
|
"client.get_all",
|
||||||
|
self,
|
||||||
|
{"filters": len(params), "limit": kwargs.get("limit", 100)},
|
||||||
)
|
)
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
def search(
|
@api_error_handler
|
||||||
self,
|
def search(self, query: str, **kwargs) -> Dict[str, Any]:
|
||||||
query: str,
|
"""Search memories based on a query.
|
||||||
user_id: Optional[str] = None,
|
|
||||||
agent_id: Optional[str] = None,
|
|
||||||
session_id: Optional[str] = None,
|
|
||||||
limit: int = 100,
|
|
||||||
filters: Optional[Dict[str, Any]] = None,
|
|
||||||
) -> Dict[str, Any]:
|
|
||||||
"""
|
|
||||||
Search memories.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
query (str): Query to search for in the memories.
|
query: The search query string.
|
||||||
user_id (Optional[str]): User ID to filter memories. Defaults to None.
|
**kwargs: Additional parameters such as user_id, agent_id, session_id, limit, filters.
|
||||||
agent_id (Optional[str]): Agent ID to filter memories. Defaults to None.
|
|
||||||
session_id (Optional[str]): Run ID to filter memories. Defaults to None.
|
|
||||||
limit (int): Number of memories to return. Defaults to 100.
|
|
||||||
filters (Optional[Dict[str, Any]]): Filters to apply to the search. Defaults to None.
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict[str, Any]: The search results.
|
A dictionary containing the search results.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
APIError: If the API request fails.
|
||||||
"""
|
"""
|
||||||
payload = {
|
payload = {"query": query}
|
||||||
"text": query,
|
payload.update({k: v for k, v in kwargs.items() if v is not None})
|
||||||
"limit": limit,
|
|
||||||
"filters": filters,
|
|
||||||
"user_id": user_id,
|
|
||||||
"agent_id": agent_id,
|
|
||||||
"run_id": session_id,
|
|
||||||
}
|
|
||||||
response = self.client.post("/memories/search/", json=payload)
|
response = self.client.post("/memories/search/", json=payload)
|
||||||
capture_client_event("client.search", self, {"limit": limit})
|
response.raise_for_status()
|
||||||
return response.json()
|
capture_client_event("client.search", self, {"limit": kwargs.get("limit", 100)})
|
||||||
|
|
||||||
def update(self, memory_id: str, data: str) -> Dict[str, Any]:
|
|
||||||
"""
|
|
||||||
Update a memory by ID.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
memory_id (str): Memory ID.
|
|
||||||
data (str): Data to update in the memory.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Dict[str, Any]: The response from the server.
|
|
||||||
"""
|
|
||||||
capture_client_event("client.update", self)
|
|
||||||
response = self.client.put(f"/memories/{memory_id}/", json={"text": data})
|
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
|
@api_error_handler
|
||||||
def delete(self, memory_id: str) -> Dict[str, Any]:
|
def delete(self, memory_id: str) -> Dict[str, Any]:
|
||||||
"""
|
"""Delete a specific memory by ID.
|
||||||
Delete a memory by ID.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
memory_id (str): Memory ID.
|
memory_id: The ID of the memory to delete.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict[str, Any]: The response from the server.
|
A dictionary containing the API response.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
APIError: If the API request fails.
|
||||||
"""
|
"""
|
||||||
capture_client_event("client.delete", self)
|
|
||||||
response = self.client.delete(f"/memories/{memory_id}/")
|
response = self.client.delete(f"/memories/{memory_id}/")
|
||||||
|
response.raise_for_status()
|
||||||
|
capture_client_event("client.delete", self)
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
def delete_all(
|
@api_error_handler
|
||||||
self,
|
def delete_all(self, **kwargs) -> Dict[str, Any]:
|
||||||
user_id: Optional[str] = None,
|
"""Delete all memories, with optional filtering.
|
||||||
agent_id: Optional[str] = None,
|
|
||||||
session_id: Optional[str] = None,
|
|
||||||
) -> Dict[str, Any]:
|
|
||||||
"""
|
|
||||||
Delete all memories.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
user_id (Optional[str]): User ID to filter memories. Defaults to None.
|
**kwargs: Optional parameters for filtering (user_id, agent_id, session_id).
|
||||||
agent_id (Optional[str]): Agent ID to filter memories. Defaults to None.
|
|
||||||
session_id (Optional[str]): Run ID to filter memories. Defaults to None.
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict[str, Any]: The response from the server.
|
A dictionary containing the API response.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
APIError: If the API request fails.
|
||||||
"""
|
"""
|
||||||
params = {"user_id": user_id, "agent_id": agent_id, "run_id": session_id}
|
params = self._prepare_params(kwargs)
|
||||||
response = self.client.delete(
|
response = self.client.delete("/memories/", params=params)
|
||||||
"/memories/", params={k: v for k, v in params.items() if v is not None}
|
response.raise_for_status()
|
||||||
)
|
|
||||||
capture_client_event("client.delete_all", self, {"params": len(params)})
|
capture_client_event("client.delete_all", self, {"params": len(params)})
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
|
@api_error_handler
|
||||||
def history(self, memory_id: str) -> Dict[str, Any]:
|
def history(self, memory_id: str) -> Dict[str, Any]:
|
||||||
"""
|
"""Retrieve the history of a specific memory.
|
||||||
Get history of a memory by ID.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
memory_id (str): Memory ID.
|
memory_id: The ID of the memory to retrieve history for.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dict[str, Any]: The memory history.
|
A dictionary containing the memory history.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
APIError: If the API request fails.
|
||||||
"""
|
"""
|
||||||
response = self.client.get(f"/memories/{memory_id}/history/")
|
response = self.client.get(f"/memories/{memory_id}/history/")
|
||||||
|
response.raise_for_status()
|
||||||
capture_client_event("client.history", self)
|
capture_client_event("client.history", self)
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
"""
|
"""Reset the client. (Not implemented)
|
||||||
Reset the client. (Not implemented yet)
|
|
||||||
|
Raises:
|
||||||
|
NotImplementedError: This method is not implemented yet.
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError("Reset is not implemented yet")
|
raise NotImplementedError("Reset is not implemented yet")
|
||||||
|
|
||||||
def chat(self):
|
def chat(self):
|
||||||
"""
|
"""Start a chat with the Mem0 AI. (Not implemented)
|
||||||
Start a chat with the Mem0 AI. (Not implemented yet)
|
|
||||||
|
Raises:
|
||||||
|
NotImplementedError: This method is not implemented yet.
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError("Chat is not implemented yet")
|
raise NotImplementedError("Chat is not implemented yet")
|
||||||
|
|
||||||
|
def _prepare_payload(
|
||||||
|
self, messages: Union[str, List[Dict[str, str]], None], kwargs: Dict[str, Any]
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""Prepare the payload for API requests.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
messages: The messages to include in the payload.
|
||||||
|
kwargs: Additional keyword arguments to include in the payload.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A dictionary containing the prepared payload.
|
||||||
|
"""
|
||||||
|
payload = {}
|
||||||
|
if isinstance(messages, str):
|
||||||
|
payload["messages"] = [{"role": "user", "content": messages}]
|
||||||
|
elif isinstance(messages, list):
|
||||||
|
payload["messages"] = messages
|
||||||
|
payload.update({k: v for k, v in kwargs.items() if v is not None})
|
||||||
|
return payload
|
||||||
|
|
||||||
|
def _prepare_params(self, kwargs: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
|
"""Prepare query parameters for API requests.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
kwargs: Keyword arguments to include in the parameters.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A dictionary containing the prepared parameters.
|
||||||
|
"""
|
||||||
|
return {k: v for k, v in kwargs.items() if v is not None}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = "mem0ai"
|
name = "mem0ai"
|
||||||
version = "0.0.8"
|
version = "0.0.9"
|
||||||
description = "Long-term memory for AI Agents"
|
description = "Long-term memory for AI Agents"
|
||||||
authors = ["Deshraj Yadav <deshraj@mem0.ai>", "Taranjeet Singh <taranjeet@mem0.ai>"]
|
authors = ["Deshraj Yadav <deshraj@mem0.ai>", "Taranjeet Singh <taranjeet@mem0.ai>"]
|
||||||
exclude = [
|
exclude = [
|
||||||
|
|||||||
Reference in New Issue
Block a user