跳转至

RAG(检索增强生成)

文档分块 → 向量化 → 检索 → 生成

RAG 流水线

  1. 文档加载 — PDF/Word/HTML/Markdown 解析
  2. 文本分块 — 按语义/固定长度/递归分块
  3. 向量化 — Embedding 模型编码
  4. 存储 — 向量数据库索引
  5. 检索 — 语义检索 / 混合检索
  6. 重排序 — Rerank 模型精排
  7. 生成 — 检索结果 + Query → LLM 生成

分块策略

策略 特点 适用
固定长度 简单,可能截断语义 通用基线
递归字符分割 按分隔符层级分割 LangChain 默认
语义分块 按语义相似度切分 高质量需求
文档结构分块 按标题/段落分割 Markdown/HTML

向量数据库

数据库 链接 特点
FAISS https://github.com/facebookresearch/faiss Meta 开源,本地,快
Chroma https://github.com/chroma-core/chroma 轻量级,Python 原生
Milvus https://github.com/milvus-io/milvus 分布式,生产级
Qdrant https://github.com/qdrant/qdrant Rust 实现,高性能
Pinecone https://www.pinecone.io/ 云托管,免运维

Embedding 模型

模型 链接 说明
BGE 系列 https://huggingface.co/BAAI/bge-large-zh-v1.5 国产,中文强
M3E https://huggingface.co/moka-ai/m3e-large 中文 Embedding
text-embedding-3 https://platform.openai.com/docs/guides/embeddings OpenAI
GTE https://huggingface.co/Alibaba-NLP/gte-large-en-v1.5 阿里

框架

框架 链接 特点
LangChain https://python.langchain.com/docs/ 全链路,生态丰富
LlamaIndex https://docs.llamaindex.ai/ RAG 专用,索引优化
Haystack https://haystack.deepset.ai/ 生产级 RAG

练习

  • 用 LangChain + ChromaDB 构建文档问答系统
  • 对比不同分块策略对检索质量的影响
  • 加入 Rerank 提升检索精度
  • 评估 RAG 系统:检索召回率 + 生成质量

资源

资源 链接
RAG 原论文 https://arxiv.org/abs/2005.11401
LangChain RAG 教程 https://python.langchain.com/docs/tutorials/rag/
LlamaIndex 教程 https://docs.llamaindex.ai/en/stable/understanding/indexing/