Files
nexus/wiki/concepts/Knowledge-Base-RAG.md

58 lines
2.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "Knowledge Base RAG"
type: concept
last_updated: 2026-04-22
---
## Definition
Retrieval-Augmented GenerationRAG在 LLM 生成回答前,先从外部知识库检索相关文档片段作为上下文补充,从而让 LLM 基于真实、私有或最新信息作答,而非依赖训练数据截止日期或模型幻觉。
## Architecture
```
用户问题 → 编码为向量 → 向量数据库 ANN 检索 → Top-K 相关片段 → 与原问题拼接 → LLM 生成
```
## Components
| 组件 | 说明 |
|------|------|
| **文档切分Chunking** | 将长文档拆分为适合检索的片段(通常 512-1024 tokens过小丢失上下文过大降低精度 |
| **Embedding 模型** | 将文本编码为向量(见 [[Vector-Embedding]] |
| **向量数据库** | 存储 embedding 并支持 ANN 检索Qdrant / Pinecone / pgvector / sqlite-vss |
| **重排序Reranker** | ANN 初筛后,用重排序模型(如 BGE-Reranker精排提高 top-K 准确率 |
| **LLM** | 接收检索片段 + 原问题,生成最终回答 |
## Chunking Strategies
| 策略 | 适用场景 |
|------|------|
| 固定长度切分 | 简单快速,但可能切断语义单元 |
| 递归字符切分Recursive Character Splitting | 按段落/句子边界切分,保留语义完整性 |
| 基于语义切分Semantic Chunking | 用 LLM 判定切分点,效果最好但成本高 |
| Agentic Chunking | 按工作流/主题边界切分,适合知识库分域管理 |
## Applications in OpenClaw Workflows
| 场景 | 说明 |
|------|------|
| [[YouTube-Content-Pipeline]] | 分享 Slack 链接时Agent 查询知识库了解用户已有内容,避免重复选题 |
| [[Second Brain]] | 个人知识库 RAG支持跨记忆/文档的语义搜索 |
| [[Pre-Build-Idea-Validator]] | 扫描知识库确认是否已做过类似项目 |
| [[autonomous-game-dev-pipeline]] | 检索技术债务和已有代码片段 |
## Quality Optimization
1. **Hybrid Search**:向量检索 + BM25 关键词检索融合,提升召回率
2. **Query Expansion**:将用户问题改写为多个视角再检索
3. **Context Compression**LLM 前对检索片段做摘要压缩,节省 token
4. **Chunk Overlap**:相邻 chunk 重叠 10-20% 防止边界截断关键信息
## Connections
- [[Vector-Embedding]] — RAG 的检索底层
- [[Semantic-Deduplication]] — 语义去重防止 RAG 检索重复片段
- [[OpenClaw]] — 提供 `knowledge-base` skill 实现 RAG
- [[Second Brain]] — RAG 的个人知识管理应用