Source code for langchain.utilities.redis
from __future__ import annotations
import logging
from typing import (
    TYPE_CHECKING,
    Any,
)
from urllib.parse import urlparse
if TYPE_CHECKING:
    from redis.client import Redis as RedisType
logger = logging.getLogger(__name__)
[docs]def get_client(redis_url: str, **kwargs: Any) -> RedisType:
    """Get a redis client from the connection url given. This helper accepts
    urls for Redis server (TCP with/without TLS or UnixSocket) as well as
    Redis Sentinel connections.
    Redis Cluster is not supported.
    Before creating a connection the existence of the database driver is checked
    an and ValueError raised otherwise
    To use, you should have the ``redis`` python package installed.
    Example:
        .. code-block:: python
            from langchain.utilities.redis import get_client
            redis_client = get_client(
                redis_url="redis://username:password@localhost:6379"
                index_name="my-index",
                embedding_function=embeddings.embed_query,
            )
    To use a redis replication setup with multiple redis server and redis sentinels
    set "redis_url" to "redis+sentinel://" scheme. With this url format a path is
    needed holding the name of the redis service within the sentinels to get the
    correct redis server connection. The default service name is "mymaster". The
    optional second part of the path is the redis db number to connect to.
    An optional username or password is used for booth connections to the rediserver
    and the sentinel, different passwords for server and sentinel are not supported.
    And as another constraint only one sentinel instance can be given:
    Example:
        .. code-block:: python
            from langchain.utilities.redis import get_client