以下所有内容仅供学习使用; 好项目大家一起分享; 项目来源于github开源项目; 他用请注明出处,尊重原创,尊重开源。
1 项目地址
2 简单介绍
2.1 Mem0 是什么?
Mem0 是专为AI 代理设计的内存层。它充当持久内存层,代理可以使用它来执行以下操作:
- 回忆过去相关的互动;
- 存储重要的用户偏好和事实背景;
- 从成功和失败中学习。
它为 AI 代理提供内存,使其能够在交互过程中记忆、学习和进化。Mem0 可轻松集成到代理堆栈中,并可从原型系统扩展到生产系统。
2.2 内存在代理堆栈中的位置
- Mem0 与检索器、规划器和 LLM 并存;
- 与基于检索的系统(例如 RAG)不同,Mem0 会追踪过去的交互,存储长期知识,并改进代理的行为。
2.3 与基于检索的系统区别
能力 | 上下文窗口 | Mem0 内存 |
---|---|---|
成本 | 随着输入大小而增长 | 优化(只优化重要部分) |
保留 | 暂时的 | 持久的 |
记起 | 令牌 | 相关性+基于意图 |
个性化 | 无 | 深度、不断发展 |
行为 | 反应式 | 自适应 |
2.4 Mem0 中的内存类型
Mem0 支持不同类型的内存来模仿存储信息的方式:
- 工作记忆:短期会话意识;
- 事实记忆:长期结构化知识(例如偏好、设置) ;
- 情景记忆:记录过去的具体对话;
- 语义记忆:随着时间的推移建立一般知识。
2.5 核心能力
- 减少令牌使用并加快响应速度:查找时间低于 50 毫秒 ;
- 语义记忆:程序性、情景性和事实支持 ;
- 多模式支持:处理文本和图像;
- 图形内存:跨会话连接洞察和实体 ;
- 自主选择方式托管:托管服务或自托管版本。
3 Mem0 平台(托管解决方案)
3.1 安装Mem0
- 使用pip安装:
python
pip install mem0ai
- 使用npm安装
python
npm install mem0ai

3.2 获取API key
- 进入到memo,获取API key,如图:
3.3 添加记忆
- 实例化客户端:
python
import os
from mem0 import MemoryClient
os.environ["MEM0_API_KEY"] = "your-api-key"
client = MemoryClient()
- 添加记忆
python
messages = [
{"role": "user", "content": "Thinking of making a sandwich. What do you recommend?"},
{"role": "assistant", "content": "How about adding some cheese for extra flavor?"},
{"role": "user", "content": "Actually, I don't like cheese."},
{"role": "assistant", "content": "I'll remember that you don't like cheese for future recommendations."}
]
client.add(messages, user_id="alex")
3.4 恢复记忆
- 搜索相关记忆
python
# Example showing location and preference-aware recommendations
query = "I'm craving some pizza. Any recommendations?"
filters = {
"AND": [
{
"user_id": "alex"
}
]
}
client.search(query, version="v2", filters=filters)
- 获取某个用户的所有记忆:
python
filters = {
"AND": [
{
"user_id": "alex"
}
]
}
all_memories = client.get_all(version="v2", filters=filters, page=1, page_size=50)
4 Mem0 开源
4.1 安装Mem0开源
- pip安装:
python
pip install mem0ai
- npm安装:
python
npm install mem0ai
4.2 添加记忆
- 实例化客户端:
python
from mem0 import Memory
m = Memory()
- 添加记忆:
python
# For a user
messages = [
{
"role": "user",
"content": "I like to drink coffee in the morning and go for a walk"
}
]
result = m.add(messages, user_id="alice", metadata={"category": "preferences"})
4.3 恢复记忆
- 搜索相关记忆
python
related_memories = m.search("Should I drink coffee or tea?", user_id="alice")