1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| class AdaptiveRetrieval: """ 自适应检索策略 """
def __init__(self, vector_store, keyword_store, graph_store): self.stores = { 'vector': vector_store, 'keyword': keyword_store, 'graph': graph_store }
def retrieve(self, query: str, query_type: str = None) -> List[dict]: """ 根据查询类型自适应选择检索策略 """ if query_type is None: query_type = self._classify_query(query)
strategy = self._select_strategy(query_type)
results = [] for source, params in strategy.items(): source_results = self.stores[source].search( query, top_k=params['top_k'] ) results.extend(source_results)
return self._fusion_rerank(results, strategy)
def _classify_query(self, query: str) -> str: """分类查询类型""" prompt = f""" 查询:{query}
判断这个查询的类型: - factual: 事实性问题(如"什么是..."、"谁发明了...") - conceptual: 概念性问题(如"解释...原理") - relational: 关系性问题(如"...和...有什么关系") - procedural: 程序性问题(如"如何...") """ response = self.llm.generate(prompt) return response.strip().lower()
def _select_strategy(self, query_type: str) -> dict: """根据查询类型选择检索策略""" strategies = { 'factual': { 'vector': {'top_k': 10, 'weight': 0.7}, 'keyword': {'top_k': 5, 'weight': 0.3} }, 'conceptual': { 'vector': {'top_k': 15, 'weight': 0.8}, 'graph': {'top_k': 5, 'weight': 0.2} }, 'relational': { 'graph': {'top_k': 10, 'weight': 0.6}, 'vector': {'top_k': 10, 'weight': 0.4} }, 'procedural': { 'vector': {'top_k': 10, 'weight': 0.5}, 'keyword': {'top_k': 10, 'weight': 0.5} } } return strategies.get(query_type, strategies['factual'])
|
💬 互动讨论
欢迎留下你的见解、疑问或心得,精选评论有机会获得积分奖励哦!
使用 GitHub 账号登录评论 · 了解 Utterances
发现错误或有建议?提交反馈