|
| 1 | +from abc import abstractmethod |
| 2 | +from typing import TypedDict |
| 3 | +from pydantic import BaseModel, SecretStr |
| 4 | +from typing_extensions import LiteralString |
| 5 | +from ..api import DBCaseConfig, DBConfig, IndexType, MetricType |
| 6 | + |
| 7 | +POSTGRE_URL_PLACEHOLDER = "postgresql://%s:%s@%s/%s" |
| 8 | + |
| 9 | + |
| 10 | +class PgVectorScaleConfigDict(TypedDict): |
| 11 | + """These keys will be directly used as kwargs in psycopg connection string, |
| 12 | + so the names must match exactly psycopg API""" |
| 13 | + |
| 14 | + user: str |
| 15 | + password: str |
| 16 | + host: str |
| 17 | + port: int |
| 18 | + dbname: str |
| 19 | + |
| 20 | + |
| 21 | +class PgVectorScaleConfig(DBConfig): |
| 22 | + user_name: SecretStr = SecretStr("postgres") |
| 23 | + password: SecretStr |
| 24 | + host: str = "localhost" |
| 25 | + port: int = 5432 |
| 26 | + db_name: str |
| 27 | + |
| 28 | + def to_dict(self) -> PgVectorScaleConfigDict: |
| 29 | + user_str = self.user_name.get_secret_value() |
| 30 | + pwd_str = self.password.get_secret_value() |
| 31 | + return { |
| 32 | + "host": self.host, |
| 33 | + "port": self.port, |
| 34 | + "dbname": self.db_name, |
| 35 | + "user": user_str, |
| 36 | + "password": pwd_str, |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | +class PgVectorScaleIndexConfig(BaseModel, DBCaseConfig): |
| 41 | + metric_type: MetricType | None = None |
| 42 | + create_index_before_load: bool = False |
| 43 | + create_index_after_load: bool = True |
| 44 | + |
| 45 | + def parse_metric(self) -> str: |
| 46 | + if self.metric_type == MetricType.COSINE: |
| 47 | + return "vector_cosine_ops" |
| 48 | + return "" |
| 49 | + |
| 50 | + def parse_metric_fun_op(self) -> LiteralString: |
| 51 | + if self.metric_type == MetricType.COSINE: |
| 52 | + return "<=>" |
| 53 | + return "" |
| 54 | + |
| 55 | + def parse_metric_fun_str(self) -> str: |
| 56 | + if self.metric_type == MetricType.COSINE: |
| 57 | + return "cosine_distance" |
| 58 | + return "" |
| 59 | + |
| 60 | + @abstractmethod |
| 61 | + def index_param(self) -> dict: |
| 62 | + ... |
| 63 | + |
| 64 | + @abstractmethod |
| 65 | + def search_param(self) -> dict: |
| 66 | + ... |
| 67 | + |
| 68 | + @abstractmethod |
| 69 | + def session_param(self) -> dict: |
| 70 | + ... |
| 71 | + |
| 72 | + |
| 73 | +class PgVectorScaleStreamingDiskANNConfig(PgVectorScaleIndexConfig): |
| 74 | + index: IndexType = IndexType.STREAMING_DISKANN |
| 75 | + storage_layout: str | None |
| 76 | + num_neighbors: int | None |
| 77 | + search_list_size: int | None |
| 78 | + max_alpha: float | None |
| 79 | + num_dimensions: int | None |
| 80 | + num_bits_per_dimension: int | None |
| 81 | + query_search_list_size: int | None |
| 82 | + query_rescore: int | None |
| 83 | + |
| 84 | + def index_param(self) -> dict: |
| 85 | + return { |
| 86 | + "metric": self.parse_metric(), |
| 87 | + "index_type": self.index.value, |
| 88 | + "options": { |
| 89 | + "storage_layout": self.storage_layout, |
| 90 | + "num_neighbors": self.num_neighbors, |
| 91 | + "search_list_size": self.search_list_size, |
| 92 | + "max_alpha": self.max_alpha, |
| 93 | + "num_dimensions": self.num_dimensions, |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + def search_param(self) -> dict: |
| 98 | + return { |
| 99 | + "metric": self.parse_metric(), |
| 100 | + "metric_fun_op": self.parse_metric_fun_op(), |
| 101 | + } |
| 102 | + |
| 103 | + def session_param(self) -> dict: |
| 104 | + return { |
| 105 | + "diskann.query_search_list_size": self.query_search_list_size, |
| 106 | + "diskann.query_rescore": self.query_rescore, |
| 107 | + } |
| 108 | + |
| 109 | +_pgvectorscale_case_config = { |
| 110 | + IndexType.STREAMING_DISKANN: PgVectorScaleStreamingDiskANNConfig, |
| 111 | +} |
0 commit comments