search_infinity.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Infinity搜索API服务
  2. from fastapi import FastAPI, HTTPException
  3. from typing import List, Dict, Any, Optional
  4. from src.api.db.services.infinity_search_service import InfinitySearchService
  5. from src.utils.infinity import get_client
  6. # 创建FastAPI应用
  7. app = FastAPI(
  8. title="Infinity Search API",
  9. description="基于Infinity向量数据库的搜索API服务",
  10. version="1.0.0"
  11. )
  12. # 请求模型
  13. from pydantic import BaseModel
  14. class SearchRequest(BaseModel):
  15. """搜索请求模型"""
  16. search_query: Dict[str, Any]
  17. # 1. 普通搜索接口
  18. @app.post("/text", response_model=Dict[str, Any])
  19. def search(request: SearchRequest):
  20. """
  21. 普通搜索接口
  22. - **table_name**: 表名
  23. - **output_fields**: 要返回的字段列表
  24. - **query**: 查询条件,包含field、query和topn字段
  25. - **database_name**: 数据库名称(可选,默认使用客户端配置的数据库)
  26. """
  27. try:
  28. search_service = InfinitySearchService(infinity_client=get_client())
  29. result = search_service.search(request.search_query)
  30. return {"success": True, "result": result}
  31. except Exception as e:
  32. raise HTTPException(status_code=500, detail=f"搜索失败: {str(e)}")
  33. # 2. 向量搜索接口
  34. @app.post("/vector", response_model=Dict[str, Any])
  35. def vector_search(request: SearchRequest):
  36. """
  37. 向量搜索接口
  38. - **table_name**: 表名
  39. - **output_fields**: 要返回的字段列表
  40. - **query**: 查询条件,包含vector_field、query_vector和topn字段
  41. - **database_name**: 数据库名称(可选,默认使用客户端配置的数据库)
  42. """
  43. try:
  44. search_service = InfinitySearchService(infinity_client=get_client())
  45. result = search_service.vector_search(request.search_query)
  46. return {"success": True, "result": result}
  47. except Exception as e:
  48. raise HTTPException(status_code=500, detail=f"向量搜索失败: {str(e)}")
  49. # 3. 混合搜索接口
  50. @app.post("/hybrid", response_model=Dict[str, Any])
  51. def hybrid_search(request: SearchRequest):
  52. """
  53. 混合搜索接口
  54. - **table_name**: 表名
  55. - **output_fields**: 要返回的字段列表
  56. - **query**: 查询条件,包含vector_field、query_vector、field、query、topn和fusion_weight字段
  57. - **database_name**: 数据库名称(可选,默认使用客户端配置的数据库)
  58. """
  59. try:
  60. search_service = InfinitySearchService(infinity_client=get_client())
  61. result = search_service.hybrid_search(request.search_query)
  62. return {"success": True, "result": result}
  63. except Exception as e:
  64. raise HTTPException(status_code=500, detail=f"混合搜索失败: {str(e)}")