系列: 个人知识管理系统 | 更新: 2026-04-07
在 AI Agent 开发过程中,我遇到了几个核心问题:
Andrej Karpathy 提出的核心理念:
"让 AI 成为知识库的主动维护者,而不是被动查询者"
这启发我设计双层记忆架构。
双层记忆 = Wiki 层(人可读) + LanceDB 层(机可检索)
OpenClaw Agent(定时任务驱动) ↓ 调用工具 wiki_tools.py(统一接口层) ↓ 读写 ┌──────────────────┬──────────────────────┐ │ Wiki 层 │ LanceDB 层 │ │ (Markdown) │ (向量索引) │ │ 人可读 │ 机可检索 │ │ Obsidian 查看 │ text-embedding-v4 │ └──────────────────┴──────────────────────┘
| 维度 | Wiki 层 | LanceDB 层 |
|---|---|---|
| 格式 | Markdown | 向量 + 结构化字段 |
| 检索 | 目录浏览 | 语义搜索 |
| 受众 | 人类 | Agent |
| 更新 | 定时任务整合 | 实时写入 |
| 特点 | 可读性强 | 检索效率高 |
系列: 双层记忆架构 | 章节: 第2章 | 更新: 2026-04-07
Wiki 层是双层记忆架构的人类可读层:
wiki/ ├── index.md # 首页(自动生成) ├── daily/ # 每日记录 │ ├── 2026-04-07.md │ └── 2026-04-06.md ├── projects/ # 项目索引 │ ├── mijiamonitor.md │ ├── weatherpush.md │ └── clawvalue.md ├── decisions/ # 重要决策记录 │ ├── oss-signature.md │ └── harness-v3.md ├── lessons/ # 经验教训 │ ├── debugging-tricks.md │ └── api-errors.md └── config/ # 配置说明 ├── tools.md └── credentials.md
自动汇总最近活动、热门项目、重要决策:
markdown# Wiki 首页
## 最近更新
- 2026-04-07: OSS 签名问题解决
- 2026-04-07: 米家监控小时报修复
## 热门项目
- [[projects/mijiamonitor]] - 米家智能监控
- [[projects/weatherpush]] - 天气推送系统
## 重要决策
- [[decisions/oss-signature]] - OSS 签名 URL 生成
每日活动汇总,整合 OpenClaw 心跳日志:
markdown# 2026-04-07
## 🐱 米家监控
- 小时报定时任务修复
- 视频链接签名问题解决
## 📝 文档编写
- 双层记忆架构文档启动
- 第一至第三章构思完成
## ✅ 完成事项
- OSS 签名问题根因定位
- TOOLS.md / MEMORY.md 更新
项目核心信息、关键决策、进展追踪:
markdown# 米家智能监控
## 基本信息
- 路径: ~/Documents/mijiamonitor
- 后端: FastAPI (8001)
- 前端: Vue 3 (3000)
## 核心功能
- 定时抽帧(每分钟)
- AI 分析(qwen3.5-plus)
- 自动录制(30秒视频)
## 关键决策
- [[decisions/oss-signature]] - OSS 签名修复
重要技术决策,记录思考过程:
markdown# OSS 签名 URL 生成
## 问题
视频链接签名认证失败
## 根因
OSS 签名基于未编码路径计算
## 解决方案
路径保持未编码状态
## 参考
- TOOLS.md 已记录
- MEMORY.md 已更新
wiki_tools.py 提供统一操作接口:
python# 添加页面
add_wiki_page(path, content, tags)
# 更新页面
update_wiki_page(path, new_content)
# 搜索页面
search_wiki(query)
# 列出页面
list_wiki_pages(category)
# 整合每日记录
consolidate_daily_log(date)
python# 添加决策记录
add_wiki_page(
"decisions/oss-signature.md",
"# OSS 签名 URL
...
",
["oss", "signature", "bugfix"]
)
# 搜索相关页面
results = search_wiki("oss signature")
Wiki 层通过定时任务自动维护:
| 任务 | 频率 | 功能 |
|---|---|---|
consolidate_daily | 每日 | 整合 OpenClaw 日志到 daily/ |
update_index | 每小时 | 更新首页汇总 |
check_health | 每周 | 检查 Wiki 健康状态 |
Wiki 目录可直接作为 Obsidian vault:
wiki/ 目录系列: 双层记忆架构 | 章节: 第3章 | 更新: 2026-04-07
LanceDB 层是双层记忆架构的机器检索层:
| 字段 | 类型 | 说明 |
|---|---|---|
id | UUID | 唯一标识 |
content | string | 原文内容 |
embedding | vector[1024] | text-embedding-v4 向量 |
memory_type | string | 记忆类型(12 种) |
tags | list | 标签列表 |
wiki_path | string | Wiki 页面路径 |
importance | int | 重要度 1-5 |
confidence | float | 可信度 0.0-1.0 |
status | string | active/archived/deprecated |
created_by | string | 写入者 |
updated_at | string | 更新时间 |
metadata | dict | 扩展字段 |
| 类型 | 说明 | 示例 |
|---|---|---|
decision | 重要决策 | OSS 签名修复 |
lesson | 经验教训 | 签名基于未编码路径 |
project | 项目信息 | 米家监控路径 |
bugfix | Bug 修复 | 403 错误根因 |
feature | 功能特性 | 视频自动录制 |
config | 配置信息 | OSS bucket 名称 |
api | API 使用 | 签名 URL 生成 |
workflow | 工作流程 | 定时任务整合 |
tool | 工具配置 | qwen 命令路径 |
integration | 集成方案 | Obsidian vault |
reference | 参考链接 | Karpathy Wiki |
note | 临时笔记 | 待验证想法 |
pythonfrom lancedb_memory import MemoryStore
store = MemoryStore()
store.add(
content="OSS 签名基于未编码路径计算",
memory_type="lesson",
tags=["oss", "signature", "bugfix"],
importance=4,
wiki_path="decisions/oss-signature.md"
)
python# 自然语言查询
results = store.search(
query="oss 签名怎么生成",
limit=5
)
# 返回最相关的 5 条记忆
for r in results:
print(f"{r.content} (score: {r._distance})")
python# 按类型过滤
results = store.search(
query="监控",
filter="memory_type = 'project'"
)
# 按标签过滤
results = store.search(
query="bug",
filter="tags contains 'oss'"
)
# 按重要度过滤
results = store.search(
query="重要",
filter="importance >= 4"
)
pythonDEDUP_DISTANCE_THRESHOLD = 0.05 # 语义相似度阈值
python# 写入前检查
if store.check_duplicate(content, threshold=0.05):
print("已存在相似记忆,跳过写入")
else:
store.add(content, ...)
python# 阿里云百炼 Embedding
model = "text-embedding-v4"
dimension = 1024
# API 调用
embedding = dashscope.TextEmbedding.call(
model=model,
input=content
)
json// lancedb-memory/config/memory.config.json
{
"embedding": {
"model": "text-embedding-v4",
"dimension": 1024
},
"dedup": {
"threshold": 0.05
}
}
每条 LanceDB 记录可关联到 Wiki 页面:
pythonstore.add(
content="OSS 签名基于未编码路径计算",
wiki_path="decisions/oss-signature.md"
)
python# 从 LanceDB 查 Wiki 页面
record = store.get_by_id(id)
wiki_page = read_wiki_page(record.wiki_path)
# 从 Wiki 页面查 LanceDB 记录
records = store.search_by_wiki_path("decisions/oss-signature.md")
~/.copaw/workspaces/default/skills/lancedb-memory/data/ ├── memory.lance # LanceDB 表 ├── config/ # 配置文件 └── scripts/ # 操作脚本
系列: 双层记忆架构 | 章节: 第4章 | 更新: 2026-04-07
双层不是两个独立系统,而是紧密协作的整体:
用户/Agent 操作 ↓ wiki_tools.py ↓ ┌──────────────────┬──────────────────────┐ │ 1. 写入 Wiki │ 2. 同步 LanceDB │ │ Markdown 文件 │ 向量 + 结构化字段 │ └──────────────────┴──────────────────────┘
pythondef add_memory(content, memory_type, wiki_path=None, tags=[]):
# 1. 写入 Wiki 层
if wiki_path:
write_wiki_page(wiki_path, content)
# 2. 同步到 LanceDB
store.add(
content=content,
memory_type=memory_type,
wiki_path=wiki_path,
tags=tags
)
python# LanceDB 搜索
results = store.search("oss 签名")
# 返回结果包含 wiki_path
for r in results:
if r.wiki_path:
# 打开 Wiki 页面查看完整内容
page = read_wiki_page(r.wiki_path)
python# 从 Wiki 页面获取相关记忆
def get_page_memories(wiki_path):
return store.search_by_wiki_path(wiki_path)
pythondef update_wiki_page(path, new_content):
# 1. 更新 Wiki 文件
write_wiki_page(path, new_content)
# 2. 更新 LanceDB 记录
records = store.search_by_wiki_path(path)
for r in records:
store.update(r.id, content=new_content)
定时任务(每日) ↓ 1. 收集 OpenClaw 日志 2. 整合到 Wiki daily/ 3. 提取关键内容 → LanceDB 4. 检查重复 → 清理冗余 5. 更新 Wiki index.md
pythondef consolidate_daily(date):
# 1. 收集日志
logs = collect_openclaw_logs(date)
# 2. 整合到 Wiki
wiki_content = format_logs_to_markdown(logs)
write_wiki_page(f"daily/{date}.md", wiki_content)
# 3. 提取关键内容到 LanceDB
for decision in extract_decisions(logs):
store.add(
content=decision,
memory_type="decision",
wiki_path=f"daily/{date}.md"
)
# 4. 去重检查
check_and_clean_duplicates()
# 5. 更新首页
update_index_page()
json// wiki.config.json
{
"sync": {
"wiki_to_lancedb": true, // Wiki 写入同步到 LanceDB
"lancedb_to_wiki": false, // LanceDB 写入不自动创建 Wiki
"dedup_on_sync": true // 同步时去重
},
"consolidate": {
"daily": true, // 每日整合
"hourly": false // 每小时整合(可选)
}
}
系列: 双层记忆架构 | 章节: 第5章 | 更新: 2026-04-07
"让 AI 成为知识库的主动维护者"
双层记忆通过定时任务实现自动维护,无需手动整理。
| 任务名 | 频率 | 功能 |
|---|---|---|
consolidate_daily | 每日 00:00 | 整合日志到 Wiki daily/ |
update_index | 每小时 | 更新 Wiki 首页汇总 |
check_health | 每周日 | 检查 Wiki 健康状态 |
clean_duplicates | 每周一 | 清理 LanceDB 重复记录 |
sync_lancedb | 实时 | Wiki 写入同步 LanceDB |
pythondef consolidate_daily(date=None):
if date is None:
date = datetime.now().strftime("%Y-%m-%d")
# 1. 收集 OpenClaw 日志
logs = collect_session_logs(date)
heartbeat_logs = collect_heartbeat_logs(date)
# 2. 分类整理
projects = extract_projects(logs)
decisions = extract_decisions(logs)
lessons = extract_lessons(logs)
# 3. 写入 Wiki daily/
daily_content = format_daily_markdown(projects, decisions, lessons)
write_wiki_page(f"daily/{date}.md", daily_content)
# 4. 同步到 LanceDB
for decision in decisions:
store.add(content=decision, memory_type="decision", importance=4)
# 5. 更新首页
update_index_page()
pythondef update_index_page():
# 1. 获取最近更新
recent_pages = list_recent_wiki_pages(limit=10)
# 2. 获取热门项目
hot_projects = get_hot_projects()
# 3. 获取重要决策
important_decisions = store.search(
query="",
filter="memory_type = 'decision' AND importance >= 4",
limit=5
)
# 4. 生成首页内容
index_content = format_index_markdown(recent_pages, hot_projects, important_decisions)
write_wiki_page("index.md", index_content)
pythondef check_health():
report = {
"wiki": {
"total_pages": count_wiki_pages(),
"broken_links": find_broken_links(),
"empty_pages": find_empty_pages()
},
"lancedb": {
"total_records": store.count(),
"deprecated_count": store.count(status="deprecated")
}
}
# 生成报告
write_wiki_page("health/weekly-report.md", format_report(report))
# 发送通知
if report["wiki"]["broken_links"] > 0:
notify("Wiki 有断链,请检查")
json// openclaw.json
{
"cron": [
{
"id": "wiki-consolidate-daily",
"schedule": "0 0 * * *",
"command": "consolidate_daily",
"delivery": {"mode": "silent"}
},
{
"id": "wiki-update-index",
"schedule": "0 * * * *",
"command": "update_index_page",
"delivery": {"mode": "silent"}
},
{
"id": "wiki-check-health",
"schedule": "0 0 * * 0",
"command": "check_health",
"delivery": {"mode": "announce"}
}
]
}
系列: 双层记忆架构 | 章节: 第6章 | 更新: 2026-04-07
开发过程中的常见问题:
python# 做出重要决策时
add_memory(
content="OSS 签名基于未编码路径,不能解码",
memory_type="decision",
tags=["oss", "signature"],
importance=4,
wiki_path="decisions/oss-signature.md"
)
# 一周后查询
results = store.search("oss 签名怎么生成")
# 立刻找到答案!
python# 修复 Bug 后
add_memory(
content="Flask 安装 macOS 需要 --break-system-packages",
memory_type="lesson",
tags=["flask", "macos", "pip"],
importance=3
)
pythonadd_memory(
content="米家监控项目: ~/Documents/mijiamonitor, 端口 8001/3000",
memory_type="project",
tags=["mijiamonitor", "fastapi", "vue"],
wiki_path="projects/mijiamonitor.md"
)
# 快速查询
results = store.search("米家监控项目路径")
bash# .git/hooks/post-commit
#!/bin/bash
COMMIT_MSG=$(git log -1 --pretty=%B)
curl -X POST http://localhost:18789/api/memory -d "content=$COMMIT_MSG" -d "memory_type=code_change"
pythondef call_api(endpoint, params, response):
add_memory(
content=f"API: {endpoint}",
memory_type="api",
metadata={"status": response.status_code}
)
| 场景 | 解决方案 | 收益 |
|---|---|---|
| 决策遗忘 | 自动记录 | 不再重复讨论 |
| Bug 重复踩坑 | 经验留存 | 快速定位 |
| 项目信息分散 | 集中索引 | 一处找到 |
系列: 双层记忆架构 | 章节: 第7章 | 更新: 2026-04-07
个人学习和知识积累的问题:
pythonadd_memory(
content="Karpathy Wiki: 让 AI 成为知识库主动维护者",
memory_type="reference",
tags=["karpathy", "wiki", "ai"],
wiki_path="references/karpathy-wiki.md",
importance=4
)
results = store.search("karpathy 知识管理理念")
python# 技术知识
add_memory(
content="LanceDB 向量数据库入门",
memory_type="lesson",
tags=["tech", "lancedb", "vector-db"],
importance=3
)
# 财务知识
add_memory(
content="ETF 定投策略:分散风险",
memory_type="lesson",
tags=["finance", "invest", "etf"],
importance=4
)
wiki/ ← Obsidian 打开此目录 ├── index.md # 首页 ├── daily/ # 每日笔记 ├── projects/ # 项目笔记 ├── decisions/ # 决策记录 └── lessons/ # 经验教训
| 功能 | 双层记忆对应 |
|---|---|
双向链接 [[...]] | Wiki 页面关联 |
标签 #tag | LanceDB tags 字段 |
| 图谱视图 | 项目/决策可视化 |
| 搜索 | LanceDB 语义搜索增强 |
pythondef find_related_knowledge(topic):
results = store.search(topic, limit=10)
related = cluster_by_tags(results)
return related
# "向量数据库" → 返回:
# - LanceDB 入门
# - Embedding 模型选择
# - 语义搜索原理
1. 学习新知识 ↓ 2. add_memory() 记录要点 ↓ 3. 定时任务整合到 Wiki ↓ 4. Obsidian 查看复习 ↓ 5. 开发时 store.search() 查询应用 ↓ 6. 新问题 → 新知识(循环)
| 方式 | 传统笔记 | 双层记忆 |
|---|---|---|
| 查找方式 | 关键词搜索 | 语义搜索 |
| 关联发现 | 手动链接 | 自动推荐 |
| 整理方式 | 手动分类 | 定时任务自动 |
| 记忆提醒 | 无 | 定期复习 |
| AI 可用 | ❌ | ✅ |
系列: 双层记忆架构 | 章节: 第8章 | 更新: 2026-04-07
| 场景 | 推荐时机 | 重要度 |
|---|---|---|
| 重要决策 | 决策确定后立即 | 4-5 |
| Bug 修复 | 问题解决后 | 3-4 |
| 项目信息 | 项目启动时 | 3 |
| 学习笔记 | 学习完成后 | 2-4 |
python# 标签层次化
tags = ["tech", "oss", "signature", "bugfix"]
# 避免过于宽泛
❌ tags = ["技术"]
✅ tags = ["tech", "oss", "signature"]
decisions/{主题}-{日期}.md projects/{项目名}.md lessons/{领域}-{主题}.md daily/{日期}.md
问题:每件小事都记录,噪音过多
解决:只记录重要度 >= 3 的内容
pythonif importance >= 3:
add_memory(...)
问题:标签过多,检索困难
解决:标签控制在 3-5 个
pythontags = ["tech", "oss", "signature"] # ✅
tags = ["oss", "bug", "fix", "url", "sign", "path", "code"] # ❌
问题:Wiki 更新了,LanceDB 没更新
解决:使用统一接口,自动同步
python✅ wiki_tools.add_memory(...) # 双层同步
❌ write_wiki_page(...) # 只写 Wiki
❌ store.add(...) # 只写 LanceDB
问题:相似内容多次写入
解决:写入前检查重复
pythonif store.check_duplicate(content, threshold=0.05):
print("已存在,跳过")
else:
add_memory(...)
python# 不要记录敏感信息
❌ add_memory(content="数据库密码: xxx")
# 使用引用
✅ add_memory(content="数据库密码见 TOOLS.md")
分享前检查:
pythondef check_health():
return {
"wiki_pages": count_wiki_pages(),
"lancedb_records": store.count(),
"deprecated_ratio": store.count(status="deprecated") / store.count()
}
# 建议阈值:deprecated_ratio < 10%
| 维度 | 传统方案 | 双层记忆 |
|---|---|---|
| 记录方式 | 手动 | 自动 + 定时 |
| 检索方式 | 关键词 | 语义搜索 |
| 知识关联 | 手动 | 自动发现 |
| 人机共享 | ❌ | ✅ |
| 维护成本 | 高 | 低(自动化) |
| AI 可用性 | ❌ | ✅ |
本系列共 8 章,完整介绍了双层记忆架构:
感谢阅读!开始构建你的双层记忆吧。
本文作者:lazyyoun
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!