马一丁

Normalize keyword type before matching

... ... @@ -136,6 +136,13 @@ class QueryEngine:
def _matches_keywords(self, node: Node, keywords: List[str]) -> bool:
"""检查节点是否匹配关键词"""
# 防御性检查:确保 keywords 为列表类型
# 若传入字符串,逐字符迭代会导致单字符匹配(如 'a', 'e'),污染结果
if isinstance(keywords, str):
keywords = [k.strip() for k in keywords.replace(',', ' ').split() if k.strip()]
elif not isinstance(keywords, list):
keywords = []
if not keywords:
# 无关键词时:只匹配 section 类型(避免返回整个图谱)
# 这样至少能获取到各引擎的段落摘要
... ...
... ... @@ -151,8 +151,16 @@ class GraphRAGQueryNode(BaseNode):
break
# 4. 执行查询:按 LLM 给出的参数查询本地图谱
# 规范化 keywords:确保为列表类型(LLM 可能返回字符串)
raw_keywords = decision.get('keywords', [])
if isinstance(raw_keywords, str):
# 按空格和逗号分割字符串为关键词列表
raw_keywords = [k.strip() for k in raw_keywords.replace(',', ' ').split() if k.strip()]
elif not isinstance(raw_keywords, list):
raw_keywords = []
params = QueryParams(
keywords=decision.get('keywords', []),
keywords=raw_keywords,
node_types=decision.get('node_types'),
engine_filter=decision.get('engine_filter'),
depth=decision.get('depth', 1)
... ...