|
| 1 | +from typing import List, Optional |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +from gptcache.utils import import_qdrant |
| 5 | +from gptcache.utils.log import gptcache_log |
| 6 | +from gptcache.manager.vector_data.base import VectorBase, VectorData |
| 7 | + |
| 8 | +import_qdrant() |
| 9 | + |
| 10 | +from qdrant_client import QdrantClient # pylint: disable=C0413 |
| 11 | +from qdrant_client.models import PointStruct, HnswConfigDiff, VectorParams, OptimizersConfigDiff, \ |
| 12 | + Distance # pylint: disable=C0413 |
| 13 | + |
| 14 | + |
| 15 | +class QdrantVectorStore(VectorBase): |
| 16 | + |
| 17 | + def __init__( |
| 18 | + self, |
| 19 | + url: Optional[str] = None, |
| 20 | + port: Optional[int] = 6333, |
| 21 | + grpc_port: int = 6334, |
| 22 | + prefer_grpc: bool = False, |
| 23 | + https: Optional[bool] = None, |
| 24 | + api_key: Optional[str] = None, |
| 25 | + prefix: Optional[str] = None, |
| 26 | + timeout: Optional[float] = None, |
| 27 | + host: Optional[str] = None, |
| 28 | + collection_name: Optional[str] = "gptcache", |
| 29 | + location: Optional[str] = "./qdrant", |
| 30 | + dimension: int = 0, |
| 31 | + top_k: int = 1, |
| 32 | + flush_interval_sec: int = 5, |
| 33 | + index_params: Optional[dict] = None, |
| 34 | + ): |
| 35 | + if dimension <= 0: |
| 36 | + raise ValueError( |
| 37 | + f"invalid `dim` param: {dimension} in the Qdrant vector store." |
| 38 | + ) |
| 39 | + self._client: QdrantClient |
| 40 | + self._collection_name = collection_name |
| 41 | + self._in_memory = location == ":memory:" |
| 42 | + self.dimension = dimension |
| 43 | + self.top_k = top_k |
| 44 | + if self._in_memory or location is not None: |
| 45 | + self._create_local(location) |
| 46 | + else: |
| 47 | + self._create_remote(url, port, api_key, timeout, host, grpc_port, prefer_grpc, prefix, https) |
| 48 | + self._create_collection(collection_name, flush_interval_sec, index_params) |
| 49 | + |
| 50 | + def _create_local(self, location): |
| 51 | + self._client = QdrantClient(location=location) |
| 52 | + |
| 53 | + def _create_remote(self, url, port, api_key, timeout, host, grpc_port, prefer_grpc, prefix, https): |
| 54 | + self._client = QdrantClient( |
| 55 | + url=url, |
| 56 | + port=port, |
| 57 | + api_key=api_key, |
| 58 | + timeout=timeout, |
| 59 | + host=host, |
| 60 | + grpc_port=grpc_port, |
| 61 | + prefer_grpc=prefer_grpc, |
| 62 | + prefix=prefix, |
| 63 | + https=https, |
| 64 | + ) |
| 65 | + |
| 66 | + def _create_collection(self, collection_name: str, flush_interval_sec: int, index_params: Optional[dict] = None): |
| 67 | + hnsw_config = HnswConfigDiff(**(index_params or {})) |
| 68 | + vectors_config = VectorParams(size=self.dimension, distance=Distance.COSINE, |
| 69 | + hnsw_config=hnsw_config) |
| 70 | + optimizers_config = OptimizersConfigDiff(deleted_threshold=0.2, vacuum_min_vector_number=1000, |
| 71 | + flush_interval_sec=flush_interval_sec) |
| 72 | + # check if the collection exists |
| 73 | + existing_collections = self._client.get_collections() |
| 74 | + for existing_collection in existing_collections.collections: |
| 75 | + if existing_collection.name == collection_name: |
| 76 | + gptcache_log.warning("The %s collection already exists, and it will be used directly.", collection_name) |
| 77 | + break |
| 78 | + else: |
| 79 | + self._client.create_collection(collection_name=collection_name, vectors_config=vectors_config, |
| 80 | + optimizers_config=optimizers_config) |
| 81 | + |
| 82 | + def mul_add(self, datas: List[VectorData]): |
| 83 | + points = [PointStruct(id=d.id, vector=d.data.reshape(-1).tolist()) for d in datas] |
| 84 | + self._client.upsert(collection_name=self._collection_name, points=points, wait=False) |
| 85 | + |
| 86 | + def search(self, data: np.ndarray, top_k: int = -1): |
| 87 | + if top_k == -1: |
| 88 | + top_k = self.top_k |
| 89 | + reshaped_data = data.reshape(-1).tolist() |
| 90 | + search_result = self._client.search(collection_name=self._collection_name, query_vector=reshaped_data, |
| 91 | + limit=top_k) |
| 92 | + return list(map(lambda x: (x.score, x.id), search_result)) |
| 93 | + |
| 94 | + def delete(self, ids: List[str]): |
| 95 | + self._client.delete(collection_name=self._collection_name, points_selector=ids) |
| 96 | + |
| 97 | + def rebuild(self, ids=None): # pylint: disable=unused-argument |
| 98 | + optimizers_config = OptimizersConfigDiff(deleted_threshold=0.2, vacuum_min_vector_number=1000) |
| 99 | + self._client.update_collection(collection_name=self._collection_name, optimizer_config=optimizers_config) |
| 100 | + |
| 101 | + def flush(self): |
| 102 | + # no need to flush manually as qdrant flushes automatically based on the optimizers_config for remote Qdrant |
| 103 | + pass |
| 104 | + |
| 105 | + |
| 106 | + def close(self): |
| 107 | + self.flush() |
0 commit comments