让AI Agent拥有本地永久记忆:零费用且中文友好的解决方案
让你的 AI Agent 拥有永不遗忘的长期记忆。一行命令接入,零 API 费用,数据 100% 本地存储。
前言:AI Agent 的"金鱼记忆"之痛
用过 Claude Code、Cursor、Cline、Hermes 等 AI 编程助手的同学一定有过这种体验:
- 上次告诉它"用 uv 管理依赖",这次它又开始用 pip
- 昨天讨论好的架构方案,今天它全忘了
- 换个对话窗口,之前积累的上下文全清零
- 多个 AI 工具之间各管各的,记忆无法共享
这些问题的根源是:大多数 AI Agent 没有可靠的长期记忆系统。
我尝试过 Mem0,但它是 SaaS 服务,数据要上传到云端,按 token 计费,对隐私敏感的项目不友好。也试过一些本地方案,但中文分词效果差,搜索结果不理想。
于是我自己做了一个:Agent Memory Lite。
它是什么?
Agent Memory Lite 是一个轻量级、中文友好的 AI Agent 记忆增强系统。核心特点:
- 零 API 费用 — 本地 ONNX 推理,不调任何外部 API
- 数据 100% 本地 — SQLite 单文件存储,复制即备份
- 中文分词精准 — jieba 定制分词 + SQLite FTS5 全文搜索
- 两种接入方式 — Hermes 插件深度集成(自动同步),其他 Agent 通过 MCP 协议接入
目前 GitHub 和 Gitee 双平台开源,版本 v0.6.0。
架构设计
整体架构分为三层:
┌─────────────────────────────────────────────────┐
│ Agent 层 │
│ Hermes Agent │ Claude Code │ Cursor/Cline │
│ (插件深度集成) │ (MCP 接入) │ (MCP 接入) │
└────────┬───────────┬──────────────┬──────────────┘
│ │ │
进程内调用 MCP 协议 MCP 协议
│ │ │
┌────────▼───────────▼──────────────▼──────────────┐
│ Agent Memory Lite │
│ ┌──────────┐ ┌──────────┐ ┌──────────────────┐ │
│ │ jieba │ │ ONNX │ │ SQLite FTS5 │ │
│ │ 中文分词 │ │ 语义嵌入 │ │ 全文搜索 │ │
│ └──────────┘ └──────────┘ └──────────────────┘ │
└────────────────────┬─────────────────────────────┘
│
~/.agent-memory/
memory.db
Hermes Agent:插件深度集成(推荐)
对于 Hermes Agent,我做了一个 Memory Provider 适配器插件,实现了进程内直接调用:
- 不走 MCP 协议,零 IPC 开销
- 通过
on_memory_write钩子,Hermes 内置 memory 工具的每次写入自动同步到 AML 数据库 - 工具不重复,只暴露
memory_store、memory_search、memory_list三个工具
数据流:
用户输入 → Hermes Agent → memory_store 工具
→ AgentMemoryLiteProvider.handle_tool_call()
→ MemoryEngine.store()(进程内直接调用)
→ SQLite WAL
→ on_memory_write() 钩子触发
→ 镜像写入 ~/.agent-memory/memory.db
关键代码(适配器核心逻辑):
class AgentMemoryLiteProvider(MemoryProvider):
def __init__(self, config: MemoryProviderConfig):
self._engine = create_engine() # 直接 import,不走 IPC
self._skip_writes = False
async def handle_tool_call(self, tool_name, arguments, context):
if tool_name == "memory_store":
memory = self._engine.store(
content=arguments["content"],
category=arguments.get("category", "general"),
)
return {"status": "ok", "id": memory.id}
async def on_memory_write(self, memory, context):
"""内置 memory 工具写入时的钩子 —— 自动同步"""
if self._skip_writes:
return
self._engine.store(
content=memory.content,
category=memory.category,
metadata={"source": "hermes_builtin"},
)
其他 Agent:MCP 协议接入
对于 Claude Code、Cursor、Cline 等支持 MCP 的 Agent,通过标准 MCP Server 接入:
# 一行命令启动 MCP Server
uv run python -m agent_memory_lite.entrypoints.mcp_server
MCP Server 提供 9 个工具:
| 工具名 | 说明 |
|---|---|
store_memory |
存储一条记忆(支持去重) |
search_memory |
搜索记忆(keyword/semantic/hybrid) |
get_memory |
获取指定记忆 |
update_memory |
更新记忆 |
delete_memory |
删除记忆 |
delete_memories_by_category |
按分类批量删除 |
list_memories |
列出记忆 |
memory_stats |
查看统计 |
reindex_memories |
重建 FTS5 分词索引 |
中文搜索:为什么我要自己做分词?
这是整个项目最值得说的技术点。
SQLite FTS5 默认的 unicode61 tokenizer 对中文完全无效 —— 它按空格分词,而中文没有空格。搜"用户"返回 0 条结果,搜"记忆"也是 0 条。
我用 jieba 做了一套 FTS5 自定义分词方案:
写入时:jieba 分词 → 拼接为 FTS5 可识别的 token 字符串
def tokenize_for_fts5(text: str) -> str:
"""jieba 分词后拼接为 FTS5 token 字符串"""
tokens = jieba.cut_for_search(text)
return " ".join(tokens)
比如"用户偏好使用 Docker 部署"会被分词为:
用户 偏好 使用 Docker 部署
存储到 FTS5 虚拟表时,这些 token 会被正确索引。查询时用同一套分词器,保证 token 完全对齐。
搜索时:三种模式自动选择
keyword— FTS5 关键词匹配,精确查找semantic— ONNX 本地向量语义搜索,模糊查找hybrid— 关键词 + 语义加权排序,兼顾精确和模糊
安装有多简单?
方式一:Hermes Agent 插件安装(推荐)
# 1. 安装依赖到 Hermes venv
uv pip install --python ~/.hermes/hermes-agent/venv/bin/python jieba tokenizers
# 2. 复制适配器插件
cp -r ~/Desktop/Agent-Memory-Lite/hermes_plugin/ ~/.hermes/plugins/agent-memory-lite/
# 3. 修改 config.yaml
# memory.provider: agent-memory-lite
# 4. 重启 Hermes
hermes gateway restart
搞定。Hermes 内置的 memory 工具写入会自动同步到 AML 数据库。
方式二:MCP Server 接入(其他 Agent)
git clone https://gitee.com/pimou/Agent-Memory-Lite.git ~/Desktop/Agent-Memory-Lite
cd ~/Desktop/Agent-Memory-Lite
uv sync
# 在 config.yaml 中添加 MCP Server 配置即可
方式三:CLI 直接使用
# 存一条记忆
uv run aml store "用户偏好使用 Docker 部署" -c user_pref
# 搜索
uv run aml search "Docker"
# 查看统计
uv run aml stats
30 秒快速体验
git clone https://gitee.com/pimou/Agent-Memory-Lite.git
cd Agent-Memory-Lite
uv sync
# 存一条记忆
uv run aml store "用户偏好使用 Docker 部署" -c user_pref
# 搜索
uv run aml search "Docker"
# 输出: #1 user_pref
# 用户偏好使用 Docker 部署
和 Mem0 对比
| 对比维度 | Agent Memory Lite | Mem0 | 内置记忆 |
|---|---|---|---|
| 中文分词 | ✅ jieba 定制 | 默认分词 | 默认分词 |
| 本地部署 | ✅ SQLite 单文件 | ❌ 需 API | ✅ 绑定框架 |
| 嵌入模型 | ✅ ONNX 本地 ~24MB | OpenAI API | 无 |
| MCP 协议 | ✅ 标准 MCP Server | ❌ | ❌ |
| 跨 Agent 共享 | ✅ 一份 .db 通用 | ❌ | ❌ |
| 数据库可备份 | ✅ 单文件复制即可 | ❌ | ❌ |
| 费用 | 💰 零 API 费用 | 💰💸 按 token 计费 | 💰 零 |
遇到的坑
做这个项目踩了不少坑,分享几个关键的:
1. FTS5 中文搜索返回 0 条
原因是 unicode61 tokenizer 不做 CJK 分词。解决:用 jieba 分词 + 自定义 tokenchars 配置。
2. 适配器 is_available() 静默失败
Hermes 插件的 is_available() 内部 import jieba 时,如果 hermes venv 没装 jieba,会抛 ImportError 被捕获后静默返回 False。调试时要加上日志才能发现。
3. SQLite 并发锁死
MCP Server 和适配器同时写入时,需要开启 WAL 模式。默认的 journal_mode 是 DELETE,会锁死。
项目地址
- GitHub:https://github.com/P1M0U/Agent-Memory-Lite
- Gitee:https://gitee.com/pimou/Agent-Memory-Lite (国内更快)
写在最后
这个项目的核心理念是:AI Agent 的记忆应该是可移植的、零成本的、本地化的。
一份 SQLite 数据库,多个 Agent 共享。不依赖任何云服务,数据永远在你本地。Hermes 用户可以享受深度集成的自动同步体验,其他 Agent 用户通过 MCP 协议也能无缝接入。
如果觉得有用,欢迎点个 Star ⭐
有问题欢迎提 Issue,也欢迎贡献代码。
作者: P1M0U
邮箱: p1m0u@foxmail.com
更多推荐
所有评论(0)