Skip to content

Commit

Permalink
community[patch]: fix redis input type for index_schema field (langch…
Browse files Browse the repository at this point in the history
…ain-ai#16874)

### Subject: Fix Type Misdeclaration for index_schema in redis/base.py

I noticed a type misdeclaration for the index_schema column in the
redis/base.py file.

When following the instructions outlined in [Redis Custom Metadata
Indexing](https://python.langchain.com/docs/integrations/vectorstores/redis)
to create our own index_schema, it leads to a Pylance type error. <br/>
**The error message indicates that Dict[str, list[Dict[str, str]]] is
incompatible with the type Optional[Union[Dict[str, str], str,
os.PathLike]].**

```
index_schema = {
    "tag": [{"name": "credit_score"}],
    "text": [{"name": "user"}, {"name": "job"}],
    "numeric": [{"name": "age"}],
}

rds, keys = Redis.from_texts_return_keys(
    texts,
    embeddings,
    metadatas=metadata,
    redis_url="redis://localhost:6379",
    index_name="users_modified",
    index_schema=index_schema,  
)
```
Therefore, I have created this pull request to rectify the type
declaration problem.

---------

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com>
Co-authored-by: Bagatur <baskaryan@gmail.com>
  • Loading branch information
4 people authored Mar 29, 2024
1 parent 074ad50 commit 6dbf1a2
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions libs/community/langchain_community/vectorstores/redis/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from langchain_community.vectorstores.utils import maximal_marginal_relevance

logger = logging.getLogger(__name__)
ListOfDict = List[Dict[str, str]]

if TYPE_CHECKING:
from redis.client import Redis as RedisType
Expand Down Expand Up @@ -249,7 +250,7 @@ def __init__(
redis_url: str,
index_name: str,
embedding: Embeddings,
index_schema: Optional[Union[Dict[str, str], str, os.PathLike]] = None,
index_schema: Optional[Union[Dict[str, ListOfDict], str, os.PathLike]] = None,
vector_schema: Optional[Dict[str, Union[str, int]]] = None,
relevance_score_fn: Optional[Callable[[float], float]] = None,
key_prefix: Optional[str] = None,
Expand Down Expand Up @@ -293,7 +294,7 @@ def from_texts_return_keys(
embedding: Embeddings,
metadatas: Optional[List[dict]] = None,
index_name: Optional[str] = None,
index_schema: Optional[Union[Dict[str, str], str, os.PathLike]] = None,
index_schema: Optional[Union[Dict[str, ListOfDict], str, os.PathLike]] = None,
vector_schema: Optional[Dict[str, Union[str, int]]] = None,
**kwargs: Any,
) -> Tuple[Redis, List[str]]:
Expand Down Expand Up @@ -335,7 +336,8 @@ def from_texts_return_keys(
dicts to add to the vectorstore. Defaults to None.
index_name (Optional[str], optional): Optional name of the index to
create or add to. Defaults to None.
index_schema (Optional[Union[Dict[str, str], str, os.PathLike]], optional):
index_schema (Optional[Union[Dict[str, ListOfDict], str, os.PathLike]],
optional):
Optional fields to index within the metadata. Overrides generated
schema. Defaults to None.
vector_schema (Optional[Dict[str, Union[str, int]]], optional): Optional
Expand Down Expand Up @@ -428,7 +430,7 @@ def from_texts(
embedding: Embeddings,
metadatas: Optional[List[dict]] = None,
index_name: Optional[str] = None,
index_schema: Optional[Union[Dict[str, str], str, os.PathLike]] = None,
index_schema: Optional[Union[Dict[str, ListOfDict], str, os.PathLike]] = None,
vector_schema: Optional[Dict[str, Union[str, int]]] = None,
**kwargs: Any,
) -> Redis:
Expand Down Expand Up @@ -471,7 +473,8 @@ def from_texts(
to add to the vectorstore. Defaults to None.
index_name (Optional[str], optional): Optional name of the index to create
or add to. Defaults to None.
index_schema (Optional[Union[Dict[str, str], str, os.PathLike]], optional):
index_schema (Optional[Union[Dict[str, ListOfDict], str, os.PathLike]],
optional):
Optional fields to index within the metadata. Overrides generated
schema. Defaults to None.
vector_schema (Optional[Dict[str, Union[str, int]]], optional): Optional
Expand Down Expand Up @@ -501,7 +504,7 @@ def from_existing_index(
cls,
embedding: Embeddings,
index_name: str,
schema: Union[Dict[str, str], str, os.PathLike],
schema: Union[Dict[str, ListOfDict], str, os.PathLike, Dict[str, ListOfDict]],
key_prefix: Optional[str] = None,
**kwargs: Any,
) -> Redis:
Expand All @@ -528,8 +531,9 @@ def from_existing_index(
embedding (Embeddings): Embedding model class (i.e. OpenAIEmbeddings)
for embedding queries.
index_name (str): Name of the index to connect to.
schema (Union[Dict[str, str], str, os.PathLike]): Schema of the index
and the vector schema. Can be a dict, or path to yaml file.
schema (Union[Dict[str, str], str, os.PathLike, Dict[str, ListOfDict]]):
Schema of the index and the vector schema. Can be a dict, or path to
yaml file.
key_prefix (Optional[str]): Prefix to use for all keys in Redis associated
with this index.
**kwargs (Any): Additional keyword arguments to pass to the Redis client.
Expand Down Expand Up @@ -1170,7 +1174,7 @@ def _prepare_vector_query(

def _get_schema_with_defaults(
self,
index_schema: Optional[Union[Dict[str, str], str, os.PathLike]] = None,
index_schema: Optional[Union[Dict[str, ListOfDict], str, os.PathLike]] = None,
vector_schema: Optional[Dict[str, Union[str, int]]] = None,
) -> "RedisModel":
# should only be called after init of Redis (so Import handled)
Expand Down

0 comments on commit 6dbf1a2

Please sign in to comment.