-
Notifications
You must be signed in to change notification settings - Fork 162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for pgdiskann client #388
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e931034
Add pgdiskann client
wahajali 50ff354
Add CLI support in pgdiskann
Sheharyar570 d081997
Merge pull request #5 from EmumbaOrg/add-pgdiskann-cli-support
wahajali 69ce9f4
add pgdiskann load config in frontend.
Sheharyar570 89352d9
Merge branch 'main' into diskann-support
wahajali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import click | ||
import os | ||
from pydantic import SecretStr | ||
|
||
from ....cli.cli import ( | ||
CommonTypedDict, | ||
cli, | ||
click_parameter_decorators_from_typed_dict, | ||
run, | ||
) | ||
from typing import Annotated, Optional, Unpack | ||
from vectordb_bench.backend.clients import DB | ||
|
||
|
||
class PgDiskAnnTypedDict(CommonTypedDict): | ||
user_name: Annotated[ | ||
str, click.option("--user-name", type=str, help="Db username", required=True) | ||
] | ||
password: Annotated[ | ||
str, | ||
click.option("--password", | ||
type=str, | ||
help="Postgres database password", | ||
default=lambda: os.environ.get("POSTGRES_PASSWORD", ""), | ||
show_default="$POSTGRES_PASSWORD", | ||
), | ||
] | ||
|
||
host: Annotated[ | ||
str, click.option("--host", type=str, help="Db host", required=True) | ||
] | ||
db_name: Annotated[ | ||
str, click.option("--db-name", type=str, help="Db name", required=True) | ||
] | ||
max_neighbors: Annotated[ | ||
int, | ||
click.option( | ||
"--max-neighbors", type=int, help="PgDiskAnn max neighbors", | ||
), | ||
] | ||
l_value_ib: Annotated[ | ||
int, | ||
click.option( | ||
"--l-value-ib", type=int, help="PgDiskAnn l_value_ib", | ||
), | ||
] | ||
l_value_is: Annotated[ | ||
float, | ||
click.option( | ||
"--l-value-is", type=float, help="PgDiskAnn l_value_is", | ||
), | ||
] | ||
maintenance_work_mem: Annotated[ | ||
Optional[str], | ||
click.option( | ||
"--maintenance-work-mem", | ||
type=str, | ||
help="Sets the maximum memory to be used for maintenance operations (index creation). " | ||
"Can be entered as string with unit like '64GB' or as an integer number of KB." | ||
"This will set the parameters: max_parallel_maintenance_workers," | ||
" max_parallel_workers & table(parallel_workers)", | ||
required=False, | ||
), | ||
] | ||
max_parallel_workers: Annotated[ | ||
Optional[int], | ||
click.option( | ||
"--max-parallel-workers", | ||
type=int, | ||
help="Sets the maximum number of parallel processes per maintenance operation (index creation)", | ||
required=False, | ||
), | ||
] | ||
|
||
@cli.command() | ||
@click_parameter_decorators_from_typed_dict(PgDiskAnnTypedDict) | ||
def PgDiskAnn( | ||
**parameters: Unpack[PgDiskAnnTypedDict], | ||
): | ||
from .config import PgDiskANNConfig, PgDiskANNImplConfig | ||
|
||
run( | ||
db=DB.PgDiskANN, | ||
db_config=PgDiskANNConfig( | ||
db_label=parameters["db_label"], | ||
user_name=SecretStr(parameters["user_name"]), | ||
password=SecretStr(parameters["password"]), | ||
host=parameters["host"], | ||
db_name=parameters["db_name"], | ||
), | ||
db_case_config=PgDiskANNImplConfig( | ||
max_neighbors=parameters["max_neighbors"], | ||
l_value_ib=parameters["l_value_ib"], | ||
l_value_is=parameters["l_value_is"], | ||
max_parallel_workers=parameters["max_parallel_workers"], | ||
maintenance_work_mem=parameters["maintenance_work_mem"], | ||
), | ||
**parameters, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
from abc import abstractmethod | ||
from typing import Any, Mapping, Optional, Sequence, TypedDict | ||
from pydantic import BaseModel, SecretStr | ||
from typing_extensions import LiteralString | ||
from ..api import DBCaseConfig, DBConfig, IndexType, MetricType | ||
|
||
POSTGRE_URL_PLACEHOLDER = "postgresql://%s:%s@%s/%s" | ||
|
||
|
||
class PgDiskANNConfigDict(TypedDict): | ||
"""These keys will be directly used as kwargs in psycopg connection string, | ||
so the names must match exactly psycopg API""" | ||
|
||
user: str | ||
password: str | ||
host: str | ||
port: int | ||
dbname: str | ||
|
||
|
||
class PgDiskANNConfig(DBConfig): | ||
user_name: SecretStr = SecretStr("postgres") | ||
password: SecretStr | ||
host: str = "localhost" | ||
port: int = 5432 | ||
db_name: str | ||
|
||
def to_dict(self) -> PgDiskANNConfigDict: | ||
user_str = self.user_name.get_secret_value() | ||
pwd_str = self.password.get_secret_value() | ||
return { | ||
"host": self.host, | ||
"port": self.port, | ||
"dbname": self.db_name, | ||
"user": user_str, | ||
"password": pwd_str, | ||
} | ||
|
||
|
||
class PgDiskANNIndexConfig(BaseModel, DBCaseConfig): | ||
metric_type: MetricType | None = None | ||
create_index_before_load: bool = False | ||
create_index_after_load: bool = True | ||
maintenance_work_mem: Optional[str] | ||
max_parallel_workers: Optional[int] | ||
|
||
def parse_metric(self) -> str: | ||
if self.metric_type == MetricType.L2: | ||
return "vector_l2_ops" | ||
elif self.metric_type == MetricType.IP: | ||
return "vector_ip_ops" | ||
return "vector_cosine_ops" | ||
|
||
def parse_metric_fun_op(self) -> LiteralString: | ||
if self.metric_type == MetricType.L2: | ||
return "<->" | ||
elif self.metric_type == MetricType.IP: | ||
return "<#>" | ||
return "<=>" | ||
|
||
def parse_metric_fun_str(self) -> str: | ||
if self.metric_type == MetricType.L2: | ||
return "l2_distance" | ||
elif self.metric_type == MetricType.IP: | ||
return "max_inner_product" | ||
return "cosine_distance" | ||
|
||
@abstractmethod | ||
def index_param(self) -> dict: | ||
... | ||
|
||
@abstractmethod | ||
def search_param(self) -> dict: | ||
... | ||
|
||
@abstractmethod | ||
def session_param(self) -> dict: | ||
... | ||
|
||
@staticmethod | ||
def _optionally_build_with_options(with_options: Mapping[str, Any]) -> Sequence[dict[str, Any]]: | ||
"""Walk through mappings, creating a List of {key1 = value} pairs. That will be used to build a where clause""" | ||
options = [] | ||
for option_name, value in with_options.items(): | ||
if value is not None: | ||
options.append( | ||
{ | ||
"option_name": option_name, | ||
"val": str(value), | ||
} | ||
) | ||
return options | ||
|
||
@staticmethod | ||
def _optionally_build_set_options( | ||
set_mapping: Mapping[str, Any] | ||
) -> Sequence[dict[str, Any]]: | ||
"""Walk through options, creating 'SET 'key1 = "value1";' list""" | ||
session_options = [] | ||
for setting_name, value in set_mapping.items(): | ||
if value: | ||
session_options.append( | ||
{"parameter": { | ||
"setting_name": setting_name, | ||
"val": str(value), | ||
}, | ||
} | ||
) | ||
return session_options | ||
|
||
|
||
class PgDiskANNImplConfig(PgDiskANNIndexConfig): | ||
index: IndexType = IndexType.DISKANN | ||
max_neighbors: int | None | ||
l_value_ib: int | None | ||
l_value_is: float | None | ||
maintenance_work_mem: Optional[str] = None | ||
max_parallel_workers: Optional[int] = None | ||
|
||
def index_param(self) -> dict: | ||
return { | ||
"metric": self.parse_metric(), | ||
"index_type": self.index.value, | ||
"options": { | ||
"max_neighbors": self.max_neighbors, | ||
"l_value_ib": self.l_value_ib, | ||
}, | ||
"maintenance_work_mem": self.maintenance_work_mem, | ||
"max_parallel_workers": self.max_parallel_workers, | ||
} | ||
|
||
def search_param(self) -> dict: | ||
return { | ||
"metric": self.parse_metric(), | ||
"metric_fun_op": self.parse_metric_fun_op(), | ||
} | ||
|
||
def session_param(self) -> dict: | ||
return { | ||
"diskann.l_value_is": self.l_value_is, | ||
} | ||
|
||
_pgdiskann_case_config = { | ||
IndexType.DISKANN: PgDiskANNImplConfig, | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These help text lines (60-61) belong with "--max-parallel-workers", this is also an issue an issue with the pgvectory cli.py.