test_fastapi_hybrid_search.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python3
  2. """
  3. 测试混合检索FastAPI服务
  4. """
  5. import requests
  6. import json
  7. # 测试数据
  8. test_data = {
  9. "text_query": "测试",
  10. "image": "https://example.com/image.jpg",
  11. "topn": 2
  12. }
  13. # 发送POST请求
  14. def test_hybrid_search():
  15. url = "http://localhost:18001/hybrid_search"
  16. headers = {
  17. "Content-Type": "application/json"
  18. }
  19. print("开始测试混合检索FastAPI服务...")
  20. print(f"请求URL: {url}")
  21. print(f"请求数据: {json.dumps(test_data, indent=2, ensure_ascii=False)}")
  22. try:
  23. # 发送POST请求
  24. response = requests.post(url, headers=headers, json=test_data, timeout=10)
  25. # 打印响应结果
  26. print(f"\n响应状态码: {response.status_code}")
  27. print(f"响应头: {dict(response.headers)}")
  28. print(f"响应内容: {response.text}")
  29. if response.status_code == 200:
  30. # 解析JSON响应
  31. response_data = response.json()
  32. print(f"\n解析后的响应数据: {json.dumps(response_data, indent=2, ensure_ascii=False)}")
  33. print("测试成功!")
  34. else:
  35. print(f"\n测试失败,状态码: {response.status_code}")
  36. except Exception as e:
  37. print(f"\n测试失败,请求异常: {str(e)}")
  38. # 测试健康检查接口
  39. def test_health_check():
  40. url = "http://localhost:18001/health"
  41. print("\n开始测试健康检查接口...")
  42. print(f"请求URL: {url}")
  43. try:
  44. # 发送GET请求
  45. response = requests.get(url, timeout=5)
  46. # 打印响应结果
  47. print(f"\n响应状态码: {response.status_code}")
  48. print(f"响应头: {dict(response.headers)}")
  49. print(f"响应内容: {response.text}")
  50. if response.status_code == 200:
  51. # 解析JSON响应
  52. response_data = response.json()
  53. print(f"\n解析后的响应数据: {json.dumps(response_data, indent=2, ensure_ascii=False)}")
  54. print("健康检查测试成功!")
  55. else:
  56. print(f"\n健康检查测试失败,状态码: {response.status_code}")
  57. except Exception as e:
  58. print(f"\n健康检查测试失败,请求异常: {str(e)}")
  59. if __name__ == "__main__":
  60. test_hybrid_search()
  61. test_health_check()