debug_database_goalid.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import requests
  2. import json
  3. def debug_database_goalid():
  4. """
  5. 调试数据库中goalId的问题
  6. """
  7. try:
  8. print("=== 调试数据库goalId问题 ===")
  9. # 1. 先调用Coze API生成一道新题目
  10. test_data = {
  11. "userId": 1,
  12. "goalId": 8, # 使用新的goalId
  13. "subject": "数学",
  14. "knowledgePoint": "基础加法运算",
  15. "difficulty": "简单",
  16. "type": "选择题",
  17. "totalQuantity": 1
  18. }
  19. print(f"步骤1: 调用Coze API生成题目,goalId={test_data['goalId']}")
  20. response = requests.post(
  21. "http://localhost:8080/api/coze/generate-questions",
  22. json=test_data,
  23. timeout=30
  24. )
  25. if response.status_code == 200:
  26. api_result = response.json()
  27. print(f"API调用成功: {api_result.get('success', False)}")
  28. if api_result.get('success'):
  29. questions = api_result.get('questions', [])
  30. print(f"生成了 {len(questions)} 道题目")
  31. # 2. 查询数据库中goalId=8的所有题目
  32. print(f"\n步骤2: 查询数据库中goalId={test_data['goalId']}的题目")
  33. db_response = requests.get(
  34. f"http://localhost:8080/api/ai/questions/{test_data['goalId']}",
  35. timeout=10
  36. )
  37. if db_response.status_code == 200:
  38. db_result = db_response.json()
  39. print(f"数据库查询成功,返回数据: {json.dumps(db_result, indent=2, ensure_ascii=False)}")
  40. # 3. 分析返回的数据结构
  41. if 'data' in db_result and db_result['data']:
  42. questions = db_result['data']
  43. print(f"\n步骤3: 分析返回的题目数据")
  44. for i, question in enumerate(questions, 1):
  45. print(f"\n题目 {i}:")
  46. print(f" - detailId: {question.get('detailId')}")
  47. print(f" - goalId: {question.get('goalId')}")
  48. print(f" - content: {question.get('content', '')[:50]}...")
  49. print(f" - answer: {question.get('answer')}")
  50. print(f" - difficulty: {question.get('difficulty')}")
  51. print(f" - knowledgePoint: {question.get('knowledgePoint')}")
  52. print(f" - contentType: {question.get('contentType')}")
  53. # 检查goalId字段
  54. if 'goalId' not in question:
  55. print(f" ❌ 缺少goalId字段!")
  56. elif question.get('goalId') is None:
  57. print(f" ❌ goalId字段为null!")
  58. elif question.get('goalId') == test_data['goalId']:
  59. print(f" ✅ goalId一致: {question.get('goalId')}")
  60. else:
  61. print(f" ❌ goalId不一致: 预期={test_data['goalId']}, 实际={question.get('goalId')}")
  62. else:
  63. print("❌ 数据库中没有找到题目")
  64. else:
  65. print(f"❌ 数据库查询失败: {db_response.status_code}")
  66. print(db_response.text)
  67. else:
  68. print(f"❌ API调用失败: {api_result.get('message', 'Unknown error')}")
  69. else:
  70. print(f"❌ API调用失败: {response.status_code}")
  71. print(response.text)
  72. except requests.exceptions.Timeout:
  73. print("❌ 请求超时")
  74. except Exception as e:
  75. print(f"❌ 请求异常: {e}")
  76. if __name__ == "__main__":
  77. debug_database_goalid()