117 lines
4.2 KiB
Python
117 lines
4.2 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
|
||
"""
|
||
测试修改后的 ReportGenerator 类
|
||
"""
|
||
|
||
import os
|
||
import sys
|
||
import asyncio
|
||
import json
|
||
|
||
# 添加当前目录到 Python 路径
|
||
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
||
|
||
from enhanced_survey_system import ReportGenerator
|
||
|
||
async def test_report_generator():
|
||
"""测试报告生成器"""
|
||
print("🧪 开始测试 ReportGenerator...")
|
||
|
||
# 检查环境变量
|
||
if not os.getenv("GLM_API_KEY") or os.getenv("GLM_API_KEY") == "YOUR_API_KEY":
|
||
print("❌ 错误: 请在 .env 文件中设置正确的 GLM_API_KEY")
|
||
return False
|
||
|
||
try:
|
||
# 创建报告生成器实例
|
||
generator = ReportGenerator()
|
||
print("✅ ReportGenerator 实例创建成功")
|
||
|
||
# 测试加载提示词
|
||
prompt = generator.load_prompt()
|
||
print(f"✅ 提示词加载成功,长度: {len(prompt)} 字符")
|
||
|
||
# 创建测试数据
|
||
test_session_id = "test_session_123"
|
||
test_analysis_data = {
|
||
'analysis_text': '''# 考试答题情况分析
|
||
|
||
## 总分:85 分
|
||
|
||
| 题目 | 题型 | 用户答案 | 正确答案 | 是否正确 | 得分 |
|
||
|------|------|----------|----------|----------|------|
|
||
| 姓名 | 填空题 | 张三 | 无标准答案 | 无法判断 | 不适用 |
|
||
| 学校 | 填空题 | 测试小学 | 无标准答案 | 无法判断 | 不适用 |
|
||
| 年级 | 填空题 | 五年级 | 无标准答案 | 无法判断 | 不适用 |
|
||
| 考试标签 | 填空题 | 科学测试 | 无标准答案 | 无法判断 | 不适用 |
|
||
| 水在自然界中的三种状态 | 单选题 | 气体、液体、固体 | 气体、液体、固体 | ✓ | 20 |
|
||
| 植物的光合作用需要 | 单选题 | 阳光 | 阳光和水 | ✗ | 0 |
|
||
| 地球的自转导致 | 单选题 | 白天黑夜 | 白天黑夜 | ✓ | 20 |''',
|
||
'student_info': {
|
||
'name': '张三',
|
||
'school': '测试小学',
|
||
'grade': '五年级'
|
||
},
|
||
'answers': [
|
||
{
|
||
'questionText': '水在自然界中的三种状态',
|
||
'questionType': '单选题',
|
||
'userAnswer': '气体、液体、固体',
|
||
'correctAnswer': '气体、液体、固体',
|
||
'isCorrect': True,
|
||
'score': 20
|
||
},
|
||
{
|
||
'questionText': '植物的光合作用需要',
|
||
'questionType': '单选题',
|
||
'userAnswer': '阳光',
|
||
'correctAnswer': '阳光和水',
|
||
'isCorrect': False,
|
||
'score': 0
|
||
}
|
||
]
|
||
}
|
||
|
||
print("📝 准备调用 GLM API...")
|
||
|
||
# 调用 API 生成报告
|
||
result = generator.call_report_api(test_analysis_data, test_session_id)
|
||
|
||
if result.get('success'):
|
||
print("✅ API 调用成功!")
|
||
print(f"📊 报告数据类型: {type(result.get('report_data'))}")
|
||
print(f"📊 原始响应长度: {len(result.get('raw_response', ''))} 字符")
|
||
|
||
# 显示报告数据的前500个字符
|
||
report_data = result.get('report_data', '')
|
||
if isinstance(report_data, str):
|
||
print(f"📄 报告内容预览:\n{report_data[:500]}...")
|
||
else:
|
||
print(f"📄 报告数据: {json.dumps(report_data, ensure_ascii=False, indent=2)[:500]}...")
|
||
|
||
return True
|
||
else:
|
||
print(f"❌ API 调用失败: {result.get('error', '未知错误')}")
|
||
return False
|
||
|
||
except Exception as e:
|
||
print(f"❌ 测试过程中发生错误: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
return False
|
||
|
||
if __name__ == "__main__":
|
||
print("=" * 60)
|
||
print("ReportGenerator 测试")
|
||
print("=" * 60)
|
||
|
||
success = asyncio.run(test_report_generator())
|
||
|
||
print("=" * 60)
|
||
if success:
|
||
print("🎉 测试通过!")
|
||
else:
|
||
print("💥 测试失败!")
|
||
print("=" * 60) |