Skip to content

Commit

Permalink
Harrison/from keys redis (#4653)
Browse files Browse the repository at this point in the history
Co-authored-by: Christoph Kahl <christoph@zauberware.com>
  • Loading branch information
hwchase17 and ChrKahl committed May 14, 2023
1 parent e781ff9 commit 9ba3a79
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions langchain/vectorstores/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def _similarity_search_with_relevance_scores(
return [(doc, self.relevance_score_fn(score)) for doc, score in docs_and_scores]

@classmethod
def from_texts(
def from_texts_return_keys(
cls: Type[Redis],
texts: List[str],
embedding: Embeddings,
Expand All @@ -369,7 +369,7 @@ def from_texts(
vector_key: str = "content_vector",
distance_metric: REDIS_DISTANCE_METRICS = "COSINE",
**kwargs: Any,
) -> Redis:
) -> Tuple[Redis, List[str]]:
"""Create a Redis vectorstore from raw documents.
This is a user-friendly interface that:
1. Embeds documents.
Expand Down Expand Up @@ -414,7 +414,49 @@ def from_texts(
instance._create_index(dim=len(embeddings[0]), distance_metric=distance_metric)

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

@classmethod
def from_texts(
cls: Type[Redis],
texts: List[str],
embedding: Embeddings,
metadatas: Optional[List[dict]] = None,
index_name: Optional[str] = None,
content_key: str = "content",
metadata_key: str = "metadata",
vector_key: str = "content_vector",
**kwargs: Any,
) -> Redis:
"""Create a Redis vectorstore from raw documents.
This is a user-friendly interface that:
1. Embeds documents.
2. Creates a new index for the embeddings in Redis.
3. Adds the documents to the newly created Redis index.
This is intended to be a quick way to get started.
Example:
.. code-block:: python
from langchain.vectorstores import Redis
from langchain.embeddings import OpenAIEmbeddings
embeddings = OpenAIEmbeddings()
redisearch = RediSearch.from_texts(
texts,
embeddings,
redis_url="redis://username:password@localhost:6379"
)
"""
instance, _ = cls.from_texts_return_keys(
cls=cls,
texts=texts,
embedding=embedding,
metadatas=metadatas,
index_name=index_name,
content_key=content_key,
metadata_key=metadata_key,
vector_key=vector_key,
kwargs=kwargs,
)
return instance

@staticmethod
Expand Down

0 comments on commit 9ba3a79

Please sign in to comment.