test_multimodal_search.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/usr/bin/env python3
  2. """
  3. 测试多模态检索 API
  4. """
  5. import requests
  6. import json
  7. # 配置
  8. EMBEDDING_API_URL = "http://localhost:8084"
  9. def test_text_only_search():
  10. """测试纯文本检索(兼容现有能力)"""
  11. print("=" * 60)
  12. print("测试 1: 纯文本检索 (TEXT_ONLY)")
  13. print("=" * 60)
  14. url = f"{EMBEDDING_API_URL}/api/v2/multimodal/search"
  15. request_body = {
  16. "searchType": "TEXT_ONLY",
  17. "textQuery": "数学",
  18. "topK": 5
  19. }
  20. try:
  21. response = requests.post(
  22. url,
  23. json=request_body,
  24. headers={"Content-Type": "application/json"},
  25. timeout=30,
  26. )
  27. print(f"Status: {response.status_code}")
  28. result = response.json()
  29. print(f"Response:")
  30. print(json.dumps(result, ensure_ascii=False, indent=2))
  31. if result.get("success"):
  32. print(f"\n✅ 成功!找到 {len(result.get('allResults', []))} 个结果")
  33. for i, item in enumerate(result.get('allResults', []), 1):
  34. print(f"\n 结果 {i}:")
  35. print(f" 文档ID: {item.get('docId')}")
  36. print(f" 相似度: {item.get('score'):.4f}")
  37. content = item.get('content', '')
  38. if len(content) > 80:
  39. content = content[:80] + "..."
  40. print(f" 内容: {content}")
  41. else:
  42. print(f"\n❌ 失败: {result.get('errorMessage')}")
  43. except Exception as e:
  44. print(f"Error: {e}")
  45. import traceback
  46. traceback.print_exc()
  47. print()
  48. def test_text_to_multimodal_search():
  49. """测试文本查多模态"""
  50. print("=" * 60)
  51. print("测试 2: 文本查多模态 (TEXT_TO_MULTIMODAL)")
  52. print("=" * 60)
  53. url = f"{EMBEDDING_API_URL}/api/v2/multimodal/search"
  54. request_body = {
  55. "searchType": "TEXT_TO_MULTIMODAL",
  56. "textQuery": "口算题",
  57. "topK": 5
  58. }
  59. try:
  60. response = requests.post(
  61. url,
  62. json=request_body,
  63. headers={"Content-Type": "application/json"},
  64. timeout=30,
  65. )
  66. print(f"Status: {response.status_code}")
  67. result = response.json()
  68. print(f"Response:")
  69. print(json.dumps(result, ensure_ascii=False, indent=2))
  70. except Exception as e:
  71. print(f"Error: {e}")
  72. import traceback
  73. traceback.print_exc()
  74. print()
  75. def test_image_to_multimodal_search():
  76. """测试图片查多模态(待实现)"""
  77. print("=" * 60)
  78. print("测试 3: 图片查多模态 (IMAGE_TO_MULTIMODAL) - 待实现")
  79. print("=" * 60)
  80. url = f"{EMBEDDING_API_URL}/api/v2/multimodal/search"
  81. request_body = {
  82. "searchType": "IMAGE_TO_MULTIMODAL",
  83. "imageData": "base64_encoded_image_here",
  84. "imageDataType": "BASE64",
  85. "topK": 5
  86. }
  87. try:
  88. response = requests.post(
  89. url,
  90. json=request_body,
  91. headers={"Content-Type": "application/json"},
  92. timeout=30,
  93. )
  94. print(f"Status: {response.status_code}")
  95. result = response.json()
  96. print(f"Response:")
  97. print(json.dumps(result, ensure_ascii=False, indent=2))
  98. except Exception as e:
  99. print(f"Error: {e}")
  100. import traceback
  101. traceback.print_exc()
  102. print()
  103. def main():
  104. print("\n")
  105. print("╔" + "=" * 58 + "╗")
  106. print("║" + " " * 15 + "多模态检索 API 测试" + " " * 18 + "║")
  107. print("╚" + "=" * 58 + "╝")
  108. print()
  109. # 检查服务状态
  110. print("检查服务状态...")
  111. try:
  112. health_response = requests.get(f"{EMBEDDING_API_URL}/actuator/health", timeout=5)
  113. if health_response.status_code == 200:
  114. health_data = health_response.json()
  115. if health_data.get("status") == "UP":
  116. print("✅ schedule-embedding-api 正常\n")
  117. else:
  118. print(f"⚠️ schedule-embedding-api 状态: {health_data.get('status')}\n")
  119. else:
  120. print(f"❌ schedule-embedding-api 异常: {health_response.status_code}\n")
  121. return
  122. except Exception as e:
  123. print(f"❌ schedule-embedding-api 不可达: {e}\n")
  124. return
  125. # 运行测试
  126. test_text_only_search()
  127. test_text_to_multimodal_search()
  128. test_image_to_multimodal_search()
  129. print("=" * 60)
  130. print("测试完成")
  131. print("=" * 60)
  132. if __name__ == "__main__":
  133. main()