构建检索增强生成(RAG)应用程序
LLM支持的最强大的应用程序之一是复杂的问答(Q&A)聊天机器人。这些应用程序可以回答有关特定源信息的问题。这些应用程序使用一种称为检索增强生成或RAG的技术。
本教程将展示如何在文本数据源上构建一个简单的问答应用程序。在此过程中,我们将介绍一个典型的问答架构,并突出显示用于更高级问答技术的其他资源。我们还将了解LangSmith如何帮助我们跟踪和理解我们的应用程序。随着我们的应用程序变得越来越简单,LangSmith将变得越来越有帮助。
如果您已经熟悉基本检索,您可能还对不同检索技术的高级概述感兴趣。
什么是RAG?
RAG是一种用附加数据扩充LLM知识的技术。
LLM可以推理广泛的主题,但他们的知识仅限于公共数据,直到他们接受培训的特定时间点。如果你想构建人工智能应用程序来推理私人数据或模型截止日期后引入的数据,你需要用模型所需的特定信息来扩充模型的知识。引入适当信息并将其插入模型提示符的过程被称为检索增强生成(RAG)。
LangChain有许多组件旨在帮助构建问答应用程序,以及更普遍的RAG应用程序。
注意:这里我们关注非结构化数据的问答。如果您对结构化数据上的RAG感兴趣,请查看我们关于在SQL数据上提问/回答的教程。
概念
典型的RAG应用程序有两个主要组件:
索引:用于从源中摄取数据并对其进行索引的管道。这通常离线发生。
检索和生成:实际的RAG链,它在运行时接受用户查询并从索引中检索相关数据,然后将其传递给模型。
从原始数据到答案最常见的完整序列如下所示:
索引
- Load:首先我们需要加载数据。加载来自各种数据源和不同类型的文档,这是使用Document Loaders完成的。
- Split(拆分):文本拆分器将大的Documents分解成更小的块。这对于索引数据和将其传递给模型都很有用,因为大块更难搜索,也不适合模型的有限上下文窗口。
- Store(存储):我们需要某个地方来存储和索引我们的拆分,以便以后可以搜索它们。这通常使用VectorStore和Embedding模型来完成。
检索和生成
设置
安装LangChain
pip install langchain langchain_community langchain_chroma
预览
在本指南中,我们将构建一个应用程序来回答有关网站内容的问题。我们将使用的具体网站是LilianWeng的LLM PoweredAutomousAgents博客帖子,它允许我们就帖子内容提出问题。
我们可以在约20行代码中创建一个简单的索引管道和RAG链来执行此操作:
pip install -qU langchain-openai
import getpass
api_key = getpass.getpass()
from langchain_openai import ChatOpenAI
base_url = "https://api.siliconflow.cn/v1/"
model = ChatOpenAI(
base_url=base_url,
api_key=api_key,
model="Pro/deepseek-ai/DeepSeek-V3",
)
import bs4
from langchain import hub
from langchain_chroma import Chroma
from langchain_community.document_loaders import WebBaseLoader
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
# Load, chunk and index the contents of the blog.
loader = WebBaseLoader(
web_paths=("https://lilianweng.github.io/posts/2023-06-23-agent/",),
bs_kwargs=dict(
parse_only=bs4.SoupStrainer(
class_=("post-content", "post-title", "post-header")
)
),
)
docs = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splits = text_splitter.split_documents(docs)
vectorstore = Chroma.from_documents(documents=splits, embedding=OpenAIEmbeddings(
model="BAAI/bge-m3",
openai_api_key=getpass.getpass(),
openai_api_base="https://api.siliconflow.cn/v1/",
chunk_size=64,
))
# Retrieve and generate using the relevant snippets of the blog.
retriever = vectorstore.as_retriever()
prompt = hub.pull("rlm/rag-prompt")
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
rag_chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| model
| StrOutputParser()
)
rag_chain.invoke("What is Task Decomposition?")
API参考:WebBaseLoader | StrOutputParser | RunnablePassthrough | OpenAIEmbeddings | RecursiveCharacterTextSplitter
'Task Decomposition is a process where a complex task is broken down into smaller, simpler steps or subtasks. This technique is utilized to enhance model performance on complex tasks by making them more manageable. It can be done by using language models with simple prompting, task-specific instructions, or with human inputs.'
# cleanup
vectorstore.delete_collection()
详细介绍
让我们逐步完成上面的代码,以真正了解发生了什么。
1. 索引:加载
我们需要首先加载博客文章内容。我们可以为此使用DocumentLoaders,它是从源加载数据并返回Documents
列表的对象。Document
是具有一些page_content(str)
和metadata(dic)
的对象。
在这种情况下,我们将使用WebBaseLoader,它使用urllib
从Web URL加载超文本标记语言,并将其拓扑为文本。我们可以通过bs_kwargs
将参数传递给BeautifulSoup
解析器来自定义超文本标记语言->文本解析(请参阅BeautifulSoup docs)。在这种情况下,只有具有“post-content”、“post-title”或“post-head”类的超文本标记语言标签是相关的,因此我们将删除所有其他标签。
import bs4
from langchain_community.document_loaders import WebBaseLoader
# Only keep post title, headers, and content from the full HTML.
bs4_strainer = bs4.SoupStrainer(class_=("post-title", "post-header", "post-content"))
loader = WebBaseLoader(
web_paths=("https://lilianweng.github.io/posts/2023-06-23-agent/",),
bs_kwargs={"parse_only": bs4_strainer},
)
docs = loader.load()
len(docs[0].page_content)
API参考:WebBaseLoader
43131
print(docs[0].page_content[:500])
LLM Powered Autonomous Agents
Date: June 23, 2023 | Estimated Reading Time: 31 min | Author: Lilian Weng
Building agents with LLM (large language model) as its core controller is a cool concept. Several proof-of-concepts demos, such as AutoGPT, GPT-Engineer and BabyAGI, serve as inspiring examples. The potentiality of LLM extends beyond generating well-written copies, stories, essays and programs; it can be framed as a powerful general problem solver.
Agent System Overview#
In
深入
DocumentLoader
:从源加载数据作为Documents
列表的对象。
2. 索引:拆分
我们加载的文档超过42k个字符长。这对于许多模型的上下文窗口来说太长了。即使对于那些可以在上下文窗口中容纳完整帖子的模型,模型也很难在很长的输入中找到信息。
为了处理这个问题,我们将Document
拆分为用于嵌入和向量存储的块。这应该有助于我们在运行时仅检索博客文章中最相关的位。
在这种情况下,我们将把文档分成1000个字符的块,块之间有200个字符的重叠。重叠有助于将语句与与其相关的重要上下文分开的可能性。我们使用RecursiveCharacterTextSplitter,它将使用公共分隔符递归地拆分文档,例如新行,直到每个块的大小合适。这是通用文本用例的推荐文本拆分器。
我们设置add_start_index=True
,以便在初始Document中每个拆分Document开始的字符索引保留为元数据属性“start_index”。
from langchain_text_splitters import RecursiveCharacterTextSplitter
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000, chunk_overlap=200, add_start_index=True
)
all_splits = text_splitter.split_documents(docs)
len(all_splits)
API参考:RecursiveCharacterTextSplitter
66
len(all_splits[0].page_content)
{'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/',
'start_index': 7056}
深入
TextSplitter
:将Document
拆分为小块的对象。DocumentTransformers
的子类。
DocumentTransformer:对Document对象列表执行转换的对象。
- Docs:关于如何使用DocumentTransformers的详细留档
- Integrations 集成
- Interface:基本接口的API引用。
3. 索引:存储
现在我们需要索引我们的66个文本块,以便我们可以在运行时搜索它们。
最常见的方法是嵌入每个文档拆分的内容,并将这些嵌入插入向量数据库(或向量存储)。
当我们想搜索拆分时,我们采用文本搜索查询,嵌入它,并执行某种“相似性”搜索,以识别与我们的查询嵌入最相似的嵌入存储的拆分。
最简单的相似性度量是余弦相似性——我们测量每对嵌入之间角度的余弦(这是高维向量)。
我们可以使用Chroma向量存储和OpenAIEmbedding模型(或其他嵌入模型)将所有文档拆分嵌入并存储在单个命令中。
from langchain_chroma import Chroma
from langchain_openai import OpenAIEmbeddings
vectorstore = Chroma.from_documents(documents=all_splits, embedding=OpenAIEmbeddings())
API参考:OpenAIEmbeddings
深入
Embeddings
:文本嵌入模型的包装器,用于将文本转换为嵌入(向量)。
VectorStore
:围绕向量数据库的包装器,用于存储和查询嵌入。
这就完成了管道的索引部分。此时,我们有一个可查询的向量存储,其中包含我们博客文章的分块内容。给定一个用户问题,理想情况下,我们应该能够返回回答问题的博客文章片段。
4. 检索和生成:检索
现在让我们编写实际的应用程序逻辑。我们想创建一个简单的应用程序,它接受一个用户问题,搜索与该问题相关的文档,将检索到的文档和初始问题传递给模型,并返回一个答案。
首先,我们需要定义搜索文档的逻辑。LangChain定义了一个Retriever接口,它包装了一个索引,该索引可以在给定字符串查询的情况下返回相关的Documents
。
Retriever
最常见的类型是VectorStoreRetriever,它使用向量存储的相似性搜索功能来促进检索。任何VectorStore
都可以很容易地变成带有VectorStore.as_retriever()
:
retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 6})
retrieved_docs = retriever.invoke("What are the approaches to Task Decomposition?")
len(retrieved_docs)
6
print(retrieved_docs[0].page_content)
Tree of Thoughts (Yao et al. 2023) extends CoT by exploring multiple reasoning possibilities at each step. It first decomposes the problem into multiple thought steps and generates multiple thoughts per step, creating a tree structure. The search process can be BFS (breadth-first search) or DFS (depth-first search) with each state evaluated by a classifier (via a prompt) or majority vote.
Task decomposition can be done (1) by LLM with simple prompting like "Steps for XYZ.\n1.", "What are the subgoals for achieving XYZ?", (2) by using task-specific instructions; e.g. "Write a story outline." for writing a novel, or (3) with human inputs.
深入
向量存储通常用于检索,但也有其他方法可以进行检索。
Retriever
:在给定文本查询的情况下返回Document
的对象
- docs:Furtherdocumentation界面和内置检索技术。其中一些包括:
MultiQueryRetriever
生成输入问题的变体以提高检索命中率。MultiVectorRetriever
改为生成嵌入的变体,也是为了提高检索命中率。Max marginal relevance
在检索到的文档中选择相关性和多样性以避免传入重复上下文。- 可以在向量存储检索期间使用元数据过滤器过滤文档,例如使用SelfQueryRetriever。
- 集成:与检索服务的集成。
- Interface:基本接口的API引用。
5. 检索和生成:生成
让我们将所有这些放在一个链中,该链接受一个问题,检索相关文档,构造一个提示,将其传递给模型,并解析输出。
我们将使用DeepSeek-V3聊天模型,但可以替换任何LangChainLLM或ChatModel。
pip install -qU langchain-openai
import getpass
api_key = getpass.getpass()
from langchain_openai import ChatOpenAI
base_url = "https://api.siliconflow.cn/v1/"
model = ChatOpenAI(
base_url=base_url,
api_key=api_key,
model="Pro/deepseek-ai/DeepSeek-V3",
)
我们将使用签入LangChain提示符中心(here)的RAG提示符。
from langchain import hub
prompt = hub.pull("rlm/rag-prompt")
example_messages = prompt.invoke(
{"context": "filler context", "question": "filler question"}
).to_messages()
example_messages
[HumanMessage(content="You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.\nQuestion: filler question \nContext: filler context \nAnswer:")]
print(example_messages[0].content)
You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.
Question: filler question
Context: filler context
Answer:
我们将使用LCEL Runnable协议来定义链,允许我们
- 以透明的方式将组件和功能管道连接在一起
- 在LangSmith中自动追踪我们的链
- 开箱即用的流式传输、异步和批量调用。 这是实现:
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
rag_chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
for chunk in rag_chain.stream("What is Task Decomposition?"):
print(chunk, end="", flush=True)
API参考:StrOutputParser | RunnablePassthrough
Task Decomposition is a process where a complex task is broken down into smaller, more manageable steps or parts. This is often done using techniques like "Chain of Thought" or "Tree of Thoughts", which instruct a model to "think step by step" and transform large tasks into multiple simple tasks. Task decomposition can be prompted in a model, guided by task-specific instructions, or influenced by human inputs.
让我们剖析LCEL以了解发生了什么。
首先:这些组件(retriever
、prompt
、llm
等)都是Runnable的实例。这意味着它们实现了相同的方法——例如sync
和async``.invoke
、.stream
或.batch
——这使得它们更容易连接在一起。它们可以通过|运算符连接到RunnableSequence——另一个Runnable组件。
当遇到|
运算符时,LangChain会自动将某些对象转换为runnable
。在这里,format_docs被转换为RunnableLambda,带有"context"和"question"的dic被转换为RunnableParline。细节并不重要,重要的一点是,每个对象都是一个Runnable
。
让我们跟踪一下输入问题是如何流经上述可运行程序的。
正如我们在上面看到的,提示词的输入应该是一个带有“context”和“question”键的字典。因此,这个链的第一个元素构建了可运行程序,将从输入问题中计算这两个:
retriver | format_docs
通过检索器传递问题,生成Document
对象,然后传递给format_docs
生成字符串;RunnablePassthrough()
不加更改地遍历输入问题
也就是说,如果你构建了
chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
)
然后chain.invoke(question)
将构建一个格式化的提示词,准备进行推理。(注意:当使用LCEL进行开发时,可以使用这样的子链进行测试。)
该链的最后两个步骤是llm和StrOutputParser(),前者运行推理,后者仅从llm的输出消息中提取字符串内容。
内置链
如果愿意的话,LangChain还包括实现上述LCEL的便利函数。我们组成两个函数:
- create_stuff_documents_chain指定如何将检索到的上下文馈送到提示词和LLM。在这种情况下,我们将把内容“塞”到提示词中——也就是说,我们将包括所有检索到的上下文,而不进行任何摘要或其他处理。它在很大程度上实现了上面的rag_chain,使用
context
和input
——它使用检索到的上下文和查询生成答案。 - create_retrieval_chain添加检索步骤并通过链传播检索到的上下文,将其与最终答案一起提供。它具有输入键input,并在其输出中包含
input
、context
和answer
。
from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
system_prompt = (
"You are an assistant for question-answering tasks. "
"Use the following pieces of retrieved context to answer "
"the question. If you don't know the answer, say that you "
"don't know. Use three sentences maximum and keep the "
"answer concise."
"\n\n"
"{context}"
)
prompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt),
("human", "{input}"),
]
)
question_answer_chain = create_stuff_documents_chain(llm, prompt)
rag_chain = create_retrieval_chain(retriever, question_answer_chain)
response = rag_chain.invoke({"input": "What is Task Decomposition?"})
print(response["answer"])
API参考:create_retrieval_chain|create_stuff_documents_chain|ChatPromptTemplate
Task Decomposition is a process in which complex tasks are broken down into smaller and simpler steps. Techniques like Chain of Thought (CoT) and Tree of Thoughts are used to enhance model performance on these tasks. The CoT method instructs the model to think step by step, decomposing hard tasks into manageable ones, while Tree of Thoughts extends CoT by exploring multiple reasoning possibilities at each step, creating a tree structure of thoughts.
返回源 通常在问答应用程序中,向用户展示用于生成答案的来源非常重要。LangChain的内置create_retrieval_chain将检索到的源文档传播到"context"键中的输出:
for document in response["context"]:
print(document)
print()
page_content='Fig. 1. Overview of a LLM-powered autonomous agent system.\nComponent One: Planning#\nA complicated task usually involves many steps. An agent needs to know what they are and plan ahead.\nTask Decomposition#\nChain of thought (CoT; Wei et al. 2022) has become a standard prompting technique for enhancing model performance on complex tasks. The model is instructed to “think step by step” to utilize more test-time computation to decompose hard tasks into smaller and simpler steps. CoT transforms big tasks into multiple manageable tasks and shed lights into an interpretation of the model’s thinking process.' metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}
page_content='Fig. 1. Overview of a LLM-powered autonomous agent system.\nComponent One: Planning#\nA complicated task usually involves many steps. An agent needs to know what they are and plan ahead.\nTask Decomposition#\nChain of thought (CoT; Wei et al. 2022) has become a standard prompting technique for enhancing model performance on complex tasks. The model is instructed to “think step by step” to utilize more test-time computation to decompose hard tasks into smaller and simpler steps. CoT transforms big tasks into multiple manageable tasks and shed lights into an interpretation of the model’s thinking process.' metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/', 'start_index': 1585}
page_content='Tree of Thoughts (Yao et al. 2023) extends CoT by exploring multiple reasoning possibilities at each step. It first decomposes the problem into multiple thought steps and generates multiple thoughts per step, creating a tree structure. The search process can be BFS (breadth-first search) or DFS (depth-first search) with each state evaluated by a classifier (via a prompt) or majority vote.\nTask decomposition can be done (1) by LLM with simple prompting like "Steps for XYZ.\\n1.", "What are the subgoals for achieving XYZ?", (2) by using task-specific instructions; e.g. "Write a story outline." for writing a novel, or (3) with human inputs.' metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/', 'start_index': 2192}
page_content='Tree of Thoughts (Yao et al. 2023) extends CoT by exploring multiple reasoning possibilities at each step. It first decomposes the problem into multiple thought steps and generates multiple thoughts per step, creating a tree structure. The search process can be BFS (breadth-first search) or DFS (depth-first search) with each state evaluated by a classifier (via a prompt) or majority vote.\nTask decomposition can be done (1) by LLM with simple prompting like "Steps for XYZ.\\n1.", "What are the subgoals for achieving XYZ?", (2) by using task-specific instructions; e.g. "Write a story outline." for writing a novel, or (3) with human inputs.' metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}
page_content='Resources:\n1. Internet access for searches and information gathering.\n2. Long Term memory management.\n3. GPT-3.5 powered Agents for delegation of simple tasks.\n4. File output.\n\nPerformance Evaluation:\n1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.\n2. Constructively self-criticize your big-picture behavior constantly.\n3. Reflect on past decisions and strategies to refine your approach.\n4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.' metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}
page_content='Resources:\n1. Internet access for searches and information gathering.\n2. Long Term memory management.\n3. GPT-3.5 powered Agents for delegation of simple tasks.\n4. File output.\n\nPerformance Evaluation:\n1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.\n2. Constructively self-criticize your big-picture behavior constantly.\n3. Reflect on past decisions and strategies to refine your approach.\n4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.' metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/', 'start_index': 29630}
深入
选择模型
ChatModel
:LLM支持的聊天模型。接收一系列消息并返回一条消息。
LLM
:文本输入文本输出LLM。接收一个字符串并返回一个字符串。
在此处查看带有本地运行模型的RAG指南。
自定义提示
如上所示,我们可以从提示符加载提示符(例如,RAG提示词)。提示也可以轻松自定义:
from langchain_core.prompts import PromptTemplate
template = """Use the following pieces of context to answer the question at the end.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
Use three sentences maximum and keep the answer as concise as possible.
Always say "thanks for asking!" at the end of the answer.
{context}
Question: {question}
Helpful Answer:"""
custom_rag_prompt = PromptTemplate.from_template(template)
rag_chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| custom_rag_prompt
| llm
| StrOutputParser()
)
rag_chain.invoke("What is Task Decomposition?")
'Task decomposition is the process of breaking down a complex task into smaller, more manageable parts. Techniques like Chain of Thought (CoT) and Tree of Thoughts allow an agent to "think step by step" and explore multiple reasoning possibilities, respectively. This process can be executed by a Language Model with simple prompts, task-specific instructions, or human inputs. Thanks for asking!'
后续步骤
我们已经介绍了基于数据构建基本问答应用程序的步骤:
- 使用Document Loader加载数据
- 使用Text Splitter将数据拆分为块,使其更容易被模型使用
- 嵌入数据并将数据存储在vectorstore
- 使用retriever检索相关数据块以响应传入的问题
- 使用检索到的块作为上下文生成答案
在上述每个部分中都有大量的特性、集成和扩展可供探索。除了上面提到的深入源代码之外,好的后续步骤包括:
- 返回源:了解如何返回源文档
- 流式传输:了解如何流式传输输出和中间步骤
- 添加聊天记录:了解如何将聊天记录添加到您的应用程序
- 检索概念指南:特定检索技术的高级概述
- 构建本地RAG应用程序:使用所有本地组件创建一个类似于上述应用程序的应用程序