test_coze_logging.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 测试Coze API调用和日志记录
  5. 用于验证goal_id一致性检查功能
  6. """
  7. import requests
  8. import json
  9. import time
  10. def test_coze_api_with_logging():
  11. """
  12. 测试Coze API调用并检查日志输出
  13. """
  14. base_url = "http://localhost:8080"
  15. # 测试数据
  16. test_request = {
  17. "userId": 1,
  18. "goalId": 3, # 使用新的goalId进行测试
  19. "subject": "数学",
  20. "type": "选择题",
  21. "difficulty": "中等",
  22. "totalQuantity": 2,
  23. "knowledgePoint": "导数的应用"
  24. }
  25. print(f"开始测试Coze API调用...")
  26. print(f"测试请求参数: {json.dumps(test_request, ensure_ascii=False, indent=2)}")
  27. try:
  28. # 1. 调用Coze生成题目API
  29. print("\n=== 步骤1: 调用Coze生成题目API ===")
  30. response = requests.post(
  31. f"{base_url}/api/coze/generate-questions",
  32. json=test_request,
  33. headers={"Content-Type": "application/json"},
  34. timeout=30
  35. )
  36. print(f"响应状态码: {response.status_code}")
  37. if response.status_code == 200:
  38. result = response.json()
  39. print(f"响应结果: {json.dumps(result, ensure_ascii=False, indent=2)}")
  40. # 2. 检查生成的题目
  41. print("\n=== 步骤2: 检查生成的题目 ===")
  42. if result.get('success') and result.get('questions'):
  43. print(f"成功生成 {len(result['questions'])} 道题目")
  44. for i, question in enumerate(result['questions'], 1):
  45. print(f"题目{i}: {question.get('question', '无题目内容')[:50]}...")
  46. else:
  47. print("未生成任何题目")
  48. if 'debugUrl' in result:
  49. print(f"调试链接: {result['debugUrl']}")
  50. # 3. 等待一下,然后检查数据库中的题目
  51. print("\n=== 步骤3: 检查数据库中的题目 ===")
  52. time.sleep(2) # 等待数据库保存完成
  53. questions_response = requests.get(
  54. f"{base_url}/api/ai/questions/{test_request['goalId']}",
  55. timeout=10
  56. )
  57. if questions_response.status_code == 200:
  58. questions_data = questions_response.json()
  59. print(f"数据库查询结果: {json.dumps(questions_data, ensure_ascii=False, indent=2)}")
  60. if questions_data.get('data'):
  61. print(f"数据库中找到 {len(questions_data['data'])} 道题目")
  62. # 验证goal_id一致性
  63. for question in questions_data['data']:
  64. if question.get('goalId') != test_request['goalId']:
  65. print(f"❌ 发现goal_id不一致!预期: {test_request['goalId']}, 实际: {question.get('goalId')}")
  66. else:
  67. print(f"✅ goal_id一致: {question.get('goalId')}")
  68. else:
  69. print("数据库中未找到题目")
  70. else:
  71. print(f"查询数据库失败,状态码: {questions_response.status_code}")
  72. else:
  73. print(f"API调用失败: {response.text}")
  74. except requests.exceptions.RequestException as e:
  75. print(f"请求异常: {e}")
  76. except Exception as e:
  77. print(f"其他异常: {e}")
  78. if __name__ == "__main__":
  79. test_coze_api_with_logging()