|
| 1 | +import os |
| 2 | +from typing import Annotated, Unpack |
| 3 | + |
| 4 | +import click |
| 5 | +from pydantic import SecretStr |
| 6 | + |
| 7 | +from vectordb_bench.backend.clients import DB |
| 8 | + |
| 9 | +from ....cli.cli import ( |
| 10 | + CommonTypedDict, |
| 11 | + cli, |
| 12 | + click_parameter_decorators_from_typed_dict, |
| 13 | + run, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class VectorChordTypedDict(CommonTypedDict): |
| 18 | + user_name: Annotated[ |
| 19 | + str, |
| 20 | + click.option("--user-name", type=str, help="Db username", required=True), |
| 21 | + ] |
| 22 | + password: Annotated[ |
| 23 | + str, |
| 24 | + click.option( |
| 25 | + "--password", |
| 26 | + type=str, |
| 27 | + help="Postgres database password", |
| 28 | + default=lambda: os.environ.get("POSTGRES_PASSWORD", ""), |
| 29 | + show_default="$POSTGRES_PASSWORD", |
| 30 | + ), |
| 31 | + ] |
| 32 | + |
| 33 | + host: Annotated[str, click.option("--host", type=str, help="Db host", required=True)] |
| 34 | + port: Annotated[ |
| 35 | + int, |
| 36 | + click.option( |
| 37 | + "--port", |
| 38 | + type=int, |
| 39 | + help="Postgres database port", |
| 40 | + default=5432, |
| 41 | + show_default=True, |
| 42 | + required=False, |
| 43 | + ), |
| 44 | + ] |
| 45 | + db_name: Annotated[str, click.option("--db-name", type=str, help="Db name", required=True)] |
| 46 | + max_parallel_workers: Annotated[ |
| 47 | + int | None, |
| 48 | + click.option( |
| 49 | + "--max-parallel-workers", |
| 50 | + type=int, |
| 51 | + help="Sets the maximum number of parallel workers for index creation", |
| 52 | + required=False, |
| 53 | + ), |
| 54 | + ] |
| 55 | + quantization_type: Annotated[ |
| 56 | + str | None, |
| 57 | + click.option( |
| 58 | + "--quantization-type", |
| 59 | + type=click.Choice(["vector", "halfvec", "rabitq8", "rabitq4"]), |
| 60 | + help="Quantization type for vectors", |
| 61 | + default="vector", |
| 62 | + show_default=True, |
| 63 | + ), |
| 64 | + ] |
| 65 | + |
| 66 | + |
| 67 | +class VectorChordRQTypedDict(VectorChordTypedDict): |
| 68 | + lists: Annotated[ |
| 69 | + int | None, |
| 70 | + click.option( |
| 71 | + "--lists", |
| 72 | + type=int, |
| 73 | + help="Number of IVF lists for vchordrq index", |
| 74 | + ), |
| 75 | + ] |
| 76 | + probes: Annotated[ |
| 77 | + int | None, |
| 78 | + click.option( |
| 79 | + "--probes", |
| 80 | + type=int, |
| 81 | + help="Number of probes during search", |
| 82 | + default=10, |
| 83 | + show_default=True, |
| 84 | + ), |
| 85 | + ] |
| 86 | + epsilon: Annotated[ |
| 87 | + float | None, |
| 88 | + click.option( |
| 89 | + "--epsilon", |
| 90 | + type=float, |
| 91 | + help="Reranking precision factor (0.0-4.0, higher is more accurate but slower)", |
| 92 | + default=1.9, |
| 93 | + show_default=True, |
| 94 | + ), |
| 95 | + ] |
| 96 | + residual_quantization: Annotated[ |
| 97 | + bool, |
| 98 | + click.option( |
| 99 | + "--residual-quantization/--no-residual-quantization", |
| 100 | + type=bool, |
| 101 | + help="Enable residual quantization for improved accuracy", |
| 102 | + default=False, |
| 103 | + show_default=True, |
| 104 | + ), |
| 105 | + ] |
| 106 | + rerank_in_table: Annotated[ |
| 107 | + bool, |
| 108 | + click.option( |
| 109 | + "--rerank-in-table/--no-rerank-in-table", |
| 110 | + type=bool, |
| 111 | + help="Read vectors from table instead of storing in index (saves storage, degrades query performance)", |
| 112 | + default=False, |
| 113 | + show_default=True, |
| 114 | + ), |
| 115 | + ] |
| 116 | + spherical_centroids: Annotated[ |
| 117 | + bool, |
| 118 | + click.option( |
| 119 | + "--spherical-centroids/--no-spherical-centroids", |
| 120 | + type=bool, |
| 121 | + help="L2-normalize centroids during K-means (recommended for cosine/IP)", |
| 122 | + default=False, |
| 123 | + show_default=True, |
| 124 | + ), |
| 125 | + ] |
| 126 | + build_threads: Annotated[ |
| 127 | + int | None, |
| 128 | + click.option( |
| 129 | + "--build-threads", |
| 130 | + type=int, |
| 131 | + help="Number of threads for index building (range: 1-255)", |
| 132 | + ), |
| 133 | + ] |
| 134 | + degree_of_parallelism: Annotated[ |
| 135 | + int | None, |
| 136 | + click.option( |
| 137 | + "--degree-of-parallelism", |
| 138 | + type=int, |
| 139 | + help="Degree of parallelism for index build (range: 1-256, default: 32)", |
| 140 | + ), |
| 141 | + ] |
| 142 | + max_scan_tuples: Annotated[ |
| 143 | + int | None, |
| 144 | + click.option( |
| 145 | + "--max-scan-tuples", |
| 146 | + type=int, |
| 147 | + help="Max tuples to scan before stopping (-1 for unlimited)", |
| 148 | + ), |
| 149 | + ] |
| 150 | + |
| 151 | + |
| 152 | +@cli.command() |
| 153 | +@click_parameter_decorators_from_typed_dict(VectorChordRQTypedDict) |
| 154 | +def VectorChordRQ( |
| 155 | + **parameters: Unpack[VectorChordRQTypedDict], |
| 156 | +): |
| 157 | + from .config import VectorChordConfig, VectorChordRQConfig |
| 158 | + |
| 159 | + run( |
| 160 | + db=DB.VectorChord, |
| 161 | + db_config=VectorChordConfig( |
| 162 | + db_label=parameters["db_label"], |
| 163 | + user_name=SecretStr(parameters["user_name"]), |
| 164 | + password=SecretStr(parameters["password"]), |
| 165 | + host=parameters["host"], |
| 166 | + port=parameters["port"], |
| 167 | + db_name=parameters["db_name"], |
| 168 | + ), |
| 169 | + db_case_config=VectorChordRQConfig( |
| 170 | + quantization_type=parameters["quantization_type"], |
| 171 | + lists=parameters["lists"], |
| 172 | + probes=parameters["probes"], |
| 173 | + epsilon=parameters["epsilon"], |
| 174 | + residual_quantization=parameters["residual_quantization"], |
| 175 | + rerank_in_table=parameters["rerank_in_table"], |
| 176 | + spherical_centroids=parameters["spherical_centroids"], |
| 177 | + build_threads=parameters["build_threads"], |
| 178 | + degree_of_parallelism=parameters["degree_of_parallelism"], |
| 179 | + max_scan_tuples=parameters["max_scan_tuples"], |
| 180 | + max_parallel_workers=parameters["max_parallel_workers"], |
| 181 | + ), |
| 182 | + **parameters, |
| 183 | + ) |
| 184 | + |
| 185 | + |
| 186 | +class VectorChordGraphTypedDict(VectorChordTypedDict): |
| 187 | + m: Annotated[ |
| 188 | + int | None, |
| 189 | + click.option( |
| 190 | + "--m", |
| 191 | + type=int, |
| 192 | + help="Max neighbors per vertex (default: 32)", |
| 193 | + ), |
| 194 | + ] |
| 195 | + ef_construction: Annotated[ |
| 196 | + int | None, |
| 197 | + click.option( |
| 198 | + "--ef-construction", |
| 199 | + type=int, |
| 200 | + help="Dynamic list size during insertion (default: 64)", |
| 201 | + ), |
| 202 | + ] |
| 203 | + bits: Annotated[ |
| 204 | + int | None, |
| 205 | + click.option( |
| 206 | + "--bits", |
| 207 | + type=int, |
| 208 | + help="RaBitQ quantization ratio (1 or 2, default: 2)", |
| 209 | + ), |
| 210 | + ] |
| 211 | + ef_search: Annotated[ |
| 212 | + int | None, |
| 213 | + click.option( |
| 214 | + "--ef-search", |
| 215 | + type=int, |
| 216 | + help="Dynamic list size for search (default: 64)", |
| 217 | + default=64, |
| 218 | + show_default=True, |
| 219 | + ), |
| 220 | + ] |
| 221 | + beam_search: Annotated[ |
| 222 | + int | None, |
| 223 | + click.option( |
| 224 | + "--beam-search", |
| 225 | + type=int, |
| 226 | + help="Batch vertex access width during search (default: 1)", |
| 227 | + ), |
| 228 | + ] |
| 229 | + max_scan_tuples: Annotated[ |
| 230 | + int | None, |
| 231 | + click.option( |
| 232 | + "--max-scan-tuples", |
| 233 | + type=int, |
| 234 | + help="Max tuples to scan before stopping (-1 for unlimited)", |
| 235 | + ), |
| 236 | + ] |
| 237 | + |
| 238 | + |
| 239 | +@cli.command() |
| 240 | +@click_parameter_decorators_from_typed_dict(VectorChordGraphTypedDict) |
| 241 | +def VectorChordGraph( |
| 242 | + **parameters: Unpack[VectorChordGraphTypedDict], |
| 243 | +): |
| 244 | + from .config import VectorChordConfig, VectorChordGraphConfig |
| 245 | + |
| 246 | + run( |
| 247 | + db=DB.VectorChord, |
| 248 | + db_config=VectorChordConfig( |
| 249 | + db_label=parameters["db_label"], |
| 250 | + user_name=SecretStr(parameters["user_name"]), |
| 251 | + password=SecretStr(parameters["password"]), |
| 252 | + host=parameters["host"], |
| 253 | + port=parameters["port"], |
| 254 | + db_name=parameters["db_name"], |
| 255 | + ), |
| 256 | + db_case_config=VectorChordGraphConfig( |
| 257 | + quantization_type=parameters["quantization_type"], |
| 258 | + m=parameters["m"], |
| 259 | + ef_construction=parameters["ef_construction"], |
| 260 | + bits=parameters["bits"], |
| 261 | + ef_search=parameters["ef_search"], |
| 262 | + beam_search=parameters["beam_search"], |
| 263 | + max_parallel_workers=parameters["max_parallel_workers"], |
| 264 | + max_scan_tuples=parameters["max_scan_tuples"], |
| 265 | + ), |
| 266 | + **parameters, |
| 267 | + ) |
0 commit comments