Skip to content

排查并修复 FinSight-AI AI 研报无法生成的报错 - #2

Merged
juanjuandog merged 1 commit into
juanjuandog:masterfrom
4tj:agent/dev/e103d3cc
May 26, 2026
Merged

排查并修复 FinSight-AI AI 研报无法生成的报错#2
juanjuandog merged 1 commit into
juanjuandog:masterfrom
4tj:agent/dev/e103d3cc

Conversation

@4tj

@4tj 4tj commented May 24, 2026

Copy link
Copy Markdown
Contributor

背景

AI 研报在 Ollama 不可用时会走规则 fallback,但 sidecar 把完整连接异常写进 model 字段,导致后端持久化到 stock_analysis_reports.model VARCHAR(128) 时失败。

根因结论

复现 /analyze-stock 在 Ollama 未启动时返回 HTTP 200,但 modelqwen2.5:7b unavailable: HTTPConnectionPool(...),本地复现长度为 262。该值超过生产库 stock_analysis_reports.model 的 128 字符上限,所以后端保存研报时报错,研报生成链路中断。

修复方案

  • ai-service/app/main.py: Ollama 调用失败时仍返回规则 fallback,但 model 保持为稳定的 rule-fallback,避免把异常详情写入持久化字段。
  • ai-service/requirements.txt: 显式声明 requests,因为 Ollama 调用路径直接 import/use requests
  • ai-service/tests/test_stock_analysis.py: 增加 fallback 响应测试,覆盖长异常下 model 保持可持久化且 aiGenerated=false

数据库变更

无 DB 变更;未新增 migration,未改 schema。

配置 / 环境变量变更

无环境变量变更。仅新增 Python runtime 直接依赖 requests==2.32.3

复现 / 验证证据

修复前复现:

  • /analyze-stock 返回 200
  • model_length 262
  • model 内容包含 HTTPConnectionPool(... Connection refused ...)

修复后验证:

  • uv run --with fastapi==0.115.5 --with pydantic==2.9.2 --with requests==2.32.3 python -m unittest discover -s tests: ✅ Ran 1 test ... OK
  • TestClient 调用 /analyze-stock: ✅ 200, model: rule-fallback, model_length 13
  • docker build -t finsight-ai-service-test ./ai-service: ✅ build success
  • docker run --rm finsight-ai-service-test python -c "import requests; from app.main import app; print('ok')": ✅ ok
  • docker run --rm -v "$PWD":/workspace -w /workspace/backend maven:3.9.9-eclipse-temurin-17 mvn test: ✅ BUILD SUCCESS, tests 4, skipped 1 Testcontainers smoke because the Maven container cannot access /var/run/docker.sock

相关 issue

SHO-551

Co-authored-by: multica-agent <github@multica.ai>

@4tj 4tj left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer 阶段性结论:PASSED

Code review

实际改动仅 3 文件 32+/2-:

  • ai-service/app/main.py L138-143:fallback 路径不再把 Ollama 异常字符串塞入 model,统一回退到 fallback.model(="rule-fallback",固定 13 字节,<<128)。根因结论与修复一致。
  • ai-service/requirements.txt:补 requests==2.32.3app/main.py:232 确实 import requests,原先依赖 akshare 间接带入,显式化合理。
  • ai-service/tests/test_stock_analysis.py:单测断言 response.model == "rule-fallback"len(response.model) <= 128aiGenerated is False,精确覆盖"长异常 fallback"路径。

无未声明的无关改动;无 DB schema / migration / DDL(命令检测确认 0 命中);其他成功路径 model=ollama_model()(典型 "qwen2.5:7b",远低于 128)未变。

本地复跑

  • uv run --with fastapi==0.115.5 --with pydantic==2.9.2 --with requests==2.32.3 python -m unittest discover -s testsRan 1 test ... OK
  • TestClient 调用 /analyze-stockOLLAMA_BASE_URL=http://127.0.0.1:1,强制连接失败)→ status_code=200, model=rule-fallback, model_length=13, source=fallback-rule, aiGenerated=False
  • docker run --rm -v "$PWD":/workspace -w /workspace/backend maven:3.9.9-eclipse-temurin-17 mvn testTests run: 4, Failures: 0, Errors: 0, Skipped: 1BUILD SUCCESS。被 skip 的是 FinSightInfrastructureSmokeTest(Testcontainers,容器内无法访问宿主 docker socket,CI ubuntu-latest 同样无 daemon,已知限制)。
  • GitHub Actions CI:Backend tests SUCCESS,Script syntax SUCCESS。

@juanjuandog
juanjuandog merged commit 3da99f6 into juanjuandog:master May 26, 2026
2 checks passed
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.

2 participants