RAG学习笔记系列(一)

RAG 介绍

RAG 全称为 Retrieval Augmented Generation(检索增强生成)。是基于LLM构建系统的一种架构。

RAG 基本上可以理解为:搜索 + LLM prompting。根据用户的查询语句,系统会先使用搜索算法获取到相关内容作为上下文,然后将用户查询语句和获取到的上下文一起注入到 prompt 中,然后将 prompt 提供给 LLM 来生成回答内容。

RAG初步实现

RAG 初步实现可以简单分解为以下步骤:

  1. 将待检索文本分割成块
  2. 使用 Transformer Encoder 模型将文本嵌入为向量(embedding),并将向量存储
  3. 构建一个 prompt,可以让模型根据搜索到的内容对用户提出的问题进行回答

使用时

  1. 使用相同的 Transformer Encoder 模型,将用户的查询文本转换成向量
  2. 使用查询的向量从向量存储中找到 top-k 的结果
  3. 将用户提的问题和查询到的文本块一起作为上下文整合到 prompt 中
python 复制代码
def question_answering(context, query):
    prompt = f"""
                Give the answer to the user query delimited by triple backticks ```{query}```\
                using the information given in context delimited by triple backticks ```{context}```.\
                If there is no relevant information in the provided context, try to answer yourself, 
                but tell user that you did not have any relevant context to base your answer on.
                Be concise and output the answer of size less than 80 tokens.
                """

    response = get_completion(instruction, prompt, model="gpt-3.5-turbo")
    answer = response.choices[0].message["content"]
    return answer

高级RAG

高级 RAG 架构如下图所示:

图中,绿色元素为 RAG 核心技术点,蓝色元素为文本。(本架构图对一些细节进行省略,不宜按照本图进行实施)
RAG 核心技术点

复制代码
		 1. 分块和矢量化
		 2. 搜索索引构建
		 3. 重排序和过滤
		 4. 查询转换
		 5. 聊天引擎
		 6. 查询路由
		 7. RAG 中的 Agent
		 8. 响应合成
相关推荐
齐生14 天前
iOS 知识点 - 渲染机制、动画、卡顿小集合
笔记
用户962377954484 天前
VulnHub DC-1 靶机渗透测试笔记
笔记·测试
齐生15 天前
iOS 知识点 - IAP 是怎样的?
笔记
tingshuo29176 天前
D006 【模板】并查集
笔记
tingshuo29177 天前
S001 【模板】从前缀函数到KMP应用 字符串匹配 字符串周期
笔记
西岸行者12 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
starlaky12 天前
Django入门笔记
笔记·django
勇气要爆发12 天前
吴恩达《LangChain LLM 应用开发精读笔记》1-Introduction_介绍
笔记·langchain·吴恩达
悠哉悠哉愿意12 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
勇气要爆发12 天前
吴恩达《LangChain LLM 应用开发精读笔记》2-Models, Prompts and Parsers 模型、提示和解析器
android·笔记·langchain