ingFang SC";font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">检索增强生成(RAG)技术通过结合大型语言模型(LLMs)与外部数据检索能力,能够提供准确且富含上下文的答案。无论是构建客户支持聊天机器人还是研究助手,RAG都能通过从数据库中提取相关信息来增强AI的性能。然而,不同的RAG技术在性能上存在差异,选择最佳技术需要进行测试。本文将全面探讨各类RAG技术,包括基础方法和高级手段,并对它们进行评估,以帮助您根据需求选择最合适的技术。ingFang SC";font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">什么是RAG及其测试的重要性ingFang SC";font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">想象一下,向AI提问:“如何解决智能手机上的登录问题?”标准的大型语言模型可能会给出通用或过时的回答。而RAG技术通过检索相关数据(例如来自产品手册)并将其输入到大型语言模型中,从而生成精确的答案,以此改善了这一情况。这使得RAG非常适用于需要最新或特定信息的应用场景,如技术支持或医疗咨询。ingFang SC";font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">测试RAG技术至关重要,因为每种方法在文档索引、检索或生成的方式上各有不同,会对准确性、速度和成本产生影响。通过比较这些技术,我们可以为特定的使用场景找到最佳设置。ingFang SC";font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">RAG系统的核心组件ingFang SC";font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">RAG流程主要包含三个阶段:ingFang SC";font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;" class="list-paddingleft-1">索引:将文档分割成块,将其转换为数值向量(嵌入),并存储在向量数据库中。检索生成:使用大型语言模型将检索到的数据与查询结合起来生成答案。ingFang SC";font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">RAG技术列表ingFang SC";font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">以下是我们将要比较的主要RAG技术: ingFang SC";font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;" class="list-paddingleft-1">1. 朴素RAG(基准)这是最简单的方法:将文档分割成固定大小的块,对其进行嵌入处理(例如使用BERT),存储在向量数据库(例如FAISS)中,并根据查询相似度检索前k个块。 示例:对于查询“如何解决智能手机上的登录问题?”,朴素RAG将手册分割成500字的块,并检索前5个块。 代码片段(使用LlamaIndex): from llama_index import VectorStoreIndex, SimpleDirectoryReader from llama_index.llms import OpenAI documents = SimpleDirectoryReader("manuals/").load_data() index = VectorStoreIndex.from_documents(documents) query_engine = index.as_query_engine() response = query_engine.query("How do I fix a login issue on my smartphone?") print(response)
输出:“要解决登录问题,请尝试通过‘忘记密码’链接重置密码或清除设置中的应用缓存。” 优点:简单、快速。 缺点:可能会遗漏上下文或检索到不相关的块。 2. 分块策略分块会影响检索质量。分块方式包括: 示例:基于句子的分块将登录查询的故障排除部分完整保留。 代码片段(语义分块): from llama_index import SemanticSplitter splitter = SemanticSplitter(chunk_size=512) chunks = splitter.split_documents(documents) index = VectorStoreIndex.from_documents(chunks) response = index.as_query_engine().query("How do I fix a login issue?") print(response)
输出:“通过设置>应用清除缓存。如果问题仍然存在,请重置密码。” 优点:更好地保留上下文。 缺点:语义分块计算密集。 3. 重排序重排序使用交叉编码器或大型语言模型对检索到的块进行重新排序,以对查询-块相关性进行评分。 示例:重排序会优先考虑有关登录故障排除的块,而不是通用设置的块。 代码片段(使用Cohere): from cohere import Client cohere_client = Client("YOUR_COHERE_API_KEY") chunks = index.retrieve("How do I fix a login issue?") reranked = cohere_client.rerank(query="How do I fix a login issue?", documents=[chunk.text for chunk in chunks]) top_chunks = [chunks[i] for i in reranked.results[:5]]
输出:“清除缓存;使用‘忘记密码’链接。” 优点:提高相关性。 缺点:增加计算开销。 4. 查询转换增强查询以改进检索: - 查询重写:重新表述模糊的查询(例如将“登录问题”改为“解决智能手机登录失败”)。
- HyDE(假设文档嵌入):创建用于检索的假设答案。
示例:HyDE生成类似“清除缓存或重置密码”的假答案,并检索匹配的块。
代码片段(HyDE): from llama_index import HyDEQueryTransform hyde = HyDEQueryTransform() transformed_query = hyde.transform("How do I fix a login issue?") response = index.as_query_engine().query(transformed_query) print(response)
输出:“检查设置>应用>清除缓存;重置密码。” 优点:捕捉细微的意图。 缺点:增加延迟。 5. 图RAG(GraphRAG)使用知识图谱捕获实体关系,适用于复杂查询。 示例:对于“什么导致登录问题?”,图RAG检索与“应用缓存”和“用户凭据”相关联的块。 工作流程: 优点:擅长多步骤推理。 缺点:设置复杂。 6. RAG融合(RAG-Fusion)将查询扩展到多个视角,执行并行搜索,并对结果进行重排序。 示例:生成“清除应用缓存”、“重置密码”等查询,并检索不同的块。 优点:覆盖全面。 缺点:计算成本高。 7. 上下文压缩压缩检索到的块,专注于最相关的部分,在生成前减少噪音。 示例:对于登录查询,压缩从较长的块中仅提取有关缓存清除和密码重置的句子。 代码片段(使用LlamaIndex): from llama_index import ContextualCompressionRetriever retriever = ContextualCompressionRetriever(base_retriever=index.as_retriever()) response = retriever.query("How do I fix a login issue?") print(response)
输出:“通过设置>应用清除应用缓存;通过‘忘记密码’重置密码。” 优点:减少不相关内容。 缺点:需要额外处理。 8. 自查询检索使用大型语言模型将复杂查询解析为结构化数据库查询,适用于富含元数据的数据集。 示例:对于“显示2025年Android设备的登录问题”,大型语言模型生成一个过滤Android和2025年元数据的查询。 代码片段: from llama_index import SelfQueryRetriever retriever = SelfQueryRetriever(index=index, metadata_fields=["device_type", "year"]) response = retriever.query("Show login issues for Android devices in 2025") print(response)
输出:“对于2025年的Android设备,通过设置>应用清除缓存或重置密码。” 优点:处理复杂的结构化查询。 缺点:需要富含元数据的数据。 9. 多文档代理使用多个代理,每个代理专门处理文档的子集,以处理不同的数据源。 示例:一个代理处理Android手册,另一个处理iOS手册,将结果结合起来回答登录查询。 代码片段: from llama_index import MultiDocumentAgent agents = [VectorStoreIndex.from_documents(docs).as_query_engine() for docs in [android_docs, ios_docs]] agent = MultiDocumentAgent(agents) response = agent.query("How do I fix a login issue?") print(response)
输出:“在Android上,清除缓存;在iOS上,通过设置重置密码。” 优点:可扩展到不同的数据集。 缺点:协调复杂。 10. 动态检索根据查询复杂度动态调整检索参数(例如top-k、相似度阈值)。 示例:简单查询使用前3个块;复杂查询使用前10个块。 代码片段: from llama_index import DynamicRetriever retriever = DynamicRetriever(index=index, complexity_estimator=lambda q: len(q.split())) response = retriever.query("How do I fix a login issue?") print(response)
输出:“清除缓存或重置密码。” 优点:适应查询需求。 缺点:需要调优。 11. 递归检索通过基于初始结果生成后续查询来迭代优化检索。 示例:在检索到有关“登录问题”的块后,查询“缓存清除步骤”以加深上下文。 代码片段: from llama_index import RecursiveRetriever retriever = RecursiveRetriever(base_retriever=index.as_retriever()) response = retriever.query("How do I fix a login issue?") print(response)
输出:“通过设置>应用清除缓存;详细步骤:进入设置,选择应用,选择该应用,然后清除缓存。” 优点:为复杂查询加深上下文。 缺点:增加延迟。 12. 父文档检索检索较小的块,但返回其父文档,以便在生成过程中获得更丰富的上下文。 示例:检索有关登录问题的句子,但向大型语言模型提供完整的故障排除部分。 代码片段: from llama_index import ParentDocumentRetriever retriever = ParentDocumentRetriever(index=index, parent_splitter=lambda x: x.split("\n\n")) response = retriever.query("How do I fix a login issue?") print(response)
输出:“在设置>应用中清除缓存;必要时重置密码。完整部分:[详细故障排除指南]。” 优点:平衡精度和上下文。 缺点:需要分层文档结构。 测试与评估为了找到最佳技术,我们将使用智能手机手册数据集对所有12种技术进行测试,评估指标包括: 演示设置我们将使用LlamaIndex、OpenAI的GPT-3.5、FAISS和Cohere进行重排序。查询为“如何解决智能手机上的登录问题?” 分析- HyDE和递归检索在精度和答案质量上得分最高,但速度较慢。
- 多文档代理在处理不同数据集时表现出色,但速度最慢。
选择最佳技术最佳技术取决于您的优先级: 示例场景:对于客户支持聊天机器人,重排序或父文档检索提供了良好的平衡。对于具有复杂查询的研究工具,图RAG或递归检索是理想的。 挑战与提示- 幻觉:使用清晰的提示使大型语言模型基于检索到的数据生成内容。
- 可扩展性:使用FAISS或FalkorDB等向量数据库。
总结一下对广泛的RAG技术的探索表明,每种技术都有其优缺点。朴素RAG是一个很好的起点,因为它简单且设置快速,非常适合快速项目。另一方面,像HyDE、图RAG、递归检索等高级方法在需要更准确和详细的答案时表现出色,特别是对于复杂或特定的任务。通过测试这些方法,人们可以找出最适合自己需求的方法,无论优先考虑的是速度、精度还是处理棘手问题的能力。 |