o
    ȳgY                      @  s   d dl mZ d dlZd dlZd dlmZmZmZmZm	Z	m
Z
 d dlmZ d dlZeeZer5d dlmZ ejfd'ddZejfd(ddZG dd dZd)ddZd*ddZd*dd Zd+d#d$Zd*d%d&ZdS ),    )annotationsN)TYPE_CHECKINGAnyListOptionalPatterncast)urlparse)RedisarrayList[float]dtyper   returnbytesc                 C  s   t | | S N)npr   astypetobytes)r   r    r   _/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/langchain_community/utilities/redis.py_array_to_buffer   s   r   bufferc                 C  s   t tt tj| |d S )N)r   )r   r   floatr   
frombuffertolist)r   r   r   r   r   _buffer_to_array   s   r   c                   @  s4   e Zd ZU dZdZded< dddd	ZdddZdS )TokenEscaperz4
    Escape punctuation within an input string.
    z&[,.<>{}\[\]\\\"\':;!@#$%^&*()\-+=~\/ ]strDEFAULT_ESCAPED_CHARSNescape_chars_reOptional[Pattern]c                 C  s    |r|| _ d S t| j| _ d S r   )escaped_chars_rerecompiler   )selfr   r   r   r   __init__!   s   
zTokenEscaper.__init__valuer   c                 C  s4   t |tstdt| ddd}| j||S )	Nz:Value must be a string object for token escaping.Got type matchre.Matchr   r   c                 S  s   |  d}d| S )Nr   \)group)r'   r&   r   r   r   escape_symbol.   s   

z*TokenEscaper.escape.<locals>.escape_symbol)r'   r(   r   r   )
isinstancer   	TypeErrortyper!   sub)r$   r&   r+   r   r   r   escape'   s   

zTokenEscaper.escaper   )r   r    )r&   r   r   r   )__name__
__module____qualname____doc__r   __annotations__r%   r0   r   r   r   r   r      s
   
 r   client	RedisTyperequired_modules
List[dict]Nonec                 C  sh   |   }dd |D }|D ]}|d |v r(t||d  d t|d kr( dS qd}t| t|)z1Check if the correct Redis modules are installed.c                 S  s   i | ]
}|d   d|qS )s   namezutf-8)decode).0moduler   r   r   
<dictcomp>8   s    z,check_redis_module_exist.<locals>.<dictcomp>names   ververNzRedis cannot be used as a vector database without RediSearch >=2.4Please head to https://redis.io/docs/stack/search/quick_start/to know more about installing the RediSearch module within Redis Stack.)module_listintloggererror
ValueError)r6   r8   installed_modulesr=   error_messager   r   r   check_redis_module_exist5   s    

rH   	redis_urlr   kwargsc                 K  s   zddl }W n ty   tdw | dr!t| fi |}|S | dr<d|d< d|vr2d	|d< t| fi |}|S |j| fi |}t|rU|  t| fi |}|S )
a  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_community.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_community.utilities.redis import get_client
            redis_client = get_client(
                redis_url="redis+sentinel://username:password@sentinelhost:26379/mymaster/0"
                index_name="my-index",
                embedding_function=embeddings.embed_query,
            )
    r   NzYCould not import redis python package. Please install it with `pip install redis>=4.1.0`.zredis+sentinelzrediss+sentinelTsslssl_cert_reqsnone)redisImportError
startswith_redis_sentinel_clientfrom_url_check_for_clusterclose_redis_cluster_client)rI   rJ   rN   redis_clientr   r   r   
get_clientJ   s*   ,

rW   c              
   K  sN  ddl }t| }|jpd|jpdfg}|jr/|jd}|d p!d}t|dkr.|d |d	< nd}i }|jr@|j|d
< |j|d
< |jrM|j|d< |j|d< |D ]}|	dsZ|dkr`|| ||< qO|j
j|fd|i|}	z|	d W n- |jjy }
 zd|
jd v rtd |j
j|fi |}	n|
W Y d}
~
nd}
~
ww |	|S )a  helper method to parse an (un-official) redis+sentinel url
    and create a Sentinel connection to fetch the final redis client
    connection to a replica-master for read-write operations.

    If username and/or password for authentication is given the
    same credentials are used for the Redis Sentinel as well as Redis Server.
    With this implementation using a redis url only it is not possible
    to use different data for authentication on booth systems.
    r   N	localhostig  /   mymaster   dbpasswordusernamerK   client_namesentinel_kwargspingzno password is setzRedis sentinel connection configured with password but Sentinel answered NO PASSWORD NEEDED - Please check Sentinel configuration)rN   r	   hostnameportpathsplitlenr^   r_   rP   sentinelSentinelexecute_command
exceptionsAuthenticationErrorargsrC   warning
master_for)rI   rJ   rN   
parsed_urlsentinel_list
path_partsservice_namesentinel_argsargsentinel_clientaer   r   r   rQ      sR   






rQ   rV   boolc                 C  s:   dd l }z| d}|d dkW S  |jjy   Y dS w )Nr   clustercluster_enabledrZ   F)rN   infork   
RedisError)rV   rN   cluster_infor   r   r   rS      s   
rS   c                 K  s   ddl m} |j| fi |S )Nr   )RedisCluster)redis.clusterr~   rR   )rI   rJ   r~   r   r   r   rU      s   rU   )r   r   r   r   r   r   )r   r   r   r   r   r   )r6   r7   r8   r9   r   r:   )rI   r   rJ   r   r   r7   )rV   r7   r   rx   )
__future__r   loggingr"   typingr   r   r   r   r   r   urllib.parser	   numpyr   	getLoggerr1   rC   redis.clientr
   r7   float32r   r   r   rH   rW   rQ   rS   rU   r   r   r   r   <module>   s"     



E
=
