VectorStoreClient.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # @description:
  2. # @author: licanglong
  3. # @date: 2025/12/19 16:08
  4. import logging
  5. from qdrant_client import AsyncQdrantClient
  6. from sentence_transformers import SentenceTransformer
  7. from app.core import CTX
  8. from app.utils.pathutils import getpath
  9. log = logging.getLogger(__name__)
  10. class VectorStoreClient:
  11. def __init__(
  12. self,
  13. host: str = None,
  14. port: int = None,
  15. model_path: str = None,
  16. ):
  17. if not host and not port:
  18. raise ValueError("host and port cannot be empty")
  19. if not model_path:
  20. raise ValueError("model_path cannot be empty")
  21. # 1. Qdrant 客户端
  22. self.client = AsyncQdrantClient(host=host, port=port)
  23. # 2. 向量模型
  24. self.embedding = SentenceTransformer(model_path)
  25. async def create_collection(self, collection_name: str, **kwargs) -> bool:
  26. if await self.client.collection_exists(collection_name):
  27. return False
  28. await self.client.create_collection(
  29. collection_name=collection_name,
  30. **kwargs,
  31. )
  32. return True
  33. vector_store_client = VectorStoreClient(host=CTX.ENV.getprop("qdrant.host"),
  34. port=CTX.ENV.getprop("qdrant.port"),
  35. model_path=getpath(r"res\models\acge_text_embedding"))