
    ^h9/                     b    S r SSKrSSKrSSKrSSKJr  SSKrSSKJr  SSK	J
r
   " S S\
5      rg)	z
Redis Semantic Cache implementation

Has 4 methods:
    - set_cache
    - get_cache
    - async_set_cache
    - async_get_cache
    N)Any)print_verbose   )	BaseCachec                   ^    \ rS rSr       SS jrS\4S jrS rS rS r	S	 r
S
 rS rSrg)RedisSemanticCache   Nc                    SSK Jn	  [        S5        Uc  [        S5      eXPl        Xpl        SSSS.S	S
0/SSSSSS./S.S.n
Uce  Ub  Ub  UcK  SS KnUR                  S5      nUR                  S5      nUR                  S5      nUb  Ub  Uc  [        S5      eSU-   S-   U-   S-   U-   n[        SU 35        USL aJ  U	R                  U
5      U l	        U R                  R                  US9   U R                  R                  SS9  g USL a9  S U
S!   S	'   U	R                  U
5      U l	        U R                  R                  USS"9  g g ! [         a!  n[        S[        U5       35         S nAg S nAff = f)#Nr   )SearchIndexzFredis semantic-cache initializing INDEX - litellm_semantic_cache_indexz2similarity_threshold must be provided, passed Nonelitellm_semantic_cache_indexlitellmhash)nameprefixstorage_typer   responselitellm_embeddingi   cosineflatfloat32)r   dimsdistance_metric	algorithmdatatype)textvector)indexfields
REDIS_HOST
REDIS_PORTREDIS_PASSWORDz/Redis host, port, and password must be providedz	redis://:@:z redis semantic-cache redis_url: F)	redis_url	overwrite-Got exception creating semantic cache index: T"litellm_semantic_cache_index_asyncr   )r$   	use_async)redisvl.indexr   r   	Exceptionsimilarity_thresholdembedding_modelosgetenv	from_dictr   connectcreatestr)selfhostportpasswordr$   r,   r)   r-   kwargsr   schemar.   es                ^/home/james-whalen/.local/lib/python3.13/site-packages/litellm/caching/redis_semantic_cache.py__init__RedisSemanticCache.__init__   s    	.T	
  'PQQ$8!. 7# & !*-. !4 $+3%+$-
& |t|x/?yy.yy.99%56<4<83C#$UVV#h.4t;cADHI8DE$..v6DJJJ3X

!!E!2 $&JF7OF#$..v6DJJJdC   X McRSfXVWWXs   -E 
E1E,,E1cached_responsec                     Uc  U$ [        U[        5      (       a  UR                  S5      n [        R                  " U5      nU$ ! [
         a    [        R                  " U5      n U$ f = f)zK
Common 'get_cache_logic' across sync + async redis client implementations
zutf-8)
isinstancebytesdecodejsonloadsr+   astliteral_eval)r4   r>   s     r;   _get_cache_logic#RedisSemanticCache._get_cache_logicY   sw     """ ou---44W=O	@"jjO
   	@!..?O	@s   A  A)(A)c                    SS K n[        SU 35        US   nSR                  S U 5       5      n[        R                  " U R
                  USSS.S9nUS	   S   S
   nUR                  XR                  S9R                  5       n	[        U5      n[        U[        5      (       d   eX&U	S./n
U R                  R                  U
5        g )Nr   z(redis semantic-cache set_cache, kwargs: messages c              3   *   #    U  H	  oS    v   M     g7fcontentN .0messages     r;   	<genexpr>/RedisSemanticCache.set_cache.<locals>.<genexpr>s        D8+8   Tzno-storezno-cachemodelinputcachedata	embeddingdtyper   promptr   )numpyr   joinr   r]   r-   arrayr   tobytesr3   r@   r   load)r4   keyvaluer8   nprJ   ra   embedding_responser]   embedding_bytesnew_datas              r;   	set_cacheRedisSemanticCache.set_cachel   s    @IJ *%D8DD %..&&#6
 'v.q1+>	 ((9JJ(?GGIE
%%%%% W

 	

!    c           
      V   [        SU 35        SSKJn  US   nSR                  S U 5       5      n[        R
                  " U R                  USSS.S	9nUS
   S   S   nU" US/ SQSS9nU R                  R                  U5      n	U	c  g [        U	[        5      (       a  [        U	5      S:X  a  g U	S   S   n
[        U
5      n
SU
-
  nU	S   S   n[        SU R                   SU SU SU 35        XR                  :  a+  U	S   S   n[        SU SU SU 35        U R                  US9$ g )Nz-sync redis semantic-cache get_cache, kwargs: r   VectorQueryrJ   rK   c              3   *   #    U  H	  oS    v   M     g7frM   rO   rP   s     r;   rS   /RedisSemanticCache.get_cache.<locals>.<genexpr>   rU   rV   TrW   rX   r\   r]   r   r   ra   vector_distancer   )r   vector_field_namereturn_fieldsnum_resultsrv   ra   &semantic cache: similarity threshold: , similarity: 
, prompt: , closest_cached_prompt: r   got a cache hit, similarity: , Current prompt: , cached_prompt: r>   )r   redisvl.queryrr   rc   r   r]   r-   r   queryr@   listlenfloatr,   rG   )r4   rg   r8   rr   rJ   ra   rj   r]   r   resultsrv   
similaritycached_promptcached_values                 r;   	get_cacheRedisSemanticCache.get_cache   s   EfXNO- *%D8DD %..&&#6
 'v.q1+>	1C	
 **""5)?gt$$7|q !!*%670(

8, 	4T5N5N4O~^h]iistzs{  |U  Vc  Ud  e	
 111"1:j1L/
|;MfXUfgtfuv (((FF ro   c                   #    SS K nSSKJnJn   U R                  R                  SS9I S h  vN   [        SU 35        US   nSR                  S	 U 5       5      n	Ub  U V
s/ s H  oS
   PM	     sn
O/ nUb~  U R                  U;   an  UR                  S0 5      R                  SS5      nUR                  U R                  U	SSS.USUR                  S0 5      R                  SS 5      S.S9I S h  vN nO+[        R                  " U R                  U	SSS.S9I S h  vN nUS   S   S   nUR                  XR                  S9R!                  5       n[        U5      n[#        U[        5      (       d   eX)US./nU R                  R%                  U5      I S h  vN   g  GNt! [         a"  n[        S[        U5       35         S nAGNS nAff = fs  sn
f  N N N@7f)Nr   llm_model_list
llm_routerFr%   r'   z.async redis semantic-cache set_cache, kwargs: rJ   rK   c              3   *   #    U  H	  oS    v   M     g7frM   rO   rP   s     r;   rS   5RedisSemanticCache.async_set_cache.<locals>.<genexpr>   rU   rV   
model_namemetadatauser_api_keyTrW   trace_idr   zsemantic-cache-embeddingr   rY   rZ   r[   r   rX   r\   r]   r^   r`   )rb   litellm.proxy.proxy_serverr   r   r   acreater+   r   r3   rc   r-   get
aembeddingr   rd   r   re   r@   aload)r4   rg   rh   r8   ri   r   r   r:   rJ   ra   mrouter_model_namesr   rj   r]   rk   rl   s                    r;   async_set_cache"RedisSemanticCache.async_set_cache   s    I	T**$$u$555 	FvhOP *%D8DD ) '55n|_n5 	
 !d&:&:>P&P!::j"599."ML'1'<'<**#'T:$004 &

:r : > >z4 P	 (= 	( 	" (/'9'9**#'T:( " 'v.q1+>	 ((9JJ(?GGIE
%%%%% W

 jjx(((a 6 	TI#a&RSS	T 6	""& 	)s{   GF# F F# 2G#G1B G1G2+GGA<GGG F# #
G-G
G
G	GGGc                 \  #    [        SU 35        SSKJn  SSKJnJn  US   nSR                  S U 5       5      nUb  U Vs/ s H  oS   PM	     snO/ n	Ub~  U R                  U	;   an  UR                  S	0 5      R                  S
S5      n
UR                  U R                  USSS.U
SUR                  S	0 5      R                  SS 5      S.S9I S h  vN nO+[        R                  " U R                  USSS.S9I S h  vN nUS   S   S   nU" US/ SQS9nU R                  R                  U5      I S h  vN nUc  SUR                  S	0 5      S'   g [        U[        5      (       a%  [!        U5      S:X  a  SUR                  S	0 5      S'   g US   S   n[#        U5      nSU-
  nUS   S   n[        SU R$                   SU SU SU 35        UUR                  S	0 5      S'   UU R$                  :  a+  US   S   n[        S U S!U S"U 35        U R'                  US#9$ g s  snf  GNS GN* N7f)$Nz.async redis semantic-cache get_cache, kwargs: r   rq   r   rJ   rK   c              3   *   #    U  H	  oS    v   M     g7frM   rO   rP   s     r;   rS   5RedisSemanticCache.async_get_cache.<locals>.<genexpr>  rU   rV   r   r   r   TrW   r   r   r   rX   r\   r]   r   ru   )r   rw   rx   g        zsemantic-similarityrv   r   ra   rz   r{   r|   r}   r   r~   r   r   r   )r   r   rr   r   r   r   rc   r-   r   r   r   r   aquery
setdefaultr@   r   r   r   r,   rG   )r4   rg   r8   rr   r   r   rJ   ra   r   r   r   rj   r]   r   r   rv   r   r   r   s                      r;   async_get_cache"RedisSemanticCache.async_get_cache   s    FvhOP-I *%D8DD ) '55n|_n5 	
 !d&:&:>P&P!::j"599."ML'1'<'<**#'T:$004 &

:r : > >z4 P	 (= 	( 	" (/'9'9**#'T:( " 'v.q1+>	1C

 

))%00?GJFj"-.CDgt$$7|q KN!!*b12GH!!*%670(

8, 	4T5N5N4O~^h]iistzs{  |U  Vc  Ud  e	

 DN*b)*?@111"1:j1L/
|;MfXUfgtfuv (((FF  6	"" 1sD   AH,HB H,H$+H,=H'>7H,5H*6C/H,'H,*H,c                 R   #    U R                   R                  5       I S h  vN $  N7f)N)r   ainfo)r4   s    r;   _index_infoRedisSemanticCache._index_infoJ  s     ZZ%%''''s   '%'c                    #    / nU H,  nUR                  U R                  " US   US   40 UD65        M.     [        R                  " U6 I S h  vN   g  N7f)Nr   r   )appendr   asynciogather)r4   
cache_listr8   tasksvals        r;   async_set_cache_pipeline+RedisSemanticCache.async_set_cache_pipelineM  sL     CLL--c!fc!fGGH nne$$$s   AAAA)r-   r   r,   )NNNNNFztext-embedding-ada-002)__name__
__module____qualname____firstlineno__r<   r   rG   rm   r   r   r   r   r   __static_attributes__rO   ro   r;   r   r      sO     !0?DD &B5n6pL\(%ro   r   )__doc__rE   r   rC   typingr   r   litellm._loggingr   
base_cacher   r   rO   ro   r;   <module>r      s/         * !{% {%ro   