-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add indexing methods * lint * respond to comments * Update test_cloudsql_vectorstore_index.py * fix tests * Update src/langchain_google_cloud_sql_pg/cloudsql_vectorstore.py Co-authored-by: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com> * Update src/langchain_google_cloud_sql_pg/cloudsql_vectorstore.py Co-authored-by: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com> * fix --------- Co-authored-by: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com>
- Loading branch information
1 parent
89a9a13
commit 8eae440
Showing
4 changed files
with
189 additions
and
5 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
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,127 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import json | ||
import os | ||
import uuid | ||
from typing import List | ||
|
||
import pytest | ||
import pytest_asyncio | ||
from langchain_community.embeddings import DeterministicFakeEmbedding | ||
from langchain_core.documents import Document | ||
|
||
from langchain_google_cloud_sql_pg import CloudSQLVectorStore, Column, PostgreSQLEngine | ||
from langchain_google_cloud_sql_pg.indexes import ( | ||
DEFAULT_INDEX_NAME, | ||
DistanceStrategy, | ||
HNSWIndex, | ||
IVFFlatIndex, | ||
) | ||
|
||
DEFAULT_TABLE = "test_table" + str(uuid.uuid4()).replace("-", "_") | ||
CUSTOM_TABLE = "test_table_custom" + str(uuid.uuid4()).replace("-", "_") | ||
VECTOR_SIZE = 768 | ||
|
||
embeddings_service = DeterministicFakeEmbedding(size=VECTOR_SIZE) | ||
|
||
texts = ["foo", "bar", "baz"] | ||
ids = [str(uuid.uuid4()) for i in range(len(texts))] | ||
metadatas = [{"page": str(i), "source": "google.com"} for i in range(len(texts))] | ||
docs = [ | ||
Document(page_content=texts[i], metadata=metadatas[i]) for i in range(len(texts)) | ||
] | ||
|
||
embeddings = [embeddings_service.embed_query("foo") for i in range(len(texts))] | ||
|
||
|
||
def get_env_var(key: str, desc: str) -> str: | ||
v = os.environ.get(key) | ||
if v is None: | ||
raise ValueError(f"Must set env var {key} to: {desc}") | ||
return v | ||
|
||
|
||
@pytest.mark.asyncio(scope="class") | ||
class TestIndex: | ||
@pytest.fixture(scope="module") | ||
def db_project(self) -> str: | ||
return get_env_var("PROJECT_ID", "project id for google cloud") | ||
|
||
@pytest.fixture(scope="module") | ||
def db_region(self) -> str: | ||
return get_env_var("REGION", "region for cloud sql instance") | ||
|
||
@pytest.fixture(scope="module") | ||
def db_instance(self) -> str: | ||
return get_env_var("INSTANCE_ID", "instance for cloud sql") | ||
|
||
@pytest.fixture(scope="module") | ||
def db_name(self) -> str: | ||
return get_env_var("DATABASE_ID", "instance for cloud sql") | ||
|
||
@pytest_asyncio.fixture(scope="class") | ||
async def engine(self, db_project, db_region, db_instance, db_name): | ||
engine = await PostgreSQLEngine.afrom_instance( | ||
project_id=db_project, | ||
instance=db_instance, | ||
region=db_region, | ||
database=db_name, | ||
) | ||
yield engine | ||
|
||
@pytest_asyncio.fixture(scope="class") | ||
async def vs(self, engine): | ||
await engine.init_vectorstore_table(DEFAULT_TABLE, VECTOR_SIZE) | ||
vs = await CloudSQLVectorStore.create( | ||
engine, | ||
embedding_service=embeddings_service, | ||
table_name=DEFAULT_TABLE, | ||
) | ||
|
||
await vs.aadd_texts(texts, ids=ids) | ||
await vs.adrop_vector_index() | ||
yield vs | ||
await engine._aexecute(f"DROP TABLE IF EXISTS {DEFAULT_TABLE}") | ||
await engine._engine.dispose() | ||
|
||
async def test_aapply_vector_index(self, vs): | ||
index = HNSWIndex() | ||
await vs.aapply_vector_index(index) | ||
assert await vs.is_valid_index(DEFAULT_INDEX_NAME) | ||
|
||
async def test_areindex(self, vs): | ||
if not await vs.is_valid_index(DEFAULT_INDEX_NAME): | ||
index = HNSWIndex() | ||
await vs.aapply_vector_index(index) | ||
await vs.areindex() | ||
await vs.areindex(DEFAULT_INDEX_NAME) | ||
assert await vs.is_valid_index(DEFAULT_INDEX_NAME) | ||
|
||
async def test_dropindex(self, vs): | ||
await vs.adrop_vector_index() | ||
result = await vs.is_valid_index(DEFAULT_INDEX_NAME) | ||
assert not result | ||
|
||
async def test_aapply_vector_index_ivfflat(self, vs): | ||
index = IVFFlatIndex(distance_strategy=DistanceStrategy.EUCLIDEAN) | ||
await vs.aapply_vector_index(index, concurrently=True) | ||
assert await vs.is_valid_index(DEFAULT_INDEX_NAME) | ||
index = IVFFlatIndex( | ||
name="secondindex", | ||
distance_strategy=DistanceStrategy.INNER_PRODUCT, | ||
) | ||
await vs.aapply_vector_index(index) | ||
assert await vs.is_valid_index("secondindex") | ||
await vs.adrop_vector_index("secondindex") |