|
| 1 | +--- |
| 2 | +sidebar_position: 7 |
| 3 | +title: 检索增强 |
| 4 | +--- |
| 5 | + |
| 6 | +检索增强生成(RAG)是一种通过结合实时数据检索来增强大语言模型文本生成的技术。它允许模型在生成过程中搜索外部数据库,产生更准确和最新的响应。 |
| 7 | + |
| 8 | +## 工作原理 |
| 9 | + |
| 10 | +1. **检索**:根据查询从外部源获取相关数据 |
| 11 | +2. **生成**:处理检索到的数据并生成响应 |
| 12 | + |
| 13 | +传统的向量数据库的工作原理是: |
| 14 | +1. 将待检索的文档提前转换为向量,存储到向量数据库中 |
| 15 | +2. 当用户提问时,大模型将用户的问题转换为向量,在向量数据库中检索最相似的向量 |
| 16 | +3. 向量数据库返回最相似的向量对应的文档 |
| 17 | +4. 大模型将文档内容与用户问题拼接,生成回答 |
| 18 | + |
| 19 | +其中主要的调优手段是: |
| 20 | +1. chunking:将文档拆分为多个小块,基于段落、特定规则、字数等、每个chunk之间有重叠,避免关键词命中重叠部分。 |
| 21 | +2. embedding:将文档转换为向量,使用相似度搜索,返回最相似的向量对应的文档。不同的embedding模型效果不同。 |
| 22 | + |
| 23 | + |
| 24 | +向量相似度搜索基于语义相似性,但语义相似性不等于相关。为了解决传统数据库返回结果不准确的问题,我们引入了ranking重新排序机制。 |
| 25 | + |
| 26 | +为了解决传统向量数据量不能关联较远知识的问题,我们引入了知识图谱型数据库。知识图谱型数据库的工作原理是利用现有的知识库,通过图数据库的查询语言,查询与用户问题相关的知识。生成较慢,但是效果有一定的提升。 |
| 27 | + |
| 28 | +同时为了解决只能匹配文本的问题,我们引入了多模态数据库。embedding模型可以处理图片、音频、视频等多种模态的数据。 |
| 29 | + |
| 30 | +知识图谱好但是慢、传统的向量数据库快但是不好,可以考虑使用混合检索。同时让他们返回搜索结果。这样知识图谱重新生成期间也可以提供服务。 |
| 31 | + |
| 32 | +以上都是传统的解决方案,但是随着AI的发展,我们引入了Agent检索。Agent检索通过智能代理来优化检索过程: |
| 33 | + |
| 34 | +## Agent检索 |
| 35 | + |
| 36 | +### 普通Agent检索 |
| 37 | + |
| 38 | +普通Agent检索通过智能代理来优化检索过程: |
| 39 | + |
| 40 | +1. **查询理解**:Agent分析用户问题,提取关键信息和意图 |
| 41 | +2. **检索策略选择**:根据问题类型选择最适合的检索方法(向量搜索、关键词搜索、混合搜索) |
| 42 | +3. **结果过滤**:Agent对检索结果进行初步筛选和排序 |
| 43 | +4. **上下文整合**:将多个检索结果整合为连贯的上下文 |
| 44 | + |
| 45 | +```python showLineNumbers |
| 46 | +# 普通Agent检索示例 |
| 47 | +class RetrievalAgent: |
| 48 | + def __init__(self): |
| 49 | + self.vector_db = VectorDatabase() |
| 50 | + self.knowledge_graph = KnowledgeGraph() |
| 51 | + |
| 52 | + def retrieve(self, query): |
| 53 | + # 1. 查询理解 |
| 54 | + intent = self.analyze_intent(query) |
| 55 | + |
| 56 | + # 2. 选择检索策略 |
| 57 | + if intent == "factual": |
| 58 | + results = self.knowledge_graph.search(query) |
| 59 | + elif intent == "semantic": |
| 60 | + results = self.vector_db.search(query) |
| 61 | + else: |
| 62 | + results = self.hybrid_search(query) |
| 63 | + |
| 64 | + # 3. 结果过滤和排序 |
| 65 | + filtered_results = self.filter_results(results, query) |
| 66 | + return filtered_results |
| 67 | +``` |
| 68 | + |
| 69 | +### 多Agent复合检索 |
| 70 | + |
| 71 | +多Agent复合检索使用多个专业Agent协同工作,每个Agent负责不同的检索任务: |
| 72 | + |
| 73 | +1. **查询分解Agent**:将复杂问题分解为多个子问题 |
| 74 | +2. **专业检索Agent**:针对不同领域进行专业检索 |
| 75 | + - 文本检索Agent |
| 76 | + - 图像检索Agent |
| 77 | + - 知识图谱检索Agent |
| 78 | + - 实时数据检索Agent |
| 79 | +3. **结果融合Agent**:整合多个Agent的检索结果 |
| 80 | +4. **质量评估Agent**:评估检索结果的质量和相关性 |
| 81 | + |
| 82 | +```python showLineNumbers |
| 83 | +# 多Agent复合检索示例 |
| 84 | +class MultiAgentRetrieval: |
| 85 | + def __init__(self): |
| 86 | + self.query_agent = QueryDecompositionAgent() |
| 87 | + self.text_agent = TextRetrievalAgent() |
| 88 | + self.image_agent = ImageRetrievalAgent() |
| 89 | + self.graph_agent = GraphRetrievalAgent() |
| 90 | + self.fusion_agent = ResultFusionAgent() |
| 91 | + self.quality_agent = QualityAssessmentAgent() |
| 92 | + |
| 93 | + def retrieve(self, query): |
| 94 | + # 1. 查询分解 |
| 95 | + sub_queries = self.query_agent.decompose(query) |
| 96 | + |
| 97 | + # 2. 并行检索 |
| 98 | + results = [] |
| 99 | + for sub_query in sub_queries: |
| 100 | + text_results = self.text_agent.search(sub_query) |
| 101 | + image_results = self.image_agent.search(sub_query) |
| 102 | + graph_results = self.graph_agent.search(sub_query) |
| 103 | + |
| 104 | + results.extend([text_results, image_results, graph_results]) |
| 105 | + |
| 106 | + # 3. 结果融合 |
| 107 | + fused_results = self.fusion_agent.merge(results) |
| 108 | + |
| 109 | + # 4. 质量评估 |
| 110 | + final_results = self.quality_agent.rank(fused_results, query) |
| 111 | + |
| 112 | + return final_results |
| 113 | +``` |
| 114 | + |
| 115 | +### Agent检索的优势 |
| 116 | + |
| 117 | +1. **智能决策**:Agent可以根据问题特点选择最佳检索策略 |
| 118 | +2. **并行处理**:多Agent可以同时处理不同类型的检索任务 |
| 119 | +3. **质量保证**:通过多轮验证和评估提高检索质量 |
| 120 | +4. **自适应优化**:Agent可以学习用户偏好,持续优化检索效果 |
| 121 | +5. **复杂查询处理**:能够处理需要多步骤推理的复杂查询 |
0 commit comments