Skip to content

Commit

Permalink
feature: adds RedisVectorStore::from_existing_index class method
Browse files Browse the repository at this point in the history
  • Loading branch information
bsbodden committed Jul 10, 2024
1 parent cd643a5 commit 78a03c3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
14 changes: 14 additions & 0 deletions libs/redis/langchain_redis/vectorstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,20 @@ def from_documents(
**kwargs,
)

@classmethod
def from_existing_index(
cls,
index_name: str,
embedding: Embeddings,
**kwargs: Any,
) -> RedisVectorStore:
"""Create a RedisVectorStore from an existing Redis Search Index."""
config = RedisConfig.from_kwargs(**kwargs)
config.index_name = index_name
config.from_existing = True

return RedisVectorStore(embedding, config=config)

def delete(self, ids: Optional[List[str]] = None, **kwargs: Any) -> Optional[bool]:
"""Delete keys from the vector store."""
return self._index.delete(ids)
Expand Down
27 changes: 27 additions & 0 deletions libs/redis/tests/integration_tests/test_vectorstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,33 @@ def test_redis_from_existing(texts: List[str], redis_url: str) -> None:
assert output == TEST_SINGLE_RESULT


def test_redis_from_existing_with_class_method(
texts: List[str], redis_url: str
) -> None:
"""Test adding a new document"""
# Create a unique index name for testing
index_name = f"test_index_{str(ULID())}"
vector_store = RedisVectorStore.from_texts(
texts,
OpenAIEmbeddings(),
index_name=index_name,
key_prefix="tst3",
redis_url=redis_url,
)

# write schema for the next test
vector_store.index.schema.to_yaml("test_schema.yml")

# Test creating from an existing
vector_store2 = RedisVectorStore.from_existing_index(
index_name=index_name,
embedding=OpenAIEmbeddings(),
redis_url=redis_url,
)
output = vector_store2.similarity_search("foo", k=1, return_metadata=False)
assert output == TEST_SINGLE_RESULT


def test_redis_add_texts_to_existing(redis_url: str) -> None:
"""Test adding a new document"""
# Test creating from an existing with yaml from file
Expand Down

0 comments on commit 78a03c3

Please sign in to comment.