🧠 Mem0:55k Stars 的 AI Agent 记忆层,让你的 LLM 不再金鱼记忆
项目地址:https://github.com/mem0ai/mem0 | ⭐ 55.3k Stars | 🛠 Python | 作者:mem0ai
老实说,现在很多 AI Agent 看着挺聪明,但聊几句就发现——它根本不记得你上一轮说了啥。每次对话都是全新的开始,跟金鱼似的。
Mem0("mem-zero")就是来解决这个问题的。它给 AI Agent 装了一个长期记忆层,能记住用户的偏好、习惯,甚至跨 session 持续学习。最新的 v3 算法在 LoCoMo 基准上拿了 91.6 分(比上代 +20 分),LongMemEval 93.4 分,关键是每次检索只用 ~7K tokens、1 秒延迟。
一行命令装上去
pip install mem0ai
如果要 BM25 关键词匹配和实体抽取,加个 NLP 依赖:
pip install mem0ai[nlp]
python -m spacy download en_core_web_sm
想直接在终端里管理记忆也行:
npm install -g @mem0/cli
# 或者 pip install mem0-cli
mem0 init
mem0 add "Prefers dark mode and vim keybindings" --user-id alice
mem0 search "What does Alice prefer?" --user-id alice
给 LLM 装上记忆,代码就这几行
from openai import OpenAI
from mem0 import Memory
client = OpenAI()
memory = Memory()
def chat_with_memories(message: str, user_id: str = "default_user") -> str:
relevant = memory.search(query=message, filters={"user_id": user_id}, top_k=3)
memories_str = "\n".join(f"- {entry['memory']}" for entry in relevant["results"])
system_prompt = f"You are a helpful AI. Answer based on query and memories.\nUser Memories:\n{memories_str}"
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "system", "content": system_prompt},
{"role": "user", "content": message}]
)
answer = response.choices[0].message.content
memory.add(
[{"role": "user", "content": message},
{"role": "assistant", "content": answer}],
user_id=user_id
)
return answer
最骚的操作是——每次对话结束时 memory.add() 自动把这次聊的内容存进去,下次 memory.search() 就能查到。Agent 越用越懂你。
自部署也简单
cd server && docker compose up -d
# 浏览器打开 http://localhost:3000
用 make bootstrap 一步到位:启动服务、创建管理员、生成 API key。
v3 新算法,值得升级
| 基准 | 旧版 | v3 | Tokens | 延迟 |
|------|------|----|--------|------|
| LoCoMo | 71.4 | 91.6 | 7.0K | 0.88s |
| LongMemEval | 67.8 | 93.4 | 6.8K | 1.09s |
| BEAM (1M) | — | 64.1 | 6.7K | 1.00s |
核心改进:单次 ADD-only 抽取(不更新不删除),实体链接跨记忆关联,语义+BM25+实体三重检索融合。
踩坑提醒
gpt-4o-mini 和 text-embedding-3-small,但支持切换其他 LLM 和 embedding 模型ADMIN_API_KEY 或 AUTH_DISABLED=true总结:
2. pip install mem0ai + 几行代码就能集成
3. v3 算法在长上下文记忆 recall 上比旧版提升了 20+ 分
4. 支持本地部署、CLI、以及 Claude Code/Codex 的 skills 集成
5. Apache 2.0 开源,YC S24 团队出品
English Version
🧠 Mem0: 55k Stars Memory Layer for AI Agents — Your LLM Doesn't Have to Be a Goldfish
Repo: https://github.com/mem0ai/mem0 | ⭐ 55.3k Stars | 🛠 Python
Let's be honest — most AI agents today are smart until you realize they forgot everything you said two messages ago. Each conversation is a fresh start, like talking to a goldfish.
Mem0 (pronounced "mem-zero") is a universal memory layer that gives AI agents long-term recall. It remembers user preferences, adapts over time, and learns across sessions. The v3 algorithm scores 91.6 on LoCoMo (+20 over the previous version), 93.4 on LongMemEval, while using only ~7K tokens and ~1s latency per retrieval.
One-line install
pip install mem0ai
With NLP support for BM25 keyword matching and entity extraction:
pip install mem0ai[nlp]
python -m spacy download en_core_web_sm
Or manage memories from your terminal:
npm install -g @mem0/cli
mem0 init
mem0 add "Prefers dark mode and vim keybindings" --user-id alice
mem0 search "What does Alice prefer?" --user-id alice
Memory in 15 lines of Python
from openai import OpenAI
from mem0 import Memory
client = OpenAI()
memory = Memory()
def chat_with_memories(message: str, user_id: str = "default_user") -> str:
relevant = memory.search(query=message, filters={"user_id": user_id}, top_k=3)
memories_str = "\n".join(f"- {entry['memory']}" for entry in relevant["results"])
# ... inject memories into prompt, call LLM, then store new memories
return answer
The trick: memory.add() automatically stores each conversation turn after the LLM responds. Next time memory.search() retrieves what's relevant. The more you use it, the smarter it gets.
Self-hosting
cd server && docker compose up -d
# Open http://localhost:3000
Or make bootstrap for one-command setup with admin creation and API key generation.
Key takeaways
2. pip install mem0ai + a few lines of Python and you're integrated
3. v3 algorithm delivers 20+ point improvements over previous version on long-context benchmarks
4. Supports local deployment, CLI, and skills for Claude Code/Codex
5. Apache 2.0 licensed, YC S24 team