hybrid_search_http_example.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 混合检索HTTP服务请求示例
  5. 使用Python requests库调用混合检索接口
  6. """
  7. import requests
  8. import json
  9. def hybrid_search_example():
  10. """
  11. 混合检索接口调用示例
  12. """
  13. # 服务地址
  14. base_url = "http://localhost:18001"
  15. endpoint = "/hybrid_search"
  16. url = f"{base_url}{endpoint}"
  17. # 示例1:基本请求(仅文本查询)
  18. print("示例1:基本请求(仅文本查询)")
  19. payload1 = {
  20. "text_query": "这是一个测试文本查询",
  21. "topn": 2
  22. }
  23. response1 = requests.post(url, json=payload1)
  24. print(f"状态码: {response1.status_code}")
  25. print(f"响应内容: {json.dumps(response1.json(), indent=2, ensure_ascii=False)}")
  26. # 示例2:完整请求(文本+图片)
  27. print("\n示例2:完整请求(文本+图片)")
  28. payload2 = {
  29. "text_query": "这是一个带图片的测试查询",
  30. "image": "https://example.com/test.jpg",
  31. "topn": 5
  32. }
  33. response2 = requests.post(url, json=payload2)
  34. print(f"状态码: {response2.status_code}")
  35. print(f"响应内容: {json.dumps(response2.json(), indent=2, ensure_ascii=False)}")
  36. # 示例3:使用默认topn值
  37. print("\n示例3:使用默认topn值")
  38. payload3 = {
  39. "text_query": "这是一个使用默认值的测试",
  40. "image": "https://example.com/another.jpg"
  41. }
  42. response3 = requests.post(url, json=payload3)
  43. print(f"状态码: {response3.status_code}")
  44. print(f"响应内容: {json.dumps(response3.json(), indent=2, ensure_ascii=False)}")
  45. if __name__ == "__main__":
  46. hybrid_search_example()