From 6dbf1a2de05d16db9052f8c08220998cfeed2087 Mon Sep 17 00:00:00 2001 From: Dt22 <82443036+Distant22@users.noreply.github.com> Date: Sat, 30 Mar 2024 02:55:54 +0800 Subject: [PATCH] community[patch]: fix redis input type for index_schema field (#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.
**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 Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: Bagatur --- .../vectorstores/redis/base.py | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/libs/community/langchain_community/vectorstores/redis/base.py b/libs/community/langchain_community/vectorstores/redis/base.py index 1c7bc06f64..39d0af280d 100644 --- a/libs/community/langchain_community/vectorstores/redis/base.py +++ b/libs/community/langchain_community/vectorstores/redis/base.py @@ -45,6 +45,7 @@ from langchain_community.vectorstores.redis.constants import ( 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 @@ -249,7 +250,7 @@ class Redis(VectorStore): 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, @@ -293,7 +294,7 @@ class Redis(VectorStore): 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]]: @@ -335,7 +336,8 @@ class Redis(VectorStore): 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 @@ -428,7 +430,7 @@ class Redis(VectorStore): 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: @@ -471,7 +473,8 @@ class Redis(VectorStore): 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 @@ -501,7 +504,7 @@ class Redis(VectorStore): 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: @@ -528,8 +531,9 @@ class Redis(VectorStore): 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. @@ -1170,7 +1174,7 @@ class Redis(VectorStore): 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)