Skip to content

fix(knowledge-graph): 消除 KG Build LLM 调用重试死循环 — 全局统一超时与重试管控 - #518

Merged
ThreeFish-AI merged 2 commits into
feature/1.x.xfrom
ThreeFish-AI/kg-build-retry-loop
May 11, 2026
Merged

fix(knowledge-graph): 消除 KG Build LLM 调用重试死循环 — 全局统一超时与重试管控#518
ThreeFish-AI merged 2 commits into
feature/1.x.xfrom
ThreeFish-AI/kg-build-retry-loop

Conversation

@ThreeFish-AI

Copy link
Copy Markdown
Owner

背景

核心变更

  • 禁用 SDK 隐形重试:所有 litellm.acompletion() 调用统一传入 num_retries=0, max_retries=0,消除 OpenAI SDK 内部不可控的重试层
  • 全局统一保障:新增 KG_LLM_TIMEOUT_SECONDS=300s(5min 单次超时)和 KG_LLM_MAX_RETRIES=3(每个独立 LLM 请求最多 3 次重试),支持环境变量覆盖
  • 公共工具函数:新增 call_llm_with_retry() 供 community_summarizer / global_search 复用,获得与提取器一致的调用保障
  • 外层超时自动推导chunk_extract_timeoutmax_retries × timeout + backoff 自动计算,确保预算覆盖
  • 断路器协同取消:新增 llm_cancel 事件信号,断路器 OPEN 时通知同批并发 chunk 立即跳过 LLM 走 fallback

风险与回滚

  • 主要风险:禁用 SDK 重试后,瞬态网络抖动不再被 SDK 静默重试,会更快触发应用层 fallback(regex/cooccurrence),图谱质量可能略降
  • 回滚方式:git revert 即可恢复原始行为

验证证据

  • 单元测试:664 passed,1 failed(无关变更 test_extraction_llm_plan
  • 覆盖率:extractors.py 80%,service.py 63%

影响范围

  • 前端:无
  • 后端:extractors.pyservice.pycommunity_summarizer.pyglobal_search.py
  • GitHub Actions / 文档:无

Next Best Action

  • 在 Harness Engineering corpus 上手动触发 KG Build,验证不再出现 Retrying request to /chat/completions 死循环
  • 观察构建耗时是否从分钟级降为秒级(断路器触发后剩余 chunk 走 fallback <1s)

🤖 Generated with Claude Code

根因:OpenAI SDK 内部隐形重试(默认 max_retries=2)与应用层重试(max_retries=2)
叠加,导致单 chunk 提取超时预算从设计的 61s 膨胀至 187s,每个 chunk 白等 90s。

修复策略:
- 新增全局可配置常量 KG_LLM_TIMEOUT_SECONDS=300s(5min)、KG_LLM_MAX_RETRIES=3,
  支持环境变量覆盖
- 所有 litellm.acompletion() 调用统一传入 num_retries=0, max_retries=0,彻底禁用
  SDK 隐形重试,全链路由应用层统一管控
- 新增 call_llm_with_retry() 公共工具函数,供 community_summarizer / global_search
  复用,获得与提取器一致的 3 次重试保障
- service.py 外层 chunk_extract_timeout 改为自动推导,确保预算覆盖内层重试
- 断路器新增 llm_cancel 协同取消信号,OPEN 时通知同批并发 chunk 立即跳过 LLM

🤖 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>
…rror;

当 CooccurrenceRelationExtractor.extract() 抛出异常时,relations 变量在 try 块内
赋值被跳过,但 return 语句在 try/except 外仍执行。在 except 块内初始化 relations = []
确保 fallback 失败时安全返回空关系列表。

🤖 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>
@ThreeFish-AI
ThreeFish-AI merged commit 4e53706 into feature/1.x.x May 11, 2026
7 checks passed
@ThreeFish-AI
ThreeFish-AI deleted the ThreeFish-AI/kg-build-retry-loop branch May 11, 2026 14:23
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