101 lines
3.9 KiB
Python
101 lines
3.9 KiB
Python
|
|
"""
|
|||
|
|
手动测试单份报告的AI分析(调试版)
|
|||
|
|
"""
|
|||
|
|
import asyncio
|
|||
|
|
import sys
|
|||
|
|
sys.path.insert(0, ".")
|
|||
|
|
|
|||
|
|
from app.database import AsyncSessionLocal, init_db
|
|||
|
|
from app.models import Report, AnalysisResult
|
|||
|
|
from app.services.ai_analyzer import ai_analyzer
|
|||
|
|
from sqlalchemy import select, delete
|
|||
|
|
from sqlalchemy.orm import selectinload
|
|||
|
|
|
|||
|
|
async def test_single_report():
|
|||
|
|
print("=" * 60)
|
|||
|
|
print("调试AI分析 - 测试单份已提取的报告")
|
|||
|
|
print("=" * 60)
|
|||
|
|
|
|||
|
|
await init_db()
|
|||
|
|
|
|||
|
|
async with AsyncSessionLocal() as db:
|
|||
|
|
# 找一份有提取内容但AI总结为空的报告
|
|||
|
|
stmt = select(Report).where(
|
|||
|
|
Report.is_extracted == True,
|
|||
|
|
Report.analysis_status == "completed"
|
|||
|
|
).options(
|
|||
|
|
selectinload(Report.company),
|
|||
|
|
selectinload(Report.extracted_contents),
|
|||
|
|
selectinload(Report.analysis_results)
|
|||
|
|
).limit(1)
|
|||
|
|
|
|||
|
|
result = await db.execute(stmt)
|
|||
|
|
report = result.scalar_one_or_none()
|
|||
|
|
|
|||
|
|
if not report:
|
|||
|
|
print("❌ 没有找到符合条件的报告")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
print(f"\n报告: {report.title}")
|
|||
|
|
print(f"公司: {report.company.short_name}")
|
|||
|
|
print(f"提取内容数: {len(report.extracted_contents)}")
|
|||
|
|
print(f"分析结果数: {len(report.analysis_results)}")
|
|||
|
|
|
|||
|
|
# 检查是否有summary类型的分析结果
|
|||
|
|
has_summary = any(r.analysis_type == "summary" for r in report.analysis_results)
|
|||
|
|
print(f"有AI总结: {has_summary}")
|
|||
|
|
|
|||
|
|
if not has_summary and report.extracted_contents:
|
|||
|
|
print("\n→ 尝试生成AI总结...")
|
|||
|
|
|
|||
|
|
# 收集已有的章节分析
|
|||
|
|
section_analyses = [r for r in report.analysis_results if r.analysis_type == "section"]
|
|||
|
|
print(f"已有章节分析: {len(section_analyses)}")
|
|||
|
|
|
|||
|
|
if section_analyses:
|
|||
|
|
# 将章节分析转换为summarize_analyses需要的格式
|
|||
|
|
valid_results = []
|
|||
|
|
for s in section_analyses:
|
|||
|
|
valid_results.append({
|
|||
|
|
"section_name": s.section_name,
|
|||
|
|
"analysis": s.summary,
|
|||
|
|
"success": True
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
company_name = report.company.short_name or report.company.company_name
|
|||
|
|
|
|||
|
|
print(f"调用AI汇总,共 {len(valid_results)} 个章节...")
|
|||
|
|
summary = await ai_analyzer.summarize_analyses(valid_results, company_name, report.title)
|
|||
|
|
|
|||
|
|
print(f"\n汇总结果:")
|
|||
|
|
print(f" success: {summary.get('success')}")
|
|||
|
|
print(f" tokens: {summary.get('tokens', 0)}")
|
|||
|
|
|
|||
|
|
if summary.get("success"):
|
|||
|
|
print(f" summary (前500字): {summary.get('summary', '')[:500]}")
|
|||
|
|
|
|||
|
|
# 保存结果
|
|||
|
|
final_analysis = AnalysisResult(
|
|||
|
|
report_id=report.id,
|
|||
|
|
analysis_type="summary",
|
|||
|
|
section_name="综合分析",
|
|||
|
|
ai_model=ai_analyzer.model,
|
|||
|
|
summary=summary["summary"],
|
|||
|
|
token_count=summary.get("tokens", 0),
|
|||
|
|
is_final=True
|
|||
|
|
)
|
|||
|
|
db.add(final_analysis)
|
|||
|
|
await db.commit()
|
|||
|
|
print("\n✅ AI总结已保存!")
|
|||
|
|
else:
|
|||
|
|
print(f"\n❌ 汇总失败")
|
|||
|
|
if "error" in summary:
|
|||
|
|
print(f" 错误: {summary.get('error')}")
|
|||
|
|
else:
|
|||
|
|
print("❌ 没有章节分析,无法生成汇总")
|
|||
|
|
else:
|
|||
|
|
print("\n✅ 报告已有AI总结,无需重新生成")
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
asyncio.run(test_single_report())
|