
    g                     t   d dl Z d dlmZmZmZmZ d dlZd dlmZ d dl	m
Z
 d dlmZ  eddg      Zej                  d	 ee      g
      d        Zej!                  ddg ee      g      defd       Zej                  d ee      g
      d        Zej!                  ddg ee      g      d        Zy)    N)	APIRouterDependsHTTPExceptionRequest)verbose_proxy_logger)
RedisCache)user_api_key_authz/cachecaching)prefixtagsz/ping)dependenciesc            
        K   i } i }	 t         j                  t        dd      t        t         j                        j	                         D ]-  \  }}	 |dk(  rt        t        j                  |            | |<   / t        t         j                  j                        j	                         D ]  \  }}	 t        |      ||<    t         j                  j                  dk(  rt         j                  j                          d{   }t        j                  dt        |      z          t         j                  j                  d	d
dddg       d{    t        j                  d       dt         j                  j                  dd| |dS dt         j                  j                  | dS # t        $ r	 d| |<   Y ow xY w# t        $ r	 d||<   Y #w xY w7 7 # t        $ r$}t        ddt        |       d|  d|       d}~ww xY ww)z6
    Endpoint for checking if cache can be pinged
    N  ,Cache not initialized. litellm.cache is Nonestatus_codedetailcachez<unable to copy or convert>redisz/cache/ping: ping_response: test_keyz
test-modeluserztest from litellm)rolecontent)resultmodelmessagesz"/cache/ping: done with set_cache()healthyTsuccess)status
cache_typeping_responseset_cache_responselitellm_cache_paramsredis_cache_params)r   r    r#   Service Unhealthy (z).Cache parameters: z.specific_cache_params: )litellmr   r   varsitemsstrcopydeepcopy	Exceptiontypepingr   debugasync_add_cache)r#   specific_cache_paramskvr!   es         Q/var/www/openai/venv/lib/python3.12/site-packages/litellm/proxy/caching_routes.py
cache_pingr6      s     3
== (V  '--/DAqH<*-dmmA.>*?$Q'	 0 ,,-335DAqI+.q6%a( 6
 ==(")--"4"4"66M &&.]1CC
 --//!"#)6IJK 0   
 !&&'KL#%mm00!%&/(<&;  $%mm00(< =  H*G$Q'H
  I+H%a(I 7(  
(Q0DEYDZZr  tI  sJ  K
 	

s   HA
G F*G !F*:9G 4F?A G GAG G9G HG )H*F<8G ;F<<G ?GG GG G 	H!H  HHz/delete)r   r   requestc                   K   	 t         j                  t        dd      | j                          d{   }|j	                  dd      }t         j                  j
                  dk(  r,t         j                  j                  |       d{    dd	iS t        d
dt         j                  j
                   d      7 7 3# t        $ r}t        d
dt        |       d      d}~ww xY ww)a  
    Endpoint for deleting a key from the cache. All responses from litellm proxy have `x-litellm-cache-key` in the headers

    Parameters:
    - **keys**: *Optional[List[str]]* - A list of keys to delete from the cache. Example {"keys": ["key1", "key2"]}

    ```shell
    curl -X POST "http://0.0.0.0:4000/cache/delete"     -H "Authorization: Bearer sk-1234"     -d '{"keys": ["key1", "key2"]}'
    ```

    Nr   r   r   keysr   )r9   r   r     Cache type z; does not support deleting a key. only `redis` is supportedzCache Delete Failed())	r&   r   r   jsongetr-   delete_cache_keysr,   r)   )r7   request_datar9   r4   s       r5   cache_deleterA   P   s     &
== (V  %\\^+-==(--11t1<<<)   $W]]%7%7$88st  , =  
)#a&3
 	

sK   C,0C B=AC B?C C,*C ?C 	C)
C$$C))C,z/redis/infoc                  :  K   	 t         j                  t        dd      t         j                  j                  dk(  rt	        t         j                  j                  t
              rat         j                  j                  j                         } t         j                  j                  j                         }t        |       }|| |dS t        ddt         j                  j                   d	      # t        $ r}t        dd
t        |       d      d}~ww xY ww)z*
    Endpoint for getting /redis/info
    Nr   r   r   r   )num_clientsclientsinfor:   r;    does not support flushingr%   r<   )r&   r   r   r-   
isinstancer   client_listrE   lenr,   r)   )rH   
redis_inforC   r4   s       r5   cache_redis_inforK   }   s     
== (V  ==(ZMM.
 "----99;K ,,113Jk*K*&"   $W]]%7%7$88RS   
(Q2
 	

s/   DCC0 D)C0 0	D9DDDz	/flushallc                    K   	 t         j                  t        dd      t         j                  j                  dk(  rTt	        t         j                  j                  t
              r,t         j                  j                  j                          ddiS t        dd	t         j                  j                   d
      # t        $ r} t        ddt        |        d      d} ~ ww xY ww)a  
    A function to flush all items from the cache. (All items will be deleted from the cache with this)
    Raises HTTPException if the cache is not initialized or if the cache type does not support flushing.
    Returns a dictionary with the status of the operation.

    Usage:
    ```
    curl -X POST http://0.0.0.0:4000/cache/flushall -H "Authorization: Bearer sk-1234"
    ```
    Nr   r   r   r   r   r   r:   r;   rF   r%   r<   )	r&   r   r   r-   rG   r   flushallr,   r)   )r4   s    r5   cache_flushallrN      s      
== (V  ==(ZMM.
 MM((*)   $W]]%7%7$88RS   
(Q2
 	

s/   C&BB; C&)B; ;	C#CC##C&)r*   fastapir   r   r   r   r&   litellm._loggingr   litellm.caching.cachingr   $litellm.proxy.auth.user_api_key_authr	   routerr>   r6   postrA   rK   rN        r5   <module>rW      s    > >  1 . B	

 +,-  9
	9
x 
+,-  
%
 %

%
P +,-  
	
@ 
+,-  
 

 
rV   