公司动态
检索增强生成 (RAG)的原理——传统检索+LLM生成相结合
前言RAG是一种检索增强生成模型由信息检索系统和seq2seq生成器组成。它的内部知识可以轻松地随时更改或补充而无需浪费时间或算力重新训练整个模型。举个例子假设你正在写一篇关于猫的文章但你不确定如何描述猫的行为。你可以使用RAG来检索与猫行为相关的文档然后将这些文档作为上下文与原始输入拼接起来再输入到seq2seq模型中。这样RAG就可以生成关于猫行为的描述了.RAG 将检索模型与生成模型相结合检索模型充当 “图书馆员”扫描大型数据库以获取相关信息生成模型充当 “作家”将这些信息合成为与任务更相关的文本。 它用途广泛适用于实时新闻摘要、自动化客户服务和复杂研究任务等多种领域。RAG 需要检索模型例如跨嵌入的向量搜索与通常基于 LLMs 构建的生成模型相结合该模型能够将检索到的信息合成为有用的响应。检索增强生成 (RAG)通用语言模型通过微调就可以完成几类常见任务比如分析情绪和识别命名实体。这些任务不需要额外的背景知识就可以完成。要完成更复杂和知识密集型的任务可以基于语言模型构建一个系统访问外部知识源来做到。这样的实现与事实更加一性生成的答案更可靠还有助于缓解“幻觉”问题。Meta AI 的研究人员引入了一种叫做检索增强生成的方法来完成这类知识密集型的任务。RAG 把一个信息检索组件和文本生成模型结合在一起。RAG 可以微调其内部知识的修改方式很高效不需要对整个模型进行重新训练。RAG 会接受输入并检索出一组相关/支撑的文档并给出文档的来源例如维基百科。这些文档作为上下文和输入的原始提示词组合送给文本生成器得到最终的输出。这样 RAG 更加适应事实会随时间变化的情况。这非常有用因为 LLM 的参数化知识是静态的。RAG 让语言模型不用重新训练就能够获取最新的信息基于检索生成产生可靠的输出。Lewis 等人2021提出一个通用的 RAG 微调方法。这种方法使用预训练的 seq2seq 作为参数记忆用维基百科的密集向量索引作为非参数记忆使通过神经网络预训练的检索器访问。RAG 在 Natural Questions(opens in a new tab)、WebQuestions(opens in a new tab) 和 CuratedTrec 等基准测试中表现抢眼。用 MS-MARCO 和 Jeopardy 问题进行测试时RAG 生成的答案更符合事实、更具体、更多样。FEVER 事实验证使用 RAG 后也得到了更好的结果。这说明 RAG 是一种可行的方案能在知识密集型任务中增强语言模型的输出。最近基于检索器的方法越来越流行经常与 ChatGPT 等流行 LLM 结合使用来提高其能力和事实一致性。LangChain 文档中可以找到一个使用检索器和 LLM 回答问题并给出知识来源的简单例子(opens in a new tab)。如下Using a RetrieverThis example showcases question answering over an index.fromlangchain.chainsimportRetrievalQAfromlangchain.document_loadersimportTextLoaderfromlangchain.embeddings.openaiimportOpenAIEmbeddingsfromlangchain.llmsimportOpenAIfromlangchain.text_splitterimportCharacterTextSplitterfromlangchain.vectorstoresimportChromaloaderTextLoader(../../state_of_the_union.txt)documentsloader.load()text_splitterCharacterTextSplitter(chunk_size1000,chunk_overlap0)textstext_splitter.split_documents(documents)embeddingsOpenAIEmbeddings()docsearchChroma.from_documents(texts,embeddings)qaRetrievalQA.from_chain_type(llmOpenAI(),chain_typestuff,retrieverdocsearch.as_retriever())queryWhat did the president say about Ketanji Brown Jacksonqa.run(query) The president said that she is one of the nations top legal minds, a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers. He also said that she is a consensus builder and has received a broad range of support, from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.Chain TypeYou can easily specify different chain types to load and use in the RetrievalQA chain. For a more detailed walkthrough of these types, please see this notebook.There are two ways to load different chain types. First, you can specify the chain type argument in thefrom_chain_typemethod. This allows you to pass in the name of the chain type you want to use. For example, in the below we change the chain type tomap_reduce.qaRetrievalQA.from_chain_type(llmOpenAI(),chain_typemap_reduce,retrieverdocsearch.as_retriever())queryWhat did the president say about Ketanji Brown Jacksonqa.run(query) The president said that Judge Ketanji Brown Jackson is one of our nations top legal minds, a former top litigator in private practice and a former federal public defender, from a family of public school educators and police officers, a consensus builder and has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.The above way allows you to really simply change the chain_type, but it doesn’t provide a ton of flexibility over parameters to that chain type. If you want to control those parameters, you can load the chain directly (as you did in this notebook) and then pass that directly to the RetrievalQA chain with thecombine_documents_chainparameter. For example:fromlangchain.chains.question_answeringimportload_qa_chain qa_chainload_qa_chain(OpenAI(temperature0),chain_typestuff)qaRetrievalQA(combine_documents_chainqa_chain,retrieverdocsearch.as_retriever())queryWhat did the president say about Ketanji Brown Jacksonqa.run(query) The president said that Ketanji Brown Jackson is one of the nations top legal minds, a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers. He also said that she is a consensus builder and has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.Custom PromptsYou can pass in custom prompts to do question answering. These prompts are the same prompts as you can pass into the base question answering chainfromlangchain.promptsimportPromptTemplate prompt_templateUse the following pieces of context to answer the question at the end. If you dont know the answer, just say that you dont know, dont try to make up an answer. {context} Question: {question} Answer in Italian:PROMPTPromptTemplate(templateprompt_template,input_variables[context,question])chain_type_kwargs{prompt:PROMPT}qaRetrievalQA.from_chain_type(llmOpenAI(),chain_typestuff,retrieverdocsearch.as_retriever(),chain_type_kwargschain_type_kwargs)queryWhat did the president say about Ketanji Brown Jacksonqa.run(query) Il presidente ha detto che Ketanji Brown Jackson è una delle menti legali più importanti del paese, che continuerà leccellenza di Justice Breyer e che ha ricevuto un ampio sostegno, da Fraternal Order of Police a ex giudici nominati da democratici e repubblicani.Vectorstore Retriever OptionsYou can adjust how documents are retrieved from your vectorstore depending on the specific task.There are two main ways to retrieve documents relevant to a query- Similarity Search and Max Marginal Relevance Search (MMR Search). Similarity Search is the default, but you can use MMR by adding thesearch_typeparameter:docsearch.as_retriever(search_typemmr)You can also modify the search by passing specific search arguments through the retriever to the search function, using thesearch_kwargskeyword argument.kdefines how many documents are returned; defaults to 4.score_thresholdallows you to set a minimum relevance for documents returned by the retriever, if you are using the “similarity_score_threshold” search type.fetch_kdetermines the amount of documents to pass to the MMR algorithm; defaults to 20.lambda_multcontrols the diversity of results returned by the MMR algorithm, with 1 being minimum diversity and 0 being maximum. Defaults to 0.5.filterallows you to define a filter on what documents should be retrieved, based on the documents’ metadata. This has no effect if the Vectorstore doesn’t store any metadata.Some examples for how these parameters can be used:# Retrieve more documents with higher diversity- useful if your dataset has many similar documentsdocsearch.as_retriever(search_typemmr,search_kwargs{k:6,lambda_mult:0.25})# Fetch more documents for the MMR algorithm to consider, but only return the top 5docsearch.as_retriever(search_typemmr,search_kwargs{k:5,fetch_k:50})# Only retrieve documents that have a relevance score above a certain thresholddocsearch.as_retriever(search_typesimilarity_score_threshold,search_kwargs{score_threshold:0.8})# Only get the single most similar document from the datasetdocsearch.as_retriever(search_kwargs{k:1})# Use a filter to only retrieve documents from a specific paperdocsearch.as_retriever(search_kwargs{filter:{paper_title:GPT-4 Technical Report}})Return Source DocumentsAdditionally, we can return the source documents used to answer the question by specifying an optional parameter when constructing the chain.qaRetrievalQA.from_chain_type(llmOpenAI(),chain_typestuff,retrieverdocsearch.as_retriever(search_typemmr,search_kwargs{fetch_k:30}),return_source_documentsTrue)queryWhat did the president say about Ketanji Brown Jacksonresultqa({query:query})result[result] The president said that Ketanji Brown Jackson is one of the nations top legal minds, a former top litigator in private practice and a former federal public defender from a family of public school educators and police officers, and that she has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.result[source_documents][Document(page_contentTonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. \n\nTonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \n\nOne of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \n\nAnd I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.,lookup_str,metadata{source:../../state_of_the_union.txt},lookup_index0),Document(page_contentA former top litigator in private practice. A former federal public defender. And from a family of public school educators and police officers. A consensus builder. Since she’s been nominated, she’s received a broad range of support—from the Fraternal Order of Police to former judges appointed by Democrats and Republicans. \n\nAnd if we are to advance liberty and justice, we need to secure the Border and fix the immigration system. \n\nWe can do both. At our border, we’ve installed new technology like cutting-edge scanners to better detect drug smuggling. \n\nWe’ve set up joint patrols with Mexico and Guatemala to catch more human traffickers. \n\nWe’re putting in place dedicated immigration judges so families fleeing persecution and violence can have their cases heard faster. \n\nWe’re securing commitments and supporting partners in South and Central America to host more refugees and secure their own borders.,lookup_str,metadata{source:../../state_of_the_union.txt},lookup_index0),Document(page_contentAnd for our LGBTQ Americans, let’s finally get the bipartisan Equality Act to my desk. The onslaught of state laws targeting transgender Americans and their families is wrong. \n\nAs I said last year, especially to our younger transgender Americans, I will always have your back as your President, so you can be yourself and reach your God-given potential. \n\nWhile it often appears that we never agree, that isn’t true. I signed 80 bipartisan bills into law last year. From preventing government shutdowns to protecting Asian-Americans from still-too-common hate crimes to reforming military justice. \n\nAnd soon, we’ll strengthen the Violence Against Women Act that I first wrote three decades ago. It is important for us to show the nation that we can come together and do big things. \n\nSo tonight I’m offering a Unity Agenda for the Nation. Four big things we can do together. \n\nFirst, beat the opioid epidemic.,lookup_str,metadata{source:../../state_of_the_union.txt},lookup_index0),Document(page_contentTonight, I’m announcing a crackdown on these companies overcharging American businesses and consumers. \n\nAnd as Wall Street firms take over more nursing homes, quality in those homes has gone down and costs have gone up. \n\nThat ends on my watch. \n\nMedicare is going to set higher standards for nursing homes and make sure your loved ones get the care they deserve and expect. \n\nWe’ll also cut costs and keep the economy going strong by giving workers a fair shot, provide more training and apprenticeships, hire them based on their skills not degrees. \n\nLet’s pass the Paycheck Fairness Act and paid leave. \n\nRaise the minimum wage to $15 an hour and extend the Child Tax Credit, so no one has to raise a family in poverty. \n\nLet’s increase Pell Grants and increase our historic support of HBCUs, and invest in what Jill—our First Lady who teaches full-time—calls America’s best-kept secret: community colleges.,lookup_str,metadata{source:../../state_of_the_union.txt},lookup_index0)]Alternatively, if our document have a “source” metadata key, we can use theRetrievalQAWithSourcesChainto cite our sources:docsearchChroma.from_texts(texts,embeddings,metadatas[{source:f{i}-pl}foriinrange(len(texts))])fromlangchain.chainsimportRetrievalQAWithSourcesChainfromlangchain.llmsimportOpenAI chainRetrievalQAWithSourcesChain.from_chain_type(OpenAI(temperature0),chain_typestuff,retrieverdocsearch.as_retriever())chain({question:What did the president say about Justice Breyer},return_only_outputsTrue){answer: The president honored Justice Breyer for his service and mentioned his legacy of excellence.\n, sources: 31-pl}RAG 的关键组件了解 RAG 的内部工作原理需要深入研究它的两个基本元素检索模型和生成模型。 这两个组件是 RAG 卓越的获取、合成和生成信息丰富文本能力的基石。 让我们来分析一下每个模型带来的好处以及它们在 RAG 框架中带来的协同作用。检索模型检索模型充当 RAG 架构中的信息看门人。 它们的主要功能是搜索大量数据以查找可用于文本生成的相关信息。 将他们视为专业的图书馆员当你提出问题时他们确切地知道要从 “书架”上取下哪些 “书”。 这些模型使用算法来排序和选择最相关的数据提供了一种将外部知识引入文本生成过程的方法。 通过这样做检索模型为更明智、上下文丰富的语言生成奠定了基础从而提升了传统语言模型的能力。检索模型可以通过多种机制来实现。 最常见的技术之一是使用向量嵌入和向量搜索但也常用的是采用 BM25最佳匹配 25和 TF-IDF词频 - 逆文档频率等技术的文档索引数据库。生成模型一旦检索模型找到了适当的信息生成模型就开始发挥作用。 这些模型充当创意作家将检索到的信息合成为连贯且上下文相关的文本。 生成模型通常建立在 LLMs 的基础上能够创建语法正确、语义有意义且与初始查询或提示一致的文本。 他们采用检索模型选择的原始数据并赋予其叙述结构使信息易于理解和操作。 在 RAG 框架中生成模型是拼图的最后一块提供我们与之交互的文本输出。为生成式人工智能制作即时三明治为什么使用 RAG在不断发展的 NLP 领域人们一直在寻求更智能、上下文感知的系统。 这就是 RAG 发挥作用的地方它解决了传统生成模型的一些局限性。 那么是什么推动了 RAG 的日益普及呢首先RAG 提供了一种生成文本的解决方案该文本不仅流畅而且事实准确且信息丰富。 通过将检索模型与生成模型相结合RAG 确保其生成的文本既消息灵通又编写良好。 检索模型带来 “什么” —— 事实内容 —— 而生成模型则贡献 “如何” —— 将这些事实组成连贯且有意义的语言的艺术。其次RAG 的双重性质在需要外部知识或上下文理解的任务中提供了固有的优势。 例如在问答系统中传统的生成模型可能难以提供精确的答案。 相比之下RAG 可以通过其检索组件提取实时信息使其响应更加准确和详细。最后需要多步骤推理或综合各种来源信息的场景才是 RAG 真正的亮点。 想想法律研究、科学文献评论甚至复杂的客户服务查询。 RAG 搜索、选择和综合信息的能力使其在处理此类复杂任务方面无与伦比。总之RAG 的混合架构提供了卓越的文本生成功能使其成为需要深度、上下文和事实准确性的应用程序的理想选择。最后的最后感谢你们的阅读和喜欢作为一位在一线互联网行业奋斗多年的老兵我深知在这个瞬息万变的技术领域中持续学习和进步的重要性。为了帮助更多热爱技术、渴望成长的朋友我特别整理了一份涵盖大模型领域的宝贵资料集。这些资料不仅是我多年积累的心血结晶也是我在行业一线实战经验的总结。这些学习资料不仅深入浅出而且非常实用让大家系统而高效地掌握AI大模型的各个知识点。如果你愿意花时间沉下心来学习相信它们一定能为你提供实质性的帮助。这份完整版的大模型 AI 学习资料已经上传CSDN朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】大模型知识脑图为了成为更好的 AI大模型 开发者这里为大家提供了总的路线图。它的用处就在于你可以按照上面的知识点去找对应的学习资源保证自己学得较为全面。经典书籍阅读阅读AI大模型经典书籍可以帮助读者提高技术水平开拓视野掌握核心技术提高解决问题的能力同时也可以借鉴他人的经验。对于想要深入学习AI大模型开发的读者来说阅读经典书籍是非常有必要的。实战案例光学理论是没用的要学会跟着一起敲要动手实操才能将自己的所学运用到实际当中去这时候可以搞点实战案例来学习。面试资料我们学习AI大模型必然是想找到高薪的工作下面这些面试题都是总结当前最新、最热、最高频的面试题并且每道题都有详细的答案面试前刷完这套面试题资料小小offer不在话下640套AI大模型报告合集这套包含640份报告的合集涵盖了AI大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师还是对AI大模型感兴趣的爱好者这套报告合集都将为您提供宝贵的信息和启示。这份完整版的大模型 AI 学习资料已经上传CSDN朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】