链载Ai

标题: RAG技术全解析:从基础原理到优化实战 [打印本页]

作者: 链载Ai    时间: 2 小时前
标题: RAG技术全解析:从基础原理到优化实战

How I built a Simple Retrieval-Augmented Generation (RAG) Pipeline | by Dr  Julija | Medium
你了解RAG吗?,下面这些问题你是否能回答上来呢?

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">本篇文章来详细介绍什么是 RAG?RAG 的流程以及RAG 进阶优化

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">干货满满,相信你看完文章之后会对RAG有更深入的理解。

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">RAG 介绍

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">RAG(Retrieval Augmented Generation) 是一种无需微调即可扩充模型知识的常用方法。 借助 RAG,LLM可以从数据库中检索上下文文档,以提高答案的准确性。

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">因为 LLM 大模型通过海量数据进行训练,数据是有时效性的。如果询问最新的文档或者一些专业领域的知识,LLM 是无法回答的。所以检索增强生成(RAG) 通过将你的数据添加到 LLM 已有的数据中来解决此问题。

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">RAG 解决了纯生成模型的局限性(如幻觉、知识过时等),通过动态检索外部知识增强生成结果的可信度和时效性。

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">RAG 流程

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">典型的 RAG 流程分为两个部分:

    ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: justify;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;" class="list-paddingleft-1">
  1. 构建向量存储:创建向量存储是构建检索增强生成 (RAG) 流程的第一步。文档会被加载(Load)、拆分(Split)、Embedding 存储到向量数据库中。


索引图


2. 检索生成:根据用户输入用向量数据库进行相似性搜索,让后把用户的question和搜索到的context作为上下文喂给 LLM 大模型,LLM 分析推理回答用户的问题。

检索图


我使用LangGraph构建了基本的 RAG 流程:

Screenshot 2024-02-14 at 3.43.58 PM.png

调用Retrieval Tool搜索相关文档,然后GradeDocument对文档进行评分:对从向量数据库检索到的文档进行评分: 如果检索到的文档与用户输入的内容相关,则GenerateAnswer生成答案返回,如果不相关,则Rewrite重新生成 query 进行检索。


代码已上传 Github:

Agent-demo/tree/main/src/rag_agent" style="box-sizing: border-box !important;overflow-wrap: break-word;text-decoration: none;color: rgb(87, 107, 149);font-weight: 600;">https://github.com/Liu-Shihao/ai-agent-demo/tree/main/src/rag_agent

进阶 - RAG 优化

文档 Chunking

大模型对话的 token 数量是有限制的,文档切分是为了将文档切分为小的文本块,适合检索并且节省 token。切分的文本块长度也会影响 LLM 回答的质量。

常见的切分文档的方法:

  1. 固定长度切分(重叠分块边界)
    :按照字符或者 Token 数(如 512 个 token)切分。重叠分块以避免边界信息丢失。这种方式最简单,但是有可能会截断语义。
  2. 按照句子边界
    (标点符号)分块,例如使用 NLP 框架SpaCy, 但是长段落可能语义断裂。
  3. 自定义规则分割
    :用正则表达式或者 DOM 解析器(如 BeautifulSoup)按照逻辑结构(标题,段落)分块。适合结构化文档,但是需要手动设计分割规则。
  4. 基于语义的分块
    : 用 Transformer 模型分析语义关系分块。

优化原则:

相似性算法

在 RAG(检索增强生成)和其他信息检索任务中,相似性算法用于衡量文本、向量或实体之间的关联程度。

  1. 欧氏距离(L2): 欧几里得距离测量连接两点的线段的长度(计算向量间的直线距离)。它是最常用的距离度量,当数据连续时非常有用。值越小,相似度越高。

  2. 余弦相似度(COSINE): 余弦相似度使用两组向量之间夹角的余弦来衡量它们的相似程度。余弦相似度始终在区间[-1, 1]内。余弦值越大,两个向量之间的夹角越小,表明这两个向量彼此越相似。适合文本 embedding 比较。

  3. BM25((Best Matching 25)):BM25 基于词频(TF)逆文档频率(IDF)。根据词频、倒排文档频率、文档规范化对相关性进行评分。用于评估文档与查询的相关性。 广泛应用于搜索引擎和问答系统。如 Elasticsearch 默认使用 BM25 排序。






欢迎光临 链载Ai (https://www.lianzai.com/) Powered by Discuz! X3.5