Skip to content

fix(knowledge-graph): 修复 KG 全局问答查询侧模型选错 + 凭证缺失 + 不可重试错误盲目重试 - #538

Merged
ThreeFish-AI merged 2 commits into
feature/1.x.xfrom
ThreeFish-AI/kg-global-qa-errors
May 13, 2026
Merged

fix(knowledge-graph): 修复 KG 全局问答查询侧模型选错 + 凭证缺失 + 不可重试错误盲目重试#538
ThreeFish-AI merged 2 commits into
feature/1.x.xfrom
ThreeFish-AI/kg-global-qa-errors

Conversation

@ThreeFish-AI

Copy link
Copy Markdown
Owner

背景

  • 本次变更要解决的问题:Knowledge Graph 全局问答(Global Search)在执行时后台暴露四项级联缺陷——查询侧 embedding 模型与语料库实际绑定模型脱钩(使用了硬编码默认 gemini/text-embedding-004 而非 corpus.config 中的模型)、completion 调用回退到无凭证的硬编码 openai/gpt-5-mini、不可重试错误(AuthenticationError / NotFoundError)仍执行完整 3 次指数退避、零证据时把基础设施故障伪装成内容缺失。端到端一次请求耗时 ~8.8s 且 evidence=0。
  • 关联上下文/Issue/文档:ISSUE-086;前序修复 commit 809c24d 已修正 ingestion 侧 embedding 路由但遗漏了查询侧。

核心变更

  • 查询侧模型注入:新增 _resolve_corpus_model_ids 共享 helper(api_helpers.py),从 corpus.config['models'] 解出 embedding_config_id / llm_config_idglobal_search_knowledge_graph / search_knowledge_graph / multi_hop_reasoning fallback 三处端点统一注入,与 ingestion 端同一向量空间。
  • GlobalSearchService 凭证透传:构造函数新增 llm_config_id 参数;_call_llm 重写为三级优先级解析(resolve_llm_config_by_id > resolve_llm_config > get_fallback_llm_config)+ extra_kwargs 透传到 call_llm_with_retry,对齐 community_summarizer._call_llm 既定模式。
  • 重试层 fail-fastextractors.py 新增 _is_non_retryable_error 双层兜底 helper(litellm exceptions isinstance + 字符串模式);call_llm_with_retry / embedding.py::_call_with_retry 在凭证/路由/参数类终态错误时立即放弃,避免 N×退避空转。
  • 零证据语义二分:候选 > 0 但全部 map 失败时 answer 改为「全局检索失败 + 基础设施排查指引」,杜绝伪装成内容缺失。

风险与回滚

  • 主要风险:语料库无自定义 config.models 时 helper 返回 (None, None)build_embedding_fn(None) 等价于修复前行为——向后兼容,风险极低。
  • 回滚方式:git revert 两次提交即可;所有变更限定在 5 个 src 文件 + 2 个 test 文件 + 1 个 doc 文件。

验证证据

  • 单元测试:test_global_search.py 新增 3 个用例(llm_config_id 路由 / 默认 fallback / 零证据 infrastructure-error 文案);test_embedding.py 新增 TestNonRetryableFailFast 4 个用例(Auth/NotFound/文本兜底/transient 仍重试)。
  • 集成测试:tests/unit_tests/knowledge/ 全量 816 项断言通过(pre-existing test_extraction_llm_plan 失败已 git stash 对照确认与本次修复无关)。
  • E2E/Workflow:待用户在自有 Chrome 主 profile + 真实语料库上端到端复测(agent 上下文 chrome_devtools 通道被占用且不应启用 sandbox profile)。
  • 覆盖率/关键截图:uv run python 内联 smoke 校验 _is_non_retryable_error 6 种异常契约 + GlobalSearchService(llm_config_id=...) 构造/读字段一致。

影响范围

  • 前端:无变更(后端 API 接口契约不变,answer 文案仅在失败分支优化)。
  • 后端:api.py / api_helpers.py / global_search.py / extractors.py / embedding.py 共 5 个 src 文件。
  • GitHub Actions / 文档:docs/issue.md 追加 ISSUE-086 摘要。

Next Best Action

  • 在自有 Chrome 主 profile + 真实语料库上执行一次全局问答端到端复测,确认 embedding_request 日志指向语料库专属模型(非 localhost:3392)且 evidence > 0。
  • community_summarizer._call_llm 评估是否也应迁移到 llm_config_id 注入模式(当前以 caller 显式 model + resolve_llm_config() 凭证拼接,若 corpus 绑定非默认 LLM 时会凭证/模型不匹配)。

四项级联缺陷修复(端到端 ~8.8s 退化至 <2s,evidence=0 误报修正):

1. 查询侧 embedding 模型脱钩:global_search / graph_search / multi_hop fallback 三处端点改为
   先解 corpus.config.models 中的 embedding_config_id,再传入 build_embedding_fn——
   与 ingestion 写入摘要 embedding 时同一模型 → 同一向量空间;
2. GlobalSearchService 接入 llm_config_id 参数与 extra_kwargs 透传,对齐 community_summarizer
   既定模式,避免回退到硬编码 openai/gpt-5-mini + 无凭证;
3. 提取 _resolve_corpus_model_ids 共享 helper,复用 graph/service.py 已确立的 corpus.config
   JSONB 查询模式(字段口径与 _MODELS_WHITELIST 一致:embedding_config_id / llm_config_id);
4. 新增 _is_non_retryable_error 模块级 helper(双层兜底:litellm.exceptions isinstance +
   字符串模式),call_llm_with_retry / _call_with_retry 在凭证 / 路由 / 参数类终态错误时
   立即放弃,避免 N×退避空转;
5. global_search 零证据语义二分:candidates>0 但全部 map 失败时 answer 改为「全局检索失败」
   + 基础设施排查指引,杜绝把基础设施故障伪装成内容缺失。

测试:test_global_search 新增 3 个用例(llm_config_id 路由 / 默认 fallback / 零证据
infrastructure-error 文案),test_embedding 新增 TestNonRetryableFailFast 4 个用例
(Auth/NotFound/文本兜底/transient 仍重试);knowledge 子集 816 项断言全过。

🤖 Generated with [Claude Code](https://github.com/claude), [CodeX](https://openai.com), [Gemini](https://github.com/apps/gemini-code-assist)
Co-Authored-By: Aurelius Huang<threefish.ai@gmail.com>
记录本次修复的表因、根因(按层级追溯)、处理方式、验证证据、后续防范与同类问题影响,
按既有「跨上下文留存问题处理经验」格式与上一条 ISSUE-085 对齐。

🤖 Generated with [Claude Code](https://github.com/claude), [CodeX](https://openai.com), [Gemini](https://github.com/apps/gemini-code-assist)
Co-Authored-By: Aurelius Huang<threefish.ai@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant