-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Baswanth Vegunta
committed
Jul 3, 2024
1 parent
09306a0
commit 6da877d
Showing
7 changed files
with
403 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ __MACOSX | |
build/ | ||
venv/ | ||
.idea/ | ||
results/ |
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,80 @@ | ||
from typing import Annotated, TypedDict, Unpack | ||
|
||
import click | ||
from pydantic import SecretStr | ||
|
||
from ....cli.cli import ( | ||
CommonTypedDict, | ||
HNSWFlavor2, | ||
cli, | ||
click_parameter_decorators_from_typed_dict, | ||
run, | ||
) | ||
from .. import DB | ||
|
||
|
||
class MemoryDBTypedDict(TypedDict): | ||
host: Annotated[ | ||
str, click.option("--host", type=str, help="Db host", required=True) | ||
] | ||
password: Annotated[str, click.option("--password", type=str, help="Db password")] | ||
port: Annotated[int, click.option("--port", type=int, default=6379, help="Db Port")] | ||
ssl: Annotated[ | ||
bool, | ||
click.option( | ||
"--ssl/--no-ssl", | ||
is_flag=True, | ||
show_default=True, | ||
default=True, | ||
help="Enable or disable SSL for Redis", | ||
), | ||
] | ||
ssl_ca_certs: Annotated[ | ||
str, | ||
click.option( | ||
"--ssl-ca-certs", | ||
show_default=True, | ||
help="Path to certificate authority file to use for SSL", | ||
), | ||
] | ||
cmd: Annotated[ | ||
bool, | ||
click.option( | ||
"--cmd", | ||
is_flag=True, | ||
show_default=True, | ||
default=False, | ||
help="Cluster Mode Disabled (CMD) for Redis doesn't use Cluster conn", | ||
), | ||
] | ||
|
||
|
||
class MemoryDBHNSWTypedDict(CommonTypedDict, MemoryDBTypedDict, HNSWFlavor2): | ||
... | ||
|
||
|
||
@cli.command() | ||
@click_parameter_decorators_from_typed_dict(MemoryDBHNSWTypedDict) | ||
def MemoryDB(**parameters: Unpack[MemoryDBHNSWTypedDict]): | ||
from .config import MemoryDBConfig, MemoryDBHNSWConfig | ||
|
||
run( | ||
db=DB.MemoryDB, | ||
db_config=MemoryDBConfig( | ||
db_label=parameters["db_label"], | ||
password=SecretStr(parameters["password"]) | ||
if parameters["password"] | ||
else None, | ||
host=SecretStr(parameters["host"]), | ||
port=parameters["port"], | ||
ssl=parameters["ssl"], | ||
ssl_ca_certs=parameters["ssl_ca_certs"], | ||
cmd=parameters["cmd"], | ||
), | ||
db_case_config=MemoryDBHNSWConfig( | ||
M=parameters["m"], | ||
ef_construction=parameters["ef_construction"], | ||
ef_runtime=parameters["ef_runtime"], | ||
), | ||
**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,54 @@ | ||
from pydantic import BaseModel, SecretStr | ||
|
||
from ..api import DBCaseConfig, DBConfig, IndexType, MetricType | ||
|
||
|
||
class MemoryDBConfig(DBConfig): | ||
host: SecretStr | ||
password: SecretStr | None = None | ||
port: int | None = None | ||
ssl: bool | None = None | ||
cmd: bool | None = None | ||
ssl_ca_certs: str | None = None | ||
|
||
def to_dict(self) -> dict: | ||
return { | ||
"host": self.host.get_secret_value(), | ||
"port": self.port, | ||
"password": self.password.get_secret_value() if self.password else None, | ||
"ssl": self.ssl, | ||
"cmd": self.cmd, | ||
"ssl_ca_certs": self.ssl_ca_certs, | ||
} | ||
|
||
|
||
class MemoryDBIndexConfig(BaseModel, DBCaseConfig): | ||
metric_type: MetricType | None = None | ||
insert_batch_size: int | None = 10 # Adjust this as needed, but don't make too big | ||
|
||
def parse_metric(self) -> str: | ||
if self.metric_type == MetricType.L2: | ||
return "l2" | ||
elif self.metric_type == MetricType.IP: | ||
return "ip" | ||
return "cosine" | ||
|
||
|
||
class MemoryDBHNSWConfig(MemoryDBIndexConfig): | ||
M: int | None = 16 | ||
ef_construction: int | None = 64 | ||
ef_runtime: int | None = 10 | ||
index: IndexType = IndexType.HNSW | ||
|
||
def index_param(self) -> dict: | ||
return { | ||
"metric": self.parse_metric(), | ||
"index_type": self.index.value, | ||
"m": self.M, | ||
"ef_construction": self.ef_construction, | ||
} | ||
|
||
def search_param(self) -> dict: | ||
return { | ||
"ef_runtime": self.ef_runtime, | ||
} |
Oops, something went wrong.