openclaw-mem 是一个面向 AI 智能体的本地优先 RAG 记忆系统,无需 API 密钥,使用本地嵌入模型,可完全离线运行。
安装与初始化
安装命令:
pip install openclaw-mem
openclaw-mem init
初始化会创建如下工作区结构:
./memory/
├── core.md # 关键决策与经验教训
├── observations.md # 结构化观察记录
└── projects/ # Brain 目录(按项目的上下文)
.env # OPENCLAW_MEM_ROOT 配置
主要命令
openclaw-mem search "query":对记忆进行语义搜索openclaw-mem index --all:索引所有 markdown 文件openclaw-mem index --changed:增量索引(仅变更文件)openclaw-mem observe "text" --tag learning:记录一条结构化观察openclaw-mem auto-capture --since 6h:从会话记录中提取观察openclaw-mem auto-capture --since 6h --route-to-brain:将观察自动路由到 Brain 文件openclaw-mem brain-check:检查 Brain 文件是否存在注入模式openclaw-mem brain-check --fix:自动修复注入模式openclaw-mem archive --execute:归档旧记忆(三层:热/温/冷)
Brain 目录
按项目的持久上下文存放于 memory/projects/{name}.md:
# 创建项目 Brain
echo "# My Project Brain\n\n## Architecture\n- Stack: Python + FastAPI\n" > memory/projects/my-project.md
# 索引它
openclaw-mem index memory/projects/my-project.md
# 跨所有 Brain 搜索
openclaw-mem search "project architecture"
# 校验完整性
openclaw-mem brain-check
渐进式披露(两步搜索)
先返回摘要,再按需返回完整内容,以减少 token 使用:
# 第一步:获取摘要(索引视图)
openclaw-mem search "deployment" --index
# 返回:分块 ID + 一行摘要
# 第二步:获取特定分块的完整内容
openclaw-mem search --detail "chunk:0:abc123"
# 返回:完整分块文本
观察标签
用标签对观察进行分类,实现结构化知识捕获:
openclaw-mem observe "Redis cache TTL should be 1h" --tag decision
openclaw-mem observe "Always test with --dry-run first" --tag learning
openclaw-mem observe "Forgot to check edge case" --tag mistake
openclaw-mem observe "Service mesh pattern works well" --tag architecture
openclaw-mem observe "User prefers dark mode" --tag preference
openclaw-mem observe "Next: add WebSocket support" --tag next
环境变量
OPENCLAW_MEM_ROOT:工作区根目录,默认.OPENCLAW_MEM_DB_PATH:LanceDB 数据库路径,默认{root}/lance_dbOPENCLAW_MEM_TABLE:LanceDB 表名,默认memoryOPENCLAW_MEM_BACKEND:嵌入后端,可选 local、openai、ollama,默认localOPENCLAW_MEM_MODEL:嵌入模型名称,默认all-MiniLM-L6-v2OPENCLAW_MEM_ARCHIVE_DIR:归档目录,默认{root}/archiveOPENCLAW_MEM_ARCHIVE_DAYS:自动归档前的天数,默认30OPENCLAW_MEM_SESSION_DIR:会话记录目录,默认{root}/sessions
安全
- 注入防御:扫描 Brain 文件中的提示注入模式
- 清洗器:所有被索引内容都经过注入清洗器
- 本地优先:数据不离开本机,嵌入在本地运行