跳转至

模块-AI调用-DeepSeek超时导致生成日报失败

问题现象

  • oneDay 总览页面点击"生成日报",提示"加载失败,可能是网络问题或服务器异常"
  • 前端 classifyError 走到 loading_failed 分支,errorState 设为 loading_failed
  • 后端日志无报错写入响应,但 curl 请求长时间无返回最终 HTTP 000

核心问题

  • 根因:DeepSeek OpenAI SDK 的 timeout=30 秒太短
  • 301 篇新闻的 prompt 约 13K 字符,DeepSeek 生成完整日报需 50+ 秒
  • 30 秒到达后触发 httpx.ReadTimeout,进入 circuit_breaker 重试/fallback 逻辑
  • 重试 3 次 × 2 个模型(deepseek-chat → deepseek-coder)全部超时,最终抛出 Max retries exceeded
  • 前端 axios timeout: 120000 也可能在多重超时重试期间被耗尽

  • 关键日志链

    httpcore.ReadTimeout: The read operation timed out
    httpx.ReadTimeout: The read operation timed out
    → Connection Error/Timeout: Triggering Model Fallback.
    → All candidate models exhausted.
    → oneday_llm_call_failed: Max retries exceeded and all models failed.
    

  • 日志文件log/news_2026-07-19.log
  • 相关文件
  • src/llmkit/llmkit/deepseek_provider.py:91timeout=30(原值)
  • web/src/api/request.ts:5timeout: 120000(原值)

解决方法

  1. 增大 DeepSeek SDK 超时src/llmkit/llmkit/deepseek_provider.py):

    # 原值
    timeout=30
    # 改为
    timeout=300  # 5 min for long generation tasks
    

  2. 增大前端 axios 超时web/src/api/request.ts):

    // 原值
    timeout: 120000,
    // 改为
    timeout: 360000, // 6 min — oneday generate with LLM can take 2-5 min
    

  3. 重新构建前端

    cd web && pnpm build
    

  4. 重启后端服务

    # 本地:重启 start.bat 或手动重启
    venv\Scripts\python.exe src\main.py
    # 服务器:
    systemctl restart news-backend
    

  5. 验证

    curl -s -X POST http://localhost:8000/api/oneday/generate \
      -H "Content-Type: application/json" \
      -d '{"date":"2026-07-19"}' --max-time 360
    # 应返回 HTTP 200 + 完整日报 JSON
    

下次防错措施

  • 快速定位:检查后端日志 grep ReadTimeout log/news_*.log,出现即为此问题
  • 超时设置原则:LLM 调用超时应 ≥ 所有重试 × 最大生成时间。当前配置:1 次调用 5 分钟 × 最多 3 重试 = 需 15 分钟兜底,前端 6 分钟 + nginx proxy_read_timeout 600s 基本覆盖单次调用
  • 新增 LLM 超时巡检:任何新接入 LLM 的功能,prompt > 5K 字符时 timeout 不应低于 180 秒
  • 前端超时联动:axios timeout 应 > SDK timeout,留出重试余量