共计 3952 个字符,预计需要花费 10 分钟才能阅读完成。
OpenClaw 增强型 LanceDB 长期记忆插件完全指南
混合检索(Vector + BM25)· 跨编码器 Rerank · 多 Scope 隔离 · 管理 CLI
本指南基于:win4r/memory-lancedb-pro
📊 功能对比
| 功能 | 内置 memory-lancedb | memory-lancedb-pro |
|---|---|---|
| 向量搜索 | ✅ | ✅ |
| BM25 全文检索 | ❌ | ✅ |
| 混合融合(Vector + BM25) | ❌ | ✅ |
| 跨编码器 Rerank(Jina) | ❌ | ✅ |
| 时效性加成 | ❌ | ✅ |
| 多 Scope 隔离 | ❌ | ✅ |
| 噪声过滤 | ❌ | ✅ |
| 自适应检索 | ❌ | ✅ |
| 管理 CLI | ❌ | ✅ |
| Session 记忆 | ❌ | ✅ |
| Task-aware Embedding | ❌ | ✅ |
🏗️ 架构设计
┌─────────────────────────────────────────┐
│ index.ts (入口) │
│ 插件注册 · 配置解析 · 生命周期钩子 │
│ 自动捕获 / 回忆 │
└────────┬──────────┬──────────┬───────────┘
│ │ │
┌────▼───┐ ┌────▼───┐ ┌───▼────┐
│ store │ │embedder│ │retriever│
│ .ts │ │ .ts │ │ .ts │
└────────┘ └────────┘ └────────┘
核心组件
| 文件 | 用途 |
|---|---|
index.ts |
插件入口,注册钩子(自动回忆 / 捕获) |
store.ts |
LanceDB 存储层,向量 / 全文搜索 |
embedder.ts |
Embedding 抽象层,支持任意 OpenAI 兼容 API |
retriever.ts |
混合检索引擎,RRF 融合 + Rerank |
scopes.ts |
多 Scope 访问控制 |
tools.ts |
Agent 工具定义 |
cli.ts |
CLI 命令实现 |
🔧 核心功能详解
1. 混合检索(Hybrid Retrieval)
流程: 向量搜索 + BM25 → RRF 融合 → Jina Rerank → 时效加成
| 阶段 | 公式 | 效果 |
|---|---|---|
| 时效加成 | exp(-ageDays / halfLife) * weight |
新记忆分数更高(半衰期 14 天) |
| 重要性加权 | score *= (0.7 + 0.3 * importance) |
重要记忆获得加成 |
| 长度归一化 | score *= 1 / (1 + 0.5 * log2(len/anchor)) |
防止长条目霸占查询 |
| 时间衰减 | score *= 0.5 + 0.5 * exp(-ageDays / halfLife) |
旧条目逐渐降权 |
| MMR 多样性 | cosine > 0.85 → 降级 |
防止近似重复 |
2. 多 Scope 隔离
内置 Scope 模式:
global– 共享知识库agent:<id>– Agent 私有custom:<name>– 自定义project:<id>– 项目级user:<id>– 用户级
3. 自适应检索
自动跳过: 问候、命令、简单确认、emoji
强制检索: 含 ” 记住 ”、” 之前 ”、” 上次 ” 等关键词
4. 噪声过滤
过滤内容:
- Agent 拒绝回复(”I don’t have information”)
- Meta 问题(”do you remember”)
- 寒暄(”hi”、”hello”、”HEARTBEAT”)
📦 安装指南
方案 A:克隆到 workspace(推荐)
# 1. 进入 OpenClaw workspace
cd ~/.openclaw/workspace
# 2. 克隆插件
git clone https://github.com/win4r/memory-lancedb-pro.git plugins/memory-lancedb-pro
# 3. 安装依赖
cd plugins/memory-lancedb-pro
npm install
方案 B:绝对路径
# 克隆到任意位置
git clone https://github.com/win4r/memory-lancedb-pro.git /path/to/memory-lancedb-pro
cd /path/to/memory-lancedb-pro
npm install
⚙️ 配置示例
{
"plugins": {
"load": {"paths": ["plugins/memory-lancedb-pro"]
},
"entries": {
"memory-lancedb-pro": {
"enabled": true,
"config": {
"embedding": {"apiKey": "${JINA_API_KEY}",
"model": "jina-embeddings-v5-text-small",
"baseURL": "https://api.jina.ai/v1",
"dimensions": 1024,
"taskQuery": "retrieval.query",
"taskPassage": "retrieval.passage"
},
"dbPath": "~/.openclaw/memory/lancedb-pro",
"autoCapture": true,
"autoRecall": false,
"retrieval": {
"mode": "hybrid",
"vectorWeight": 0.7,
"bm25Weight": 0.3,
"minScore": 0.3,
"rerank": "cross-encoder",
"rerankApiKey": "${JINA_API_KEY}",
"rerankModel": "jina-reranker-v3"
}
}
}
},
"slots": {"memory": "memory-lancedb-pro"}
}
}
🔑 API Key 配置
支持的 Embedding 提供商
| 提供商 | 模型 | Base URL | 维度 |
|---|---|---|---|
| Jina(推荐) | jina-embeddings-v5 | https://api.jina.ai/v1 | 1024 |
| OpenAI | text-embedding-3-small | https://api.openai.com/v1 | 1536 |
| Google Gemini | gemini-embedding-001 | Google API | 3072 |
| Ollama(本地) | nomic-embed-text | http://localhost:11434 | 可变 |
⚠️ 重要: 使用 ${JINA_API_KEY} 环境变量时,确保 Gateway 进程能访问该变量。
🛠️ CLI 命令
# 列出记忆
openclaw memory-pro list [--scope global] [--limit 20]
# 搜索记忆
openclaw memory-pro search "query" [--scope global] [--limit 10]
# 查看统计
openclaw memory-pro stats [--scope global]
# 删除记忆
openclaw memory-pro delete <id>
# 批量删除
openclaw memory-pro delete-bulk --scope global [--before 2025-01-01]
# 导出 / 导入
openclaw memory-pro export [--output memories.json]
openclaw memory-pro import memories.json [--scope global]
# 重新生成 Embedding
openclaw memory-pro reembed --source-db /path/to/old-db
# 从内置版迁移
openclaw memory-pro migrate check [--source /path]
openclaw memory-pro migrate run [--source /path]
🧠 高级功能
1. Session 记忆蒸馏
使用 /new 命令触发非阻塞沉淀管线:
- Hook 投递 task.json(毫秒级)
- systemd worker 异步处理
- Gemini Map-Reduce 抽取高价值记忆
- 写入 LanceDB Pro
2. 增量蒸馏(Cron 自动)
# 创建蒸馏 Agent
openclaw agents add memory-distiller \
--workspace ~/.openclaw/workspace-memory-distiller
# 初始化
python3 scripts/jsonl_distill.py init
# 添加定时任务(每小时)openclaw cron add \
--agent memory-distiller \
--name "jsonl-memory-distill" \
--cron "0 * * * *" \
--message "run jsonl memory distill"
3. 记忆强化机制
经常被召回的记忆衰减更慢:
reinforcementFactor(0-2,默认 0.5)— 设为 0 关闭maxHalfLifeMultiplier(1-10,默认 3)— 半衰期上限
⚠️ 常见问题
Q: 周期性显示长期记忆怎么办?
方案 A(推荐): 关闭自动召回
"autoRecall": false
方案 B: 在 system prompt 添加
请勿在回复中展示或引用任何 <relevant-memories> 区块。
Q: 如何确认插件已加载?
openclaw plugins list
openclaw plugins info memory-lancedb-pro
openclaw config get plugins.slots.memory
Q: 多个 Agent 如何隔离记忆?
配置 scopes.agentAccess:
"scopes": {
"agentAccess": {"discord-bot": ["global", "agent:discord-bot"],
"main": ["global", "agent:main"]
}
}
📚 参考资源
- GitHub 仓库:win4r/memory-lancedb-pro
- OpenClaw 官方:openclaw/openclaw
- LanceDB 文档:lancedb.com
- 视频教程:YouTube | Bilibili
最后更新:2026-03-06
正文完