← 返回案例列表

🧠 个人知识库 (RAG) 搭建

📅 2026 年 3 月 9 日 · ⏱️ 预计阅读时间 35 分钟 · 📊 难度:进阶
💡 效果: 自动抓取 URL 内容、YouTube 转录、PDF 解析,支持语义搜索。构建你的第二大脑。

🎯 系统架构

┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ 内容来源 │ ──→ │ 处理管道 │ ──→ │ 向量存储 │ │ │ │ │ │ │ │ - URL │ │ - 抓取 │ │ - ChromaDB │ │ - YouTube │ │ - 转录 │ │ - Pinecone │ │ - PDF │ │ - 解析 │ │ - 本地索引 │ │ - 笔记 │ │ - 分块 │ │ │ └─────────────┘ └─────────────┘ └─────────────┘ │ ↓ ┌─────────────┐ │ 语义搜索 │ │ │ │ - 向量检索 │ │ - 排名 │ │ - 摘要 │ └─────────────┘

📋 前置准备

🔧 配置步骤

步骤 1: 安装向量数据库

# 安装 ChromaDB (本地)
pip install chromadb

# 或使用 Pinecone (云端)
pip install pinecone-client

步骤 2: 配置知识库

创建 ~/.openclaw/config/knowledge-base.yaml:

knowledge_base:
  name: 个人知识库
  
  # 内容来源
  sources:
    urls:
      - https://docs.openclaw.ai/
      - https://github.com/openclaw/openclaw
    
    youtube:
      - channel: UCAgA6bO0xvJzE0
    
    pdf:
      - path: ~/Documents/papers/
    
    notes:
      - path: ~/Notes/
  
  # 处理配置
  processing:
    chunk_size: 1000
    chunk_overlap: 200
    embedding_model: text-embedding-3-small
  
  # 存储配置
  storage:
    type: chromadb
    path: ~/.openclaw/knowledge-base
    # 或 Pinecone
    # type: pinecone
    # api_key: xxx
    # environment: us-west1-gcp

步骤 3: 创建内容抓取脚本

#!/bin/bash

# 抓取 URL 内容
url_content=$(openclaw run web-fetch \
  --url "https://docs.openclaw.ai/" \
  --extract-mode markdown)

# YouTube 转录
youtube_transcript=$(openclaw run youtube-dl \
  --url "https://youtube.com/watch?v=xxx" \
  --subtitles)

# PDF 解析
pdf_content=$(openclaw run pdf \
  --file ~/Documents/paper.pdf \
  --extract text)

# 存储到知识库
openclaw run knowledge-base \
  --add "$url_content" \
  --add "$youtube_transcript" \
  --add "$pdf_content" \
  --tag "auto-import"

步骤 4: 语义搜索

# 搜索知识库
openclaw run knowledge-base \
  --search "如何配置多 Agent 系统" \
  --limit 5 \
  --include-sources

📊 效果展示

🔍 搜索:"如何配置多 Agent 系统"

📄 结果 1 (相似度:0.92)
来源:blog/multi-agent-configuration-guide.md
内容:创建 ~/.openclaw/config/agents.yaml 文件,定义
团队配置,包含 strategist、developer、reviewer 
等角色...

📄 结果 2 (相似度:0.87)
来源:guides/autonomous-tasks.md
内容:orchestration 配置中,mode 可选 sequential
或 parallel,workflow 定义执行顺序...

📄 结果 3 (相似度:0.81)
来源:youtube/transcript/xxx.txt
内容:在这个视频中,我们演示了如何设置多 Agent
协作,首先配置每个 Agent 的角色和技能...

💡 高级用法

自动更新知识库

#!/bin/bash

# 每日自动更新
# crontab: 0 3 * * *

# 检查关注的 URL 是否有更新
openclaw run knowledge-base \
  --update \
  --notify-changes

问答系统

# 基于知识库问答
openclaw run knowledge-base \
  --ask "OpenClaw 支持哪些消息渠道?" \
  --cite-sources

🐛 常见问题

Q: 搜索速度慢?

A: 使用本地 ChromaDB 或优化索引,减少知识库大小。

Q: 搜索结果不相关?

A: 调整 chunk_size 参数,或更换 embedding 模型。

Q: 如何清理知识库?

A: 使用 openclaw run knowledge-base --clean 清理无效内容。

📖 相关阅读