Skip to content
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

feat: Add table name to default index name #182

Merged
merged 5 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/langchain_google_alloydb_pg/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ class DistanceStrategy(StrategyMixin, enum.Enum):


DEFAULT_DISTANCE_STRATEGY: DistanceStrategy = DistanceStrategy.COSINE_DISTANCE
DEFAULT_INDEX_NAME: str = "langchainvectorindex"
DEFAULT_INDEX_NAME_SUFFIX: str = "langchainvectorindex"


@dataclass
class BaseIndex(ABC):
name: str = DEFAULT_INDEX_NAME
name: Optional[str] = None
index_type: str = "base"
distance_strategy: DistanceStrategy = field(
default_factory=lambda: DistanceStrategy.COSINE_DISTANCE
Expand Down
16 changes: 11 additions & 5 deletions src/langchain_google_alloydb_pg/vectorstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from .engine import AlloyDBEngine
from .indexes import (
DEFAULT_DISTANCE_STRATEGY,
DEFAULT_INDEX_NAME,
DEFAULT_INDEX_NAME_SUFFIX,
BaseIndex,
DistanceStrategy,
ExactNearestNeighbor,
Expand Down Expand Up @@ -759,28 +759,34 @@ async def aapply_vector_index(

filter = f"WHERE ({index.partial_indexes})" if index.partial_indexes else ""
params = "WITH " + index.index_options()
name = name or index.name
if name is None:
if index.name == None:
index.name = self.table_name + DEFAULT_INDEX_NAME_SUFFIX
name = index.name
stmt = f"CREATE INDEX {'CONCURRENTLY' if concurrently else ''} {name} ON \"{self.table_name}\" USING {index.index_type} ({self.embedding_column} {function}) {params} {filter};"
if concurrently:
await self.engine._aexecute_outside_tx(stmt)
else:
await self.engine._aexecute(stmt)

async def areindex(self, index_name: str = DEFAULT_INDEX_NAME) -> None:
async def areindex(self, index_name: Optional[str] = None) -> None:
index_name = index_name or self.table_name + DEFAULT_INDEX_NAME_SUFFIX
query = f"REINDEX INDEX {index_name};"
await self.engine._aexecute(query)

async def adrop_vector_index(
self,
index_name: str = DEFAULT_INDEX_NAME,
index_name: Optional[str] = None,
) -> None:
index_name = index_name or self.table_name + DEFAULT_INDEX_NAME_SUFFIX
query = f"DROP INDEX IF EXISTS {index_name};"
await self.engine._aexecute(query)

async def is_valid_index(
self,
index_name: str = DEFAULT_INDEX_NAME,
index_name: Optional[str] = None,
) -> bool:
index_name = index_name or self.table_name + DEFAULT_INDEX_NAME_SUFFIX
query = f"""
SELECT tablename, indexname
FROM pg_indexes
Expand Down
3 changes: 2 additions & 1 deletion tests/test_alloydb_vectorstore_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from langchain_google_alloydb_pg import AlloyDBEngine, AlloyDBVectorStore
from langchain_google_alloydb_pg.indexes import (
DEFAULT_INDEX_NAME,
DEFAULT_INDEX_NAME_SUFFIX,
DistanceStrategy,
HNSWIndex,
IVFFlatIndex,
Expand All @@ -34,6 +34,7 @@

DEFAULT_TABLE = "test_table" + str(uuid.uuid4()).replace("-", "_")
CUSTOM_TABLE = "test_table_custom" + str(uuid.uuid4()).replace("-", "_")
DEFAULT_INDEX_NAME = DEFAULT_TABLE + DEFAULT_INDEX_NAME_SUFFIX
VECTOR_SIZE = 768

embeddings_service = DeterministicFakeEmbedding(size=VECTOR_SIZE)
Expand Down