Mem0上线GitHub仅仅2天就已经达到13K🌟!
项目地址:https://github.com/mem0ai/mem0

基础用法

1. 安装

1
pip install mem0ai

2. 基础用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os
from mem0 import Memory

os.environ["OPENAI_API_KEY"] = "xxx"

# Initialize Mem0
m = Memory()

# Store a memory from any unstructured text
result = m.add("I am working on improving my tennis skills. Suggest some online courses.", user_id="alice", metadata={"category": "hobbies"})
print(result)
# Created memory: Improving her tennis skills. Looking for online suggestions.

# Retrieve memories
all_memories = m.get_all()
memory_id = all_memories[0]["id"] # get a memory_id
print(all_memories)

# Search memories
related_memories = m.search(query="What are Alice's hobbies?", user_id="alice")
print(related_memories)

# Update a memory
result = m.update(memory_id=memory_id, data="Likes to play tennis on weekends")
print(result)

# Get memory history
history = m.history(memory_id=memory_id)
print(history)

3. 初始化

1
2
from mem0 import Memory
m = Memory()

4. 记忆储存

1
2
3
4
5
6
7
8
9
10
11
12
13
result = m.add("I am working on improving my tennis skills. Suggest some online courses.", user_id="alice", metadata={"category": "hobbies"})
print(result)



# OutPut
[
{
'id': 'm1',
'event': 'add',
'data': 'I am working on improving my tennis skills. Suggest some online courses.'
}
]

5. 找回记忆

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Get all memories
all_memories = m.get_all()
print(all_memories)

# OutPut

[
{
'id': 'm1',
'text': 'Likes to play cricket on weekends',
'metadata': {
'data': 'Likes to play cricket on weekends',
'category': 'hobbies'
}
},
# ... other memories ...
]

6. 记忆搜索

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
related_memories = m.search(query="What are Alice's hobbies?", user_id="alice")
print(related_memories)

# output

[
{
'id': 'm1',
'text': 'Likes to play cricket on weekends',
'metadata': {
'data': 'Likes to play cricket on weekends',
'category': 'hobbies'
},
'score': 0.85 # Similarity score
},
# ... other related memories ...
]

7. 记忆更新

1
2
result = m.update(memory_id="m1", data="Likes to play tennis on weekends")
print(result)

8. 记忆删除

1
2
m.delete(memory_id="m1") # Delete a memory
m.delete_all(user_id="alice") # Delete all memories

特征:

  • Multi-Level Memory: User, Session, and AI Agent memory retention
  • Adaptive Personalization: Continuous improvement based on interactions
  • Developer-Friendly API: Simple integration into various applications
  • Cross-Platform Consistency: Uniform behavior across devices
  • Managed Service: Hassle-free hosted solution