2.2 检索策略 — 精准选择的艺术 本节导读:通过学习本节,读者将掌握如何根据不同的查询类型和场景特点,选择最优的检索策略,实现图检索与向量检索的智能化协同,显著提升知识问答的准确性和相关性。 学习目标 理解不同查询类型的特点和检索需求 掌握多种检索策略的设计原理 学会根据应用场景选择合适的检索方法 了解检索策略的性能优化技巧 核心概念 检索策略是LightRAG系统的大脑,它决定了如何协调图检索和向量检索两大核心组件,以最有效的方式响应用户查询。不同于传统单一检索方法,LightRAG的检索策略采用多维度分析和动态决策机制。
本节导读:通过学习本节,读者将掌握如何根据不同的查询类型和场景特点,选择最优的检索策略,实现图检索与向量检索的智能化协同,显著提升知识问答的准确性和相关性。
检索策略是LightRAG系统的大脑,它决定了如何协调图检索和向量检索两大核心组件,以最有效的方式响应用户查询。不同于传统单一检索方法,LightRAG的检索策略采用多维度分析和动态决策机制。
基于查询复杂度和知识需求的不同,LightRAG设计了四类核心检索策略:
# 检索策略核心依赖 from typing import List, Dict, Any from enum import Enum import numpy as np from dataclasses import dataclass import re from collections import defaultdict # 检索策略模式枚举 class QueryType(Enum): FACTUAL = "fact" # 事实型查询 REASONING = "reasoning" # 推理型查询 COMPREHENSIVE = "comprehensive" # 综合型查询 FUZZY = "fuzzy" # 模糊型查询 @dataclass class QueryAnalysis: query_type: QueryType confidence: float keywords: List[str] entities: List[str] complexity_score: float
def analyze_query_complexity(query: str) -> float: """分析查询复杂度""" complexity_factors = { 'question_words': ['为什么', '如何', '怎样', '解释', '分析', '比较'], 'length_factor': len(query) / 50, 'entity_count': len(re.findall(r'\b[A-Z][a-z]+\b', query)), 'negation_words': ['不是', '不', '没有', '除...外'] } # 计算复杂度分数 complexity = 0.1 * complexity_factors['length_factor'] # 问题词加分 for question_word in complexity_factors['question_words']: if question_word in query: complexity += 0.2 # 实体数量加分 complexity += 0.1 * min(complexity_factors['entity_count'], 3) # 否定词增加复杂度 for negation_word in complexity_factors['negation_words']: if negation_word in query: complexity += 0.15 return min(complexity, 1.0)
class QueryClassifier: """查询分类器:识别用户查询的意图类型""" def __init__(self): self.patterns = { QueryType.FACTUAL: [ r'什么是', r'(.*)是什么', r'(.*)的定义', r'(.*)的概念', r'(.*)的特点', r'(.*)的优势', r'(.*)的缺点' ], QueryType.REASONING: [ r'为什么(.*)', r'如何(.*)', r'怎样(.*)', r'分析(.*)', r'比较(.*)和(.*)', r'(.*)的区别', r'(.*)的关系', r'推导(.*)' ], QueryType.COMPREHENSIVE: [ r'总结(.*)', r'概述(.*)', r'全面介绍(.*)', r'系统讲解(.*)', r'(.*)的各个方面' ], QueryType.FUZZY: [ r'(.*)相关', r'关于(.*)', r'(.*)相关内容', r'了解(.*)', r'(.*)信息' ] } def classify_query(self, query: str) -> QueryAnalysis: """分类用户查询""" # 1. 关键词提取 keywords = self._extract_keywords(query) entities = self._extract_entities(query) # 2. 计算复杂度 complexity = analyze_query_complexity(query) # 3. 模式匹配 query_type, confidence = self._match_patterns(query) # 4. 实体识别 entity_confidence = len(entities) / max(1, len(keywords)) return QueryAnalysis( query_type=query_type, confidence=confidence, keywords=keywords, entities=entities, complexity_score=complexity ) def _extract_keywords(self, query: str) -> List[str]: """提取关键词""" stop_words = {'的', '了', '是', '在', '我', '有', '和', '就', '不', '人', '都', '一', '个', '上', '也', '很', '到', '说', '要', '去', '你', '会', '着', '没有', '看', '好', '自己', '这'} words = re.findall(r'[\u4e00-\u9fff]+|[a-zA-Z]+', query) keywords = [word for word in words if len(word) > 1 and word not in stop_words] return keywords def _extract_entities(self, query: str) -> List[str]: """提取实体""" entities = re.findall(r'\b[A-Z][a-z]+(?:\s+[A-Z][a-z]+)*', query) return entities def _match_patterns(self, query: str) -> tuple: """模式匹配查询类型""" best_match = None best_confidence = 0.0 for query_type, patterns in self.patterns.items(): for pattern in patterns: match = re.search(pattern, query, re.IGNORECASE) if match: confidence = self._calculate_pattern_confidence(pattern, match, query) if confidence > best_confidence: best_confidence = confidence best_match = query_type # 如果没有匹配到模式,根据复杂度默认分类 if best_match is None: if analyze_query_complexity(query) > 0.5: best_match = QueryType.REASONING else: best_match = QueryType.FACTUAL best_confidence = 0.5 return best_match, best_confidence
class RetrievalStrategyManager: """检索策略管理器:根据查询类型选择最优检索策略""" def __init__(self, retriever): self.retriever = retriever self.strategies = { QueryType.FACTUAL: FactualRetrievalStrategy(), QueryType.REASONING: ReasoningRetrievalStrategy(), QueryType.COMPREHENSIVE: ComprehensiveRetrievalStrategy(), QueryType.FUZZY: FuzzyRetrievalStrategy() } def execute_strategy(self, query: str) -> List[Dict[str, Any]]: """根据查询类型执行相应策略""" # 1. 查询分类 query_analysis = self.classifier.classify_query(query) # 2. 选择策略 strategy = self.strategies[query_analysis.query_type] # 3. 执行检索 results = strategy.execute_retrieval(query, query_analysis, self.retriever) return results # 具体检索策略实现 class FactualRetrievalStrategy: """事实型检索策略:精准匹配最相关内容""" def execute_retrieval(self, query: str, analysis: QueryAnalysis, retriever) -> List[Dict[str, Any]]: """执行事实型检索""" logger.info(f"使用事实型检索策略: {query}") # 1. 向量检索为主,高精度 vector_results = retriever.vector_layer.similarity_search(query, top_k=8, method="semantic") # 2. 关键词精炼 keyword_results = self._keyword_refinement(query, vector_results, analysis) # 3. 排序和筛选 results = self._rank_and_filter(keyword_results, analysis) return results[:10] def _keyword_refinement(self, query: str, results: List[Dict], analysis: QueryAnalysis) -> List[Dict]: """关键词精炼""" keywords = analysis.keywords refined_results = [] for result in results: # 计算关键词匹配度 content = result['document']['content'] keyword_matches = sum(1 for keyword in keywords if keyword in content) match_ratio = keyword_matches / max(1, len(keywords)) # 添加匹配度分数 result['keyword_match_ratio'] = match_ratio refined_results.append(result) # 按关键词匹配度调整分数 for result in refined_results: result['adjusted_score'] = result['similarity'] * (0.7 + 0.3 * result['keyword_match_ratio']) return refined_results def _rank_and_filter(self, results: List[Dict], analysis: QueryAnalysis) -> List[Dict]: """排序和筛选结果""" # 综合评分 = 相似度 + 关键词匹配度 + 实体匹配度 for result in results: result['final_score'] = ( result['adjusted_score'] * 0.4 + result['similarity'] * 0.3 + (1.0 if analysis.entities and any(entity in result['document']['content'] for entity in analysis.entities) else 0.5) * 0.3 ) # 按最终分数排序 results.sort(key=lambda x: x['final_score'], reverse=True) return results class ReasoningRetrievalStrategy: """推理型检索策略:基于图谱的深度推理""" def execute_retrieval(self, query: str, analysis: QueryAnalysis, retriever) -> List[Dict[str, Any]]: """执行推理型检索""" logger.info(f"使用推理型检索策略: {query}") # 1. 图谱推理 graph_results = retriever.graph_layer.graph_reasoning(query, max_depth=3) graph_context = self._build_reasoning_context(graph_results) # 2. 向量辅助检索 vector_results = retriever.vector_layer.similarity_search(query, top_k=5) # 3. 融合推理结果 results = self._fuse_reasoning_results(graph_context, vector_results) return results[:8] class ComprehensiveRetrievalStrategy: """综合型检索策略:全面覆盖和深度挖掘""" def execute_retrieval(self, query: str, analysis: QueryAnalysis, retriever) -> List[Dict[str, Any]]: """执行综合型检索""" logger.info(f"使用综合型检索策略: {query}") # 1. 多角度检索 results = [] # 实体角度检索 if analysis.entities: entity_results = self._entity_oriented_search(analysis.entities, retriever) results.extend(entity_results) # 关键词角度检索 keyword_results = self._keyword_oriented_search(analysis.keywords, retriever) results.extend(keyword_results) # 语义角度检索 semantic_results = retriever.vector_layer.similarity_search(query, top_k=10) results.extend(semantic_results) # 2. 结果去重和融合 unique_results = self._deduplicate_and_fuse(results) # 3. 多维度评分 scored_results = self._multi_dimensional_scoring(unique_results, analysis) return scored_results[:15] class FuzzyRetrievalStrategy: """模糊检索策略:语义扩展和上下文理解""" def execute_retrieval(self, query: str, analysis: QueryAnalysis, retriever) -> List[Dict[str, Any]]: """执行模糊检索""" logger.info(f"使用模糊检索策略: {query}") # 1. 查询扩展 expanded_queries = self._query_expansion(query, analysis) # 2. 多查询检索 all_results = [] for expanded_query in expanded_queries: results = retriever.vector_layer.similarity_search(expanded_query, top_k=8) all_results.extend(results) # 3. 结果融合和重排序 fused_results = self._fuse_expanded_results(all_results, expanded_queries) return fused_results[:12]
class StrategyOptimizer: """策略优化器:动态调整检索策略参数""" def __init__(self): self.performance_metrics = { 'retrieval_time': [], 'result_quality': [], 'user_satisfaction': [] } self.strategy_configs = {} def optimize_strategy_parameters(self, strategy_type: QueryType, query_history: List[Dict]) -> Dict[str, Any]: """优化策略参数""" # 分析查询历史 avg_complexity = np.mean([q['complexity'] for q in query_history]) avg_response_time = np.mean([q['response_time'] for q in query_history]) # 根据性能调整参数 if avg_response_time > 5.0: # 响应时间过长 return { 'top_k_reduction': max(5, self.strategy_configs.get('top_k', 10) - 2), 'max_depth_reduction': max(2, self.strategy_configs.get('max_depth', 3) - 1), 'cache_enabled': True } elif avg_complexity < 0.3: # 简单查询居多 return { 'top_k_increase': min(15, self.strategy_configs.get('top_k', 10) + 3), 'semantic_boost': 1.1 } else: return { 'balance_mode': True, 'adaptive_threshold': 0.7 } def evaluate_strategy_performance(self, results: List[Dict], ground_truth: List[str]) -> Dict[str, float]: """评估策略性能""" # 计算命中率 hit_documents = set() for result in results[:5]: # 前5个结果 doc_id = result.get('document', {}).get('id') if doc_id in ground_truth: hit_documents.add(doc_id) hit_rate = len(hit_documents) / len(ground_truth) if ground_truth else 0 # 计算平均相关性 relevance_scores = [] for result in results[:5]: relevance = result.get('relevance', 0) relevance_scores.append(relevance) avg_relevance = np.mean(relevance_scores) if relevance_scores else 0 return { 'hit_rate': hit_rate, 'avg_relevance': avg_relevance, 'result_diversity': self._calculate_diversity(results) }
# 完整的检索策略示例 class RetrievalStrategyDemo: """检索策略演示""" def __init__(self): self.retriever = None self.strategy_manager = None self.optimizer = StrategyOptimizer() def setup_system(self): """设置检索系统""" # 初始化LightRAG检索引擎 self.setup_light_rag() # 设置策略管理器 self.strategy_manager = RetrievalStrategyManager(self.retriever) logger.info("检索策略系统设置完成") def setup_light_rag(self): """初始化LightRAG检索引擎""" # 模拟文档数据 self.documents = [ { 'id': 1, 'content': '人工智能(Artificial Intelligence,简称AI)是计算机科学的一个分支,致力于创造能够执行通常需要人类智能的任务的系统。', 'title': '人工智能基础概念' }, { 'id': 2, 'content': '机器学习是人工智能的一个重要子领域,它使计算机能够从数据中学习并改进性能,而无需明确编程。', 'title': '机器学习原理' }, { 'id': 3, 'content': '深度学习使用神经网络来模拟人脑的工作方式,在图像识别、自然语言处理等领域取得了突破性进展。', 'title': '深度学习技术' }, { 'id': 4, 'content': '知识图谱是一种结构化的语义知识库,用于以图形化的方式表示实体及其之间的关系。', 'title': '知识图谱基础' }, { 'id': 5, 'content': '向量检索基于语义相似度,通过将文本转换为向量表示来实现快速的内容匹配和检索。', 'title': '向量检索方法' } ] # 初始化检索引擎 llm_client = MockLLMClient() self.retriever = LightRAGRetriever(llm_client) self.retriever.setup(self.documents) def demonstrate_strategies(self): """演示不同检索策略""" print("=== 检索策略演示 ===") test_queries = [ { 'query': '什么是人工智能', 'expected_type': QueryType.FACTUAL, 'ground_truth': [1] }, { 'query': '为什么深度学习重要', 'expected_type': QueryType.REASONING, 'ground_truth': [3] }, { 'query': '机器学习和深度学习的关系', 'expected_type': QueryType.COMPREHENSIVE, 'ground_truth': [2, 3] }, { 'query': 'AI相关内容', 'expected_type': QueryType.FUZZY, 'ground_truth': [1, 2, 3] } ] for test_case in test_queries: print(f"\n查询: {test_case['query']}") # 执行检索 start_time = time.time() results = self.strategy_manager.execute_strategy(test_case['query']) end_time = time.time() # 评估性能 performance = self.optimizer.evaluate_strategy_performance( results, test_case['ground_truth'] ) print(f"响应时间: {end_time - start_time:.2f}秒") print(f"命中率: {performance['hit_rate']:.2f}") print(f"平均相关性: {performance['avg_relevance']:.2f}") print(f"结果多样性: {performance['result_diversity']:.2f}") # 显示前3个结果 for i, result in enumerate(results[:3], 1): print(f"{i}. [{result.get('source', 'unknown')}]: {result['content'][:50]}...") # 使用示例 if __name__ == "__main__": demo = RetrievalStrategyDemo() demo.setup_system() demo.demonstrate_strategies()
A:查询类型对检索效果的影响主要体现在以下几个方面:
优化建议:
A:多语言检索策略优化需要考虑以下因素:
A:处理实时知识更新需要以下机制:
A:用户满意度评估应包含多个维度:
A:应对恶意查询需要多层防护:
不要一次性大幅度调整策略参数,应该采用渐进式优化:
过度关注特定查询类型会导致整体性能下降。建议:
建立完善的监控体系,包括:
新系统或新功能上线时,缺乏足够的数据进行优化。解决方案:
本节详细介绍了LightRAG检索策略的设计与实现,包括:
通过本节的学习,读者应该能够:
下一节将介绍融合算法的设计与实现,探讨如何将图检索和向量检索的结果进行有效融合,进一步提升检索质量。
关键词:检索策略, 查询分类, 图检索, 向量检索, 自适应优化, 性能评估, 意图识别, 多策略融合
难度:进阶
预计阅读:50 分钟