|
| 1 | +import httpx |
| 2 | + |
| 3 | +from benchmark.dataset import Dataset |
| 4 | +from engine.base_client.configure import BaseConfigurator |
| 5 | +from engine.base_client.distances import Distance |
| 6 | +from engine.clients.qdrant_native.config import QDRANT_API_KEY, QDRANT_COLLECTION_NAME |
| 7 | + |
| 8 | + |
| 9 | +class QdrantNativeConfigurator(BaseConfigurator): |
| 10 | + SPARSE_VECTOR_SUPPORT = True |
| 11 | + DISTANCE_MAPPING = { |
| 12 | + Distance.L2: "Euclid", |
| 13 | + Distance.COSINE: "Cosine", |
| 14 | + Distance.DOT: "Dot", |
| 15 | + } |
| 16 | + INDEX_TYPE_MAPPING = { |
| 17 | + "int": "integer", |
| 18 | + "keyword": "keyword", |
| 19 | + "text": "text", |
| 20 | + "float": "float", |
| 21 | + "geo": "geo", |
| 22 | + } |
| 23 | + |
| 24 | + def __init__(self, host, collection_params: dict, connection_params: dict): |
| 25 | + super().__init__(host, collection_params, connection_params) |
| 26 | + |
| 27 | + self.host = f"http://{host.rstrip('/')}:6333" |
| 28 | + self.connection_params = connection_params |
| 29 | + |
| 30 | + self.headers = {"Content-Type": "application/json"} |
| 31 | + if QDRANT_API_KEY: |
| 32 | + self.headers["api-key"] = QDRANT_API_KEY |
| 33 | + |
| 34 | + timeout = connection_params.get("timeout", 30) |
| 35 | + self.client = httpx.Client( |
| 36 | + headers=self.headers, |
| 37 | + timeout=httpx.Timeout(timeout=timeout), |
| 38 | + ) |
| 39 | + |
| 40 | + def clean(self): |
| 41 | + """Delete the collection""" |
| 42 | + url = f"{self.host}/collections/{QDRANT_COLLECTION_NAME}" |
| 43 | + response = self.client.delete(url) |
| 44 | + # 404 is ok if collection doesn't exist |
| 45 | + if response.status_code not in [200, 404]: |
| 46 | + response.raise_for_status() |
| 47 | + |
| 48 | + def recreate(self, dataset: Dataset, collection_params): |
| 49 | + """Create collection with proper configuration""" |
| 50 | + url = f"{self.host}/collections/{QDRANT_COLLECTION_NAME}" |
| 51 | + |
| 52 | + # Build vectors configuration |
| 53 | + if dataset.config.type == "sparse": |
| 54 | + vectors_config = {} |
| 55 | + sparse_vectors_config = { |
| 56 | + "sparse": { |
| 57 | + "index": { |
| 58 | + "on_disk": False, |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + else: |
| 63 | + is_vectors_on_disk = self.collection_params.get("vectors_config", {}).get( |
| 64 | + "on_disk", False |
| 65 | + ) |
| 66 | + self.collection_params.pop("vectors_config", None) |
| 67 | + |
| 68 | + vectors_config = { |
| 69 | + "size": dataset.config.vector_size, |
| 70 | + "distance": self.DISTANCE_MAPPING.get(dataset.config.distance), |
| 71 | + "on_disk": is_vectors_on_disk, |
| 72 | + } |
| 73 | + sparse_vectors_config = None |
| 74 | + |
| 75 | + payload_index_params = self.collection_params.pop("payload_index_params", {}) |
| 76 | + if not set(payload_index_params.keys()).issubset(dataset.config.schema.keys()): |
| 77 | + raise ValueError("payload_index_params are not found in dataset schema") |
| 78 | + |
| 79 | + # Set optimizers config - disable index building during upload by default |
| 80 | + optimizers_config = self.collection_params.setdefault("optimizers_config", {}) |
| 81 | + optimizers_config.setdefault("max_optimization_threads", 0) |
| 82 | + |
| 83 | + # Build the collection creation payload |
| 84 | + payload = {} |
| 85 | + if vectors_config: |
| 86 | + payload["vectors"] = vectors_config |
| 87 | + if sparse_vectors_config: |
| 88 | + payload["sparse_vectors"] = sparse_vectors_config |
| 89 | + |
| 90 | + for key, value in self.collection_params.items(): |
| 91 | + payload[key] = value |
| 92 | + |
| 93 | + response = self.client.put(url, json=payload) |
| 94 | + response.raise_for_status() |
| 95 | + |
| 96 | + for field_name, field_type in dataset.config.schema.items(): |
| 97 | + self._create_payload_index(field_name, field_type, payload_index_params) |
| 98 | + |
| 99 | + def _create_payload_index( |
| 100 | + self, field_name: str, field_type: str, payload_index_params: dict |
| 101 | + ): |
| 102 | + """Create a payload index for a specific field""" |
| 103 | + url = f"{self.host}/collections/{QDRANT_COLLECTION_NAME}/index" |
| 104 | + |
| 105 | + # Build the field schema based on type |
| 106 | + if field_type in ["keyword", "uuid"]: |
| 107 | + field_schema = { |
| 108 | + "type": self.INDEX_TYPE_MAPPING.get(field_type, "keyword"), |
| 109 | + } |
| 110 | + |
| 111 | + # Add optional parameters if provided |
| 112 | + params = payload_index_params.get(field_name, {}) |
| 113 | + if "is_tenant" in params and params["is_tenant"] is not None: |
| 114 | + field_schema["is_tenant"] = params["is_tenant"] |
| 115 | + if "on_disk" in params and params["on_disk"] is not None: |
| 116 | + field_schema["on_disk"] = params["on_disk"] |
| 117 | + else: |
| 118 | + # For other types, just use the type string |
| 119 | + field_schema = self.INDEX_TYPE_MAPPING.get(field_type, field_type) |
| 120 | + |
| 121 | + payload = { |
| 122 | + "field_name": field_name, |
| 123 | + "field_schema": field_schema, |
| 124 | + } |
| 125 | + |
| 126 | + response = self.client.put(url, json=payload) |
| 127 | + response.raise_for_status() |
| 128 | + |
| 129 | + def delete_client(self): |
| 130 | + """Cleanup HTTP client""" |
| 131 | + if hasattr(self, "client") and self.client is not None: |
| 132 | + self.client.close() |
0 commit comments