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. <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>
pull/18934/head
Dt22 6 months ago committed by GitHub
parent 074ad5095f
commit 6dbf1a2de0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -45,6 +45,7 @@ from langchain_community.vectorstores.redis.constants import (
from langchain_community.vectorstores.utils import maximal_marginal_relevance from langchain_community.vectorstores.utils import maximal_marginal_relevance
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
ListOfDict = List[Dict[str, str]]
if TYPE_CHECKING: if TYPE_CHECKING:
from redis.client import Redis as RedisType from redis.client import Redis as RedisType
@ -249,7 +250,7 @@ class Redis(VectorStore):
redis_url: str, redis_url: str,
index_name: str, index_name: str,
embedding: Embeddings, 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, vector_schema: Optional[Dict[str, Union[str, int]]] = None,
relevance_score_fn: Optional[Callable[[float], float]] = None, relevance_score_fn: Optional[Callable[[float], float]] = None,
key_prefix: Optional[str] = None, key_prefix: Optional[str] = None,
@ -293,7 +294,7 @@ class Redis(VectorStore):
embedding: Embeddings, embedding: Embeddings,
metadatas: Optional[List[dict]] = None, metadatas: Optional[List[dict]] = None,
index_name: Optional[str] = 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, vector_schema: Optional[Dict[str, Union[str, int]]] = None,
**kwargs: Any, **kwargs: Any,
) -> Tuple[Redis, List[str]]: ) -> Tuple[Redis, List[str]]:
@ -335,7 +336,8 @@ class Redis(VectorStore):
dicts to add to the vectorstore. Defaults to None. dicts to add to the vectorstore. Defaults to None.
index_name (Optional[str], optional): Optional name of the index to index_name (Optional[str], optional): Optional name of the index to
create or add to. Defaults to None. 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 Optional fields to index within the metadata. Overrides generated
schema. Defaults to None. schema. Defaults to None.
vector_schema (Optional[Dict[str, Union[str, int]]], optional): Optional vector_schema (Optional[Dict[str, Union[str, int]]], optional): Optional
@ -428,7 +430,7 @@ class Redis(VectorStore):
embedding: Embeddings, embedding: Embeddings,
metadatas: Optional[List[dict]] = None, metadatas: Optional[List[dict]] = None,
index_name: Optional[str] = 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, vector_schema: Optional[Dict[str, Union[str, int]]] = None,
**kwargs: Any, **kwargs: Any,
) -> Redis: ) -> Redis:
@ -471,7 +473,8 @@ class Redis(VectorStore):
to add to the vectorstore. Defaults to None. to add to the vectorstore. Defaults to None.
index_name (Optional[str], optional): Optional name of the index to create index_name (Optional[str], optional): Optional name of the index to create
or add to. Defaults to None. 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 Optional fields to index within the metadata. Overrides generated
schema. Defaults to None. schema. Defaults to None.
vector_schema (Optional[Dict[str, Union[str, int]]], optional): Optional vector_schema (Optional[Dict[str, Union[str, int]]], optional): Optional
@ -501,7 +504,7 @@ class Redis(VectorStore):
cls, cls,
embedding: Embeddings, embedding: Embeddings,
index_name: str, 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, key_prefix: Optional[str] = None,
**kwargs: Any, **kwargs: Any,
) -> Redis: ) -> Redis:
@ -528,8 +531,9 @@ class Redis(VectorStore):
embedding (Embeddings): Embedding model class (i.e. OpenAIEmbeddings) embedding (Embeddings): Embedding model class (i.e. OpenAIEmbeddings)
for embedding queries. for embedding queries.
index_name (str): Name of the index to connect to. index_name (str): Name of the index to connect to.
schema (Union[Dict[str, str], str, os.PathLike]): Schema of the index schema (Union[Dict[str, str], str, os.PathLike, Dict[str, ListOfDict]]):
and the vector schema. Can be a dict, or path to yaml file. 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 key_prefix (Optional[str]): Prefix to use for all keys in Redis associated
with this index. with this index.
**kwargs (Any): Additional keyword arguments to pass to the Redis client. **kwargs (Any): Additional keyword arguments to pass to the Redis client.
@ -1170,7 +1174,7 @@ class Redis(VectorStore):
def _get_schema_with_defaults( def _get_schema_with_defaults(
self, 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, vector_schema: Optional[Dict[str, Union[str, int]]] = None,
) -> "RedisModel": ) -> "RedisModel":
# should only be called after init of Redis (so Import handled) # should only be called after init of Redis (so Import handled)

Loading…
Cancel
Save