
    dhW                         S SK Jr  S SKrS SKrS SKJrJrJrJrJ	r	J
r
  S SKJr  S SKr\R                  " \5      r\(       a  S SKJr  \R*                  4SS jjr\R*                  4SS jjr " S S	5      rSS
 jrSS jrSS jrSS jrSS jrg)    )annotationsN)TYPE_CHECKINGAnyListOptionalPatterncast)urlparse)Redisc                h    [         R                  " U 5      R                  U5      R                  5       $ N)nparrayastypetobytes)r   dtypes     [/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/utilities/redis.py_array_to_bufferr      s$    88E?!!%(0022    c                p    [        [        [           [        R                  " XS9R                  5       5      $ )N)r   )r	   r   floatr   
frombuffertolist)bufferr   s     r   _buffer_to_arrayr      s%    UR]]6?FFHIIr   c                  @    \ rS rSr% SrSrS\S'   S
SS jjrSS jrS	r	g)TokenEscaper   z,
Escape punctuation within an input string.
z&[,.<>{}\[\]\\\"\':;!@#$%^&*()\-+=~\/ ]strDEFAULT_ESCAPED_CHARSNc                j    U(       a  Xl         g [        R                  " U R                  5      U l         g r   )escaped_chars_rerecompiler    )selfescape_chars_res     r   __init__TokenEscaper.__init__!   s"    $3!$&JJt/I/I$JD!r   c                    [        U[        5      (       d  [        S[        U5       35      eSS jnU R                  R                  X!5      $ )Nz:Value must be a string object for token escaping.Got type c                .    U R                  S5      nSU 3$ )Nr   \)group)matchvalues     r   escape_symbol*TokenEscaper.escape.<locals>.escape_symbol.   s    KKNEw<r   )r-   zre.Matchreturnr   )
isinstancer   	TypeErrortyper"   sub)r%   r.   r/   s      r   escapeTokenEscaper.escape'   sM    %%% K=* 
	  $$((>>r   )r"   r   )r&   zOptional[Pattern])r.   r   r1   r   )
__name__
__module____qualname____firstlineno____doc__r    __annotations__r'   r6   __static_attributes__ r   r   r   r      s      "K3JK?r   r   c                (   U R                  5       nU Vs0 sH  o3S   R                  S5      U_M     nnU H3  nUS   U;   d  M  [        X#S      S   5      [        US   5      :  d  M3    g   Sn[        R	                  U5        [        U5      es  snf )z1Check if the correct Redis modules are installed.s   namezutf-8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decodeintloggererror
ValueError)clientrequired_modulesinstalled_modulesmoduleerror_messages        r   check_redis_module_existrN   5   s    **,>O>OFww'/>O   #&>..3Vn-f54
4  	 #	R 
 LL
]
##s   Bc                p    SSK nU R                  S5      (       a  [        U 40 UD6nU$ U R                  S5      (       a  SUS'   SU;  a  S	US'   [        U 40 UD6nU$ UR                  " U 40 UD6n[        U5      (       a  UR                  5         [        U 40 UD6nU$ ! [         a    [        S5      ef = f)
aZ  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 both 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)	redis_urlkwargsrS   redis_clients       r   
get_clientr^   J   s    X
 ,---iB6B  
		/	0	0u&(&,F?#-iB6B 	 ~~i:6:l++ 0EfEL)  
A
 	

s   B B5c                   SSK n[        U 5      nUR                  =(       d    SUR                  =(       d    S4/nUR                  (       aA  UR                  R                  S5      nUS   =(       d    Sn[        U5      S:  a  US   US	'   OSn0 nUR                  (       a  UR                  US
'   UR                  US
'   UR                  (       a  UR                  US'   UR                  US'   U H'  nUR                  S5      (       d  US:X  d  M!  X   Xx'   M)     UR                  R                  " U4SU0UD6n	 U	R                  S5        U	R%                  U5      $ ! UR                  R                   aQ  n
SU
R                  S   ;   a7  [         R#                  S5        UR                  R                  " U40 UD6n	 Sn
A
NuU
eSn
A
ff = f)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 both systems.
r   N	localhostig  /   mymaster   dbpasswordusernamerP   client_namesentinel_kwargspingzno password is setzRedis sentinel connection configured with password but Sentinel answered NO PASSWORD NEEDED - Please check Sentinel configuration)rS   r
   hostnameportpathsplitlenrf   rg   rU   sentinelSentinelexecute_command
exceptionsAuthenticationErrorargsrF   warning
master_for)r[   r\   rS   
parsed_urlsentinel_list
path_partsservice_namesentinel_argsargsentinel_clientaes              r   rV   rV      s    )$J ))8[*//:RUSTM__**3/
!!}2
z?Q%a=F4L!M$.$7$7j!'00z$.$7$7j!'00z >>%  C=$8!'M  nn--'48>O
''/ %%l33 // 2771:-NNC $nn55mNvNOHs   <E G	8AGGG	c                ~    SS K n U R                  S5      nUS   S:H  $ ! UR                  R                   a     gf = f)Nr   clustercluster_enabledrb   F)rS   infors   
RedisError)r]   rS   cluster_infos      r   rX   rX      sI    #((3-.!33&& s    <<c                4    SSK Jn  UR                  " U 40 UD6$ )Nr   )RedisCluster)redis.clusterr   rW   )r[   r\   r   s      r   rZ   rZ      s    *  5f55r   )r   List[float]r   r   r1   bytes)r   r   r   r   r1   r   )rI   	RedisTyperJ   z
List[dict]r1   None)r[   r   r\   r   r1   r   )r]   r   r1   bool)
__future__r   loggingr#   typingr   r   r   r   r   r	   urllib.parser
   numpyr   	getLoggerr8   rF   redis.clientr   r   float32r   r   r   rN   r^   rV   rX   rZ   r?   r   r   <module>r      ss    "  	 D D ! 			8	$/ 79jj 3 24 J? ?:$*BJ:4z6r   