test_new_api.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python3
  2. """
  3. 测试新的API实现
  4. """
  5. import requests
  6. import json
  7. from datetime import datetime
  8. def test_api():
  9. """测试新的API端点"""
  10. print("=" * 60)
  11. print("测试新的API实现")
  12. print("=" * 60)
  13. base_url = "http://localhost:8084"
  14. thread_id = "wang10:20250717211620915"
  15. user_id = "wang10"
  16. # 测试不包含工具消息的API
  17. print(f"\n🚀 测试API (不包含工具消息)...")
  18. url = f"{base_url}/api/v0/react/users/{user_id}/conversations/{thread_id}"
  19. try:
  20. response = requests.get(url, timeout=30)
  21. print(f"📊 响应状态码: {response.status_code}")
  22. if response.status_code == 200:
  23. data = response.json()
  24. print(f"✅ API调用成功")
  25. print(f"📝 响应结构:")
  26. print(f" success: {data.get('success')}")
  27. print(f" timestamp: {data.get('timestamp')}")
  28. if 'data' in data:
  29. api_data = data['data']
  30. print(f" data.user_id: {api_data.get('user_id')}")
  31. print(f" data.thread_id: {api_data.get('thread_id')}")
  32. print(f" data.message_count: {api_data.get('message_count')}")
  33. print(f" data.created_at: {api_data.get('created_at')}")
  34. print(f" data.total_checkpoints: {api_data.get('total_checkpoints')}")
  35. messages = api_data.get('messages', [])
  36. print(f"\n📋 前3条消息:")
  37. for i, msg in enumerate(messages[:3]):
  38. print(f" 消息 {i+1}:")
  39. print(f" id: {msg.get('id')}")
  40. print(f" type: {msg.get('type')}")
  41. print(f" timestamp: {msg.get('timestamp')}")
  42. print(f" content: {msg.get('content', '')[:50]}...")
  43. # 保存完整响应到文件
  44. with open('api_response_no_tools.json', 'w', encoding='utf-8') as f:
  45. json.dump(data, f, ensure_ascii=False, indent=2)
  46. print(f"\n💾 完整响应已保存到 api_response_no_tools.json")
  47. else:
  48. print(f"❌ API调用失败")
  49. print(f"响应内容: {response.text}")
  50. except Exception as e:
  51. print(f"❌ 请求失败: {e}")
  52. # 测试包含工具消息的API
  53. print(f"\n🚀 测试API (包含工具消息)...")
  54. url_with_tools = f"{base_url}/api/v0/react/users/{user_id}/conversations/{thread_id}?include_tools=true"
  55. try:
  56. response = requests.get(url_with_tools, timeout=30)
  57. print(f"📊 响应状态码: {response.status_code}")
  58. if response.status_code == 200:
  59. data = response.json()
  60. print(f"✅ API调用成功")
  61. if 'data' in data:
  62. api_data = data['data']
  63. messages = api_data.get('messages', [])
  64. print(f"📝 包含工具消息的总数: {len(messages)}")
  65. # 统计消息类型
  66. type_counts = {}
  67. for msg in messages:
  68. msg_type = msg.get('type', 'unknown')
  69. type_counts[msg_type] = type_counts.get(msg_type, 0) + 1
  70. print(f"📊 消息类型统计:")
  71. for msg_type, count in type_counts.items():
  72. print(f" {msg_type}: {count}")
  73. # 保存完整响应到文件
  74. with open('api_response_with_tools.json', 'w', encoding='utf-8') as f:
  75. json.dump(data, f, ensure_ascii=False, indent=2)
  76. print(f"\n💾 完整响应已保存到 api_response_with_tools.json")
  77. else:
  78. print(f"❌ API调用失败")
  79. print(f"响应内容: {response.text}")
  80. except Exception as e:
  81. print(f"❌ 请求失败: {e}")
  82. if __name__ == "__main__":
  83. test_api()