Skip to content

Commit

Permalink
Redis: Fix keys (#10413)
Browse files Browse the repository at this point in the history
- Description: Fixes user issue with custom keys for ``from_texts`` and
``from_documents`` methods.
  - Issue: #10411 
  - Tag maintainer: @baskaryan 
  - Twitter handle: @Spartee
  • Loading branch information
Sam Partee authored Sep 10, 2023
1 parent ee3f950 commit d09ef9e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion libs/langchain/langchain/vectorstores/redis/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ def from_texts_return_keys(
if "generate" in kwargs:
kwargs.pop("generate")

# see if the user specified keys
keys = None
if "keys" in kwargs:
keys = kwargs.pop("keys")

# Name of the search index if not given
if not index_name:
index_name = uuid.uuid4().hex
Expand Down Expand Up @@ -422,7 +427,7 @@ def from_texts_return_keys(
instance._create_index(dim=len(embeddings[0]))

# Add data to Redis
keys = instance.add_texts(texts, metadatas, embeddings)
keys = instance.add_texts(texts, metadatas, embeddings, keys=keys)
return instance, keys

@classmethod
Expand Down
26 changes: 26 additions & 0 deletions libs/langchain/tests/integration_tests/vectorstores/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,32 @@ def test_redis_from_documents(texts: List[str]) -> None:
assert drop(docsearch.index_name)


def test_custom_keys(texts: List[str]) -> None:
keys_in = ["test_key_1", "test_key_2", "test_key_3"]
docsearch, keys_out = Redis.from_texts_return_keys(
texts, FakeEmbeddings(), redis_url=TEST_REDIS_URL, keys=keys_in
)
assert keys_in == keys_out
assert drop(docsearch.index_name)


def test_custom_keys_from_docs(texts: List[str]) -> None:
keys_in = ["test_key_1", "test_key_2", "test_key_3"]
docs = [Document(page_content=t, metadata={"a": "b"}) for t in texts]

docsearch = Redis.from_documents(
docs, FakeEmbeddings(), redis_url=TEST_REDIS_URL, keys=keys_in
)
client = docsearch.client
# test keys are correct
assert client.hget("test_key_1", "content")
# test metadata is stored
assert client.hget("test_key_1", "a") == bytes("b", "utf-8")
# test all keys are stored
assert client.hget("test_key_2", "content")
assert drop(docsearch.index_name)


# -- test filters -- #


Expand Down

0 comments on commit d09ef9e

Please sign in to comment.