
    CjϮ                     6   S SK r S SKrS SKrS SKrS SKrS SKJ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S SKJr  S SKJr  S SKJr  S SK7  S S	KJrJr  S
SKJr  S
SKJr  S
SKJ r   S
SK!J"r"  S
SK#J$r$  S
SK%J&r&  S
SK'J(r(  S
SK)J*r*  S
SK+J,r,  S
SK-J.r.  S
SK/J0r0  S r1 " S S\2\5      r3 " S S5      r4\5Rl                  SSS/ SQ4S\\5   S\\2   S\\2   S\\2   S \\
\7      4
S! jjr8\5Rl                  SSS/ SQ4S\\5   S\\2   S\\2   S\\2   S \\
\7      4
S" jjr9S# r:g)$    N)Enum)AnyDictListOptionalTupleUnion)	BaseModel)verbose_logger)CACHED_STREAMING_CHUNK_DELAY)ModelParamHelper)*)EmbeddingResponseall_litellm_params   )AzureBlobCache)	BaseCache)	DiskCache)	DualCache)GCSCache)InMemoryCache)QdrantSemanticCache)
RedisCache)RedisClusterCache)RedisSemanticCache)S3Cachec                      [         R                  " U 5        [        R                  (       a  [	        U 5        g g ! [
         a     g f = fN)r   debuglitellmset_verboseprint	Exception)print_statements    i/home/rurouni/.local/share/pipx/venvs/strix-agent/lib/python3.13/site-packages/litellm/caching/caching.pyprint_verboser&   (   s=    _-/"  s   6: 
AAc                       \ rS rSrSrSrSrg)	CacheMode1   
default_ondefault_off N)__name__
__module____qualname____firstlineno__r*   r+   __static_attributes__r,       r%   r(   r(   1   s    JKr2   r(   c            V       &   \ rS rSr% \R
                  \R                  SSSSSSSS/ SQSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS4*S\\   S\\   S\\	   S	\\	   S
\\	   S\\	   S\\
   S\\
   S\\
   S\\
   S\\\      S\\	   S\\	   S\\	   S\\	   S\\	   S\\   S\\\\	4      S\\	   S\\	   S\\	   S\\	   S\\   S\\	   S\\	   S\\	   S \\	   S!\	S"\\	   S#\	S$\	S-  S%\\   S&\\   S'\\	   S(\\	   S)\\	   S*\\	   S+\\	   S,\	S-\\   S.\\	   S/\\	   4TS0 jjr\" 1 S1k5      r\\S2'   S3r\\	S44   \S5'   S6\4S7 jrS8\S6\	4S9 jrS6\	4S: jrS;\	S8\S6\\	   4S< jrS8\S6\	4S= jrS>\S?\\	   S6\\	   4S@ jrS8\S6\	4SA jrS6\\	   4SB jrSC\	S6S4SD jr \!SE\	S6\	4SF j5       r"SG\	S6\	4SH jr#SI r$SJ\\   SK\\
   4SL jr%\!S8\&\	\4   S6\&\	\4   4SM j5       r'\!SN\&\	\4   SO\&\	\4   S6S4SP j5       r(SjSQ\\)   4SR jjr* SjSQ\\)   4SS jjr+ST r,SU r- SjSQ\\)   4SV jjr.  SkSW\SX\\	   SY\\   SZ\\   S6\/4
S[ jjr0S\\1S]\S6\\   4S^ jr2S\\1S]\S6\\   4S_ jr3 SlS\\1S`\	S8\S]\S6\4\	\\4   4
Sa jjr5 SjSQ\\)   4Sb jjr6Sc r7Sd r8Se r9Sf r:Sg r;S6\4Sh jr<Sir=g)mCache7   N
completionacompletion	embedding
aembeddingatranscriptiontranscriptionatext_completiontext_completionarerankrerank	responses
aresponsesTztext-embedding-ada-002typemodehostportpassword	namespacettldefault_in_memory_ttldefault_in_redis_ttlsimilarity_thresholdsupported_call_typesazure_account_urlazure_blob_containers3_bucket_names3_region_names3_api_version
s3_use_ssl	s3_verifys3_endpoint_urls3_aws_access_key_ids3_aws_secret_access_keys3_aws_session_token	s3_configs3_pathgcs_bucket_namegcs_path_service_accountgcs_path$redis_semantic_cache_embedding_modelredis_semantic_cache_index_name%valkey_semantic_cache_embedding_model valkey_semantic_cache_index_nameredis_flush_sizeredis_startup_nodesdisk_cache_dirqdrant_api_baseqdrant_api_keyqdrant_collection_nameqdrant_quantization_config%qdrant_semantic_cache_embedding_model!qdrant_semantic_cache_vector_sizegcp_service_accountgcp_ssl_ca_certsc+                 4   U[         R                  :X  a  U!(       dD  [        R                  " S5      n,U,b+  [	        U,[
        5      (       a  [        R                  " U,5      n!U!(       a,  UUUU U!S.U+En-U)b  U)U-S'   U*b  U*U-S'   [        S0 U-D6U l	        GO][        SUUUU S.U+D6U l	        GOFU[         R                  :X  a  [        SUUUU
UUS.U+D6U l	        GOU[         R                  :X  a  SS	KJn.  U." SUUUU
UUU!S
.U+D6U l	        OU[         R                   :X  a  [#        U#U$U%U
U&U'U(S9U l	        OU[         R$                  :X  a  ['        5       U l	        OU[         R(                  :X  a  [+        SUUUUUUUUUUUS.U+D6U l	        OkU[         R,                  :X  a  [/        UUUS9U l	        OFU[         R0                  :X  a  [3        UUS9U l	        O"U[         R4                  :X  a  [7        U"S9U l	        S[        R8                  ;  a  [        R8                  R;                  S5        S[        R<                  ;  a  [        R>                  RA                  S5        S[        RB                  ;  a  [        R>                  RE                  S5        Xl#        Xl$        X`l%        U U l&        Xpl'        U=(       d    [P        RR                  U l*        U RH                  [         R$                  :X  a	  Ub  Xl'        U RH                  [         R                  :X  d<  U RH                  [         R                  :X  d  U RH                  [         R                  :X  a	  U	b  Xl'        U RJ                  b<  [	        U R                  [        5      (       a  U RJ                  U R                  l%        ggg)a  
Initializes the cache based on the given type.

Args:
    type (str, optional): The type of cache to initialize. Can be "local", "redis", "redis-semantic", "qdrant-semantic", "s3" or "disk". Defaults to "local".

    # Redis Cache Args
    host (str, optional): The host address for the Redis cache. Required if type is "redis".
    port (int, optional): The port number for the Redis cache. Required if type is "redis".
    password (str, optional): The password for the Redis cache. Required if type is "redis".
    namespace (str, optional): The namespace for the Redis cache. Required if type is "redis".
    ttl (float, optional): The ttl for the Redis cache
    redis_flush_size (int, optional): The number of keys to flush at a time. Defaults to 1000. Only used if batch redis set caching is used.
    redis_startup_nodes (list, optional): The list of startup nodes for the Redis cache. Defaults to None.

    # Qdrant Cache Args
    qdrant_api_base (str, optional): The url for your qdrant cluster. Required if type is "qdrant-semantic".
    qdrant_api_key (str, optional): The api_key for the local or cloud qdrant cluster.
    qdrant_collection_name (str, optional): The name for your qdrant collection. Required if type is "qdrant-semantic".
    similarity_threshold (float, optional): The similarity threshold for semantic-caching, Required if type is "redis-semantic" or "qdrant-semantic".

    # Disk Cache Args
    disk_cache_dir (str, optional): The directory for the disk cache. Defaults to None.

    # S3 Cache Args
    s3_bucket_name (str, optional): The bucket name for the s3 cache. Defaults to None.
    s3_region_name (str, optional): The region name for the s3 cache. Defaults to None.
    s3_api_version (str, optional): The api version for the s3 cache. Defaults to None.
    s3_use_ssl (bool, optional): The use ssl for the s3 cache. Defaults to True.
    s3_verify (bool, optional): The verify for the s3 cache. Defaults to None.
    s3_endpoint_url (str, optional): The endpoint url for the s3 cache. Defaults to None.
    s3_aws_access_key_id (str, optional): The aws access key id for the s3 cache. Defaults to None.
    s3_aws_secret_access_key (str, optional): The aws secret access key for the s3 cache. Defaults to None.
    s3_aws_session_token (str, optional): The aws session token for the s3 cache. Defaults to None.
    s3_config (dict, optional): The config for the s3 cache. Defaults to None.

    # GCS Cache Args
    gcs_bucket_name (str, optional): The bucket name for the gcs cache. Defaults to None.
    gcs_path_service_account (str, optional): Path to the service account json.
    gcs_path (str, optional): Folder path inside the bucket to store cache files.

    # Common Cache Args
    supported_call_types (list, optional): List of call types to cache for. Defaults to cache == on for all call types.
    **kwargs: Additional keyword arguments for redis.Redis() cache

Raises:
    ValueError: If an invalid cache type is provided.

Returns:
    None. Cache is set as a litellm param
REDIS_CLUSTER_NODESN)rE   rF   rG   rb   startup_nodesrk   rl   )rE   rF   rG   rb   )rE   rF   rG   rL   embedding_model
index_namer   )ValkeySemanticCache)rE   rF   rG   rL   rp   rq   ro   )re   rf   collection_namerL   quantization_configrp   vector_size)rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   )bucket_namepath_service_accountr]   )account_url	container)rd   cacher,   )+LiteLLMCacheTypeREDISr    
get_secret
isinstancestrjsonloadsr   rz   r   REDIS_SEMANTICr   VALKEY_SEMANTICvalkey_semantic_cacherr   QDRANT_SEMANTICr   LOCALr   S3r   GCSr   
AZURE_BLOBr   DISKr   input_callbackappendsuccess_callbacklogging_callback_manageradd_litellm_success_callback_async_success_callback"add_litellm_async_success_callbackrM   rC   rH   rb   rI   r(   r*   rD   )/selfrC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   kwargs_env_cluster_nodescluster_kwargsrr   s/                                                  r%   __init__Cache.__init__8   s   d #)))&%,%7%78M%N"%1j&7 7 +/**5G*H'" !  ((8%8" " '2<ON#89#/9IN#56(9(KN(K
' %%5	
 
 %444+ !%9 D: DJ %555 C, 	!%9 E;1	 	DJ %555, /- 6%9$> E=DJ %+++&DJ%(((  ---%# /%9)A%9# DJ %)))!+%=!DJ
 %000'-.DJ %***".ADJ'000""))'2'222,,II'R'999,,OOPWX$8!	" 0#;y';';	99(...3H3T,H II)///yy,;;;yy,<<<".+H>>%*TZZ*L*L#'>>DJJ  +M%r2   >   inputpromptmessages%_SEMANTIC_CACHE_SCOPE_EXCLUDED_PARAMS)user_api_keyuser_api_key_team_iduser_api_key_org_id.#_SEMANTIC_CACHE_TENANT_SCOPE_FIELDSreturnc                 z    U R                   [        R                  [        R                  [        R                  4;   $ r   )rC   r{   r   r   r   r   s    r%   _is_semantic_cacheCache._is_semantic_cache6  s3    yy++,,,,
 
 	
r2   r   c                 2   UR                  S5      =(       d    0 nUR                  S5      =(       d    0 nUR                  S5      =(       d    0 nSnU R                   H6  nUR                  U5      nUc  UR                  U5      nUc  M-  XV SU 3-  nM8     U$ )Nmetadatalitellm_params : )getr   )r   r   r   r   metadata_in_litellm_paramsscopefieldvalues           r%    _get_semantic_cache_tenant_scope&Cache._get_semantic_cache_tenant_scope=  s    J/52%zz*:;Ar+9+=+=j+I+OR"==ELL'E}266u= 7"UG,, > r2   c                 `   SnU R                   " S0 UD6nUb  [        R                  " SU5        U$ [        R                  " 5       n[
        nU R                  5       nU(       a  U R                  O	[        5       nU H  nX;   a  M
  X;   a4  U R                  X5      n	U	b  U[        U5       S[        U	5       3-  nMA  MC  X;  d  MJ  [        R                  SL d  M_  X   c  Mf  X   n	U[        U5       S[        U	5       3-  nM     U(       a  X R                  U5      -  n[        R                  U5      n
U R                   " U
40 UD6n
[        R                  " SU
[#        U5      5        UR%                  5        VVs0 s H  u  pUS:w  d  M  X_M     nnnU R&                  " SSU
0UD6  U
$ s  snnf )z
Get the cache key for the given arguments.

Args:
    **kwargs: kwargs to litellm.completion() or embedding()

Returns:
    str: The cache key generated from the arguments, or None if no cache key could be generated.
r   z
Returning preset cache key: %sr   Tz3
Created cache key: %s (source material length: %d)preset_cache_keyr,   )!_get_preset_cache_key_from_kwargsr   r   r   _get_all_llm_api_paramsr   r   r   	frozenset_get_param_valuer   r    3enable_caching_on_provider_specific_optional_paramsr   r4   _get_hashed_cache_key_add_namespace_to_cache_keylenitems_set_preset_cache_key_in_kwargs)r   r   	cache_keyr   combined_kwargslitellm_param_kwargsis_semantic_cachescope_excluded_paramsparamparam_valuehashed_cache_keykvkwargs_for_presets                 r%   get_cache_keyCache.get_cache_keyK  s    	  AAKFK'  !CEUV##*BBD1 335 ! 66 	
 E-'-1-B-B5-Q*CJ<r#k2B1C!DDI + 1 OOSWW}, "(-KCJ<r#k2B1C!DDI! $ >>vFFI 66yA;;<LWPVWB	N	
 /5llnXndaEW@WTQTnX,, 	
-	
1B	
  	 Ys   ;F*F*r   c                 f    US:X  a  U R                  U5      $ US:X  a  U R                  U5      $ X!   $ )z/
Get the value for the given param from kwargs
modelfile)_get_model_param_value_get_file_param_value)r   r   r   s      r%   r   Cache._get_param_value  s=     G..v66f_--f55}r2   c                 F   UR                  S0 5      =(       d    0 nUR                  S0 5      =(       d    0 nUR                  S0 5      =(       d    0 nUR                  S5      =(       d    UR                  S5      nU R                  X%5      nU=(       d    U=(       d    US   $ )a|  
Handles getting the value for the 'model' param from kwargs

1. If caching groups are set, then return the caching group as the model https://docs.litellm.ai/docs/routing#caching-across-model-groups
2. Else if a model_group is set, then return the model_group as the model. This is used for all requests sent through the litellm.Router()
3. Else use the `model` passed in kwargs
r   r   model_groupr   )r   _get_caching_group)r   r   r   r   r   r   caching_groups          r%   r   Cache._get_model_param_value  s      J39r%zz*:B?E2+9+=+=j"+M+SQS"%-\\&
 &;'++M: 	 //F>>vg>r2   r   r   c                 l    UR                  S/ 5      nU(       a  U H  nX$;   d  M
  [        U5      s  $    g )Ncaching_groups)r   r   )r   r   r   r   groups        r%   r   Cache._get_caching_group  s7     *26F)K''u:% ( r2   c                    UR                  S5      nUR                  S0 5      nUR                  S0 5      nUR                  S5      =(       d=    [        USS5      =(       d)    UR                  S5      =(       d    UR                  S5      $ )z_
Handles getting the value for the 'file' param from kwargs. Used for `transcription` requests
r   r   r   file_checksumnameN	file_name)r   getattr)r   r   r   r   r   s        r%   r   Cache._get_file_param_value  s{     zz&!::j"-$4b9LL) /tVT*/||K(/ !!+.		
r2   c                 H    U(       a  SU;   a  US   R                  SS5      $ g)z
Get the preset cache key from kwargs["litellm_params"]

We use _get_preset_cache_keys for two reasons

1. optional params like max_tokens, get transformed for bedrock -> max_new_tokens
2. avoid doing duplicate / repeated work
r   r   N)r   )r   r   s     r%   r   'Cache._get_preset_cache_key_from_kwargs  s,     6)./334FMMr2   r   c                 0    U(       a  SU;   a  XS   S'   ggg)z
Set the calculated cache key in kwargs

This is used to avoid doing duplicate / repeated work

Placed in kwargs["litellm_params"]
r   r   Nr,   )r   r   r   s      r%   r   %Cache._set_preset_cache_key_in_kwargs  s(     6)?O'();< * r2   r   c                     [         R                  " U R                  5       5      nUR                  5       n[        R
                  " SU5        U$ )z
Get the hashed cache key for the given cache key.

Use hashlib to create a sha256 hash of the cache key

Args:
    cache_key (str): The cache key to hash.

Returns:
    str: The hashed cache key.
zHashed cache key (SHA-256): %s)hashlibsha256encode	hexdigestr   r   )r   hash_objecthash_hexs      r%   r   Cache._get_hashed_cache_key  s?     nnY%5%5%78((*=xHr2   r   c                     UR                  S0 5      nUR                  S5      =(       d    0 nUR                  S5      =(       d$    UR                  S5      =(       d    U R                  nU(       a  U SU 3n[        R                  " SU5        U$ )z
If a redis namespace is provided, add it to the cache key

Args:
    hash_hex (str): The hashed cache key.
    **kwargs: Additional keyword arguments.

Returns:
    str: The final hashed cache key with the redis namespace.
rz   r   rH   redis_namespace:zFinal hashed key: %s)r   rH   r   r   )r   r   r   dynamic_cache_controlr   rH   s         r%   r   !Cache._add_namespace_to_cache_key  s     6<ZZ5L::j)/R!%%k2 ||-.~~ 	
 #AhZ0H3X>r2   c              #      #    Sn[        S[        U5      U5       H-  nSSSXX2-    S.0/0v   [        R                  " [        5        M/     g 7f)N   r   choicesdelta	assistant)rolecontent)ranger   timesleepr   )r   r   
chunk_sizeis       r%   generate_streaming_content Cache.generate_streaming_content  s\     
q#g,
3A$/'.1>'B"	 	 JJ34 4s   AAcached_resultmax_agec                 d   Ub  [        U[        5      (       ap  SU;   aj  US   n[        R                  " 5       nXC-
  nUb  XR:  a  gUR                  S5      n [        U[        5      (       a   U$ [        R
                  " U5      n U$ U$ ! [         a    [        R                  " U5      n U$ f = f)z<
Common get cache logic across sync + async implementations
N	timestampresponse)	r~   dictr   r   r   r   r#   astliteral_eval)r   r   r   r  current_timeresponse_agecached_responses          r%   _get_cache_logicCache._get_cache_logic  s     %=$//},%k2I99;L (3L "|'= ,//
;ODot44 #" '+jj''O
 #"  D"%"2"2?"C""Ds   B 0B  B/.B/c                     0 nS H  nX ;   d  M
  X   X'   M     [        U R                  S5      [        5      (       a  0 US'   U$ )N)r   r   r   )r~   r   r  )r   cache_lookup_kwargsprompt_kwargs      r%   _get_safe_cache_lookup_kwargs#Cache._get_safe_cache_lookup_kwargs:  sM    .01L%4:4H#1 2 fjj,d33.0
+""r2   original_kwargsr  c                     U R                  S5      nUR                  S5      n[        U[        5      (       a  [        U[        5      (       d  g SU;   a	  US   US'   g g )Nr   zsemantic-similarity)r   r~   r  )r  r  original_metadatacache_lookup_metadatas       r%   )_update_metadata_from_cache_lookup_kwargs/Cache._update_metadata_from_cache_lookup_kwargsF  sl     ,//
; 3 7 7
 C+T22*!4;
 ;
  $997L%834 :r2   dynamic_cache_objectc                 8    U R                   " S0 UD6SLa  gSU;   a  US   nOU R                  " S0 UD6nUb  UR                  S0 5      nUR                  S5      =(       d#    UR                  S5      =(       d    [        S5      nU R	                  U5      nUb  UR
                  " U40 UD6nOU R                  R
                  " U40 UD6nU R                  UUS9  U R                  XuS	9$ g! [         a$    [        S
[        R                  " 5        35         gf = f)z
Retrieves the cached result for the given arguments.

Args:
    *args: args to litellm.completion() or embedding()
    **kwargs: kwargs to litellm.completion() or embedding()

Returns:
    The cached result if it exists, otherwise None.
TNr   rz   s-maxage	s-max-ageinf)r  r  r   r   An exception occurred: r,   )should_use_cacher   r   floatr  	get_cacherz   r  r	  r#   r&   	traceback
format_exc)r   r  r   r   cache_control_argsr   r  r   s           r%   r  Cache.get_cacheV  sN    	$$.v.d:f$";/	 ..88	$:@**Wb:Q"&**:6 $)--k:$U| 
 '+&H&H&P#'3$8$B$B!%%8%M %)JJ$8$8!%%8%M >>$*(; ?  ,,"/ -  ) %.  	3I4H4H4J3KLM	s   C+ CC+ ++DDc           	        #     U R                   " S
0 UD6SLa  gSU;   a  US   nOU R                  " S
0 UD6nUb  UR                  S0 5      nUR                  SUR                  S[        S5      5      5      nUb  UR                  " U40 UD6I Sh  vN nO%U R
                  R                  " U40 UD6I Sh  vN nU R                  XeS9$ g N: N! [         a$    [        S	[        R                  " 5        35         gf = f7f)zL
Async get cache implementation.

Used for embedding calls in async wrapper
TNr   rz   r  r  r  r  r  r,   )r  r   r   r  async_get_cacherz   r	  r#   r&   r   r!  )r   r  r   r   r"  r   r   s          r%   r%  Cache.async_get_cache  s-    	$$.v.d:f$";/	 ..88	$%+ZZ%<",00!3!7!7
E%L!Q (3*>*N*N!+%++ %M +/***D*D!+%++ %M ,,"/ -   %%%  	3I4H4H4J3KLM	sW   DC DA7C C%C 7C8C 
DC C +C>;D=C>>Dc                     SU;   a  US   nOU R                   " S0 UD6nUb  [        U[        5      (       a  UR                  5       nU R                  b  U R                  US'   UR                  SS5      n[        U[        5      (       a%  UR                  5        H  u  pVUS:X  d  M  XbS'   M     [        R                  " 5       US.nX7U4$ [        S5      e! [         a  nUeSnAff = f)z?
Common implementation across sync + async add_cache functions
r   NrI   rz   )r  r  zcache key is Noner,   )
r   r~   r
   model_dump_jsonrI   r   r  r   r   r#   )	r   resultr   r   _cache_kwargsr   r   cached_dataes	            r%   _add_cache_logicCache._add_cache_logic  s    	f$";/	 ..88	$fi00#335F 88'$(HHF5M &

7D 9mT22 - 3 3 5:,-5M !6 -1IIKVL v55 344 	G	s$   B"C (#C C 
C(!C##C(c                     U R                   " S0 UD6SLa  gU R                  " SSU0UD6u  p4nU R                  R                  " X440 UD6  g! [         a,  n[
        R                  " S[        U5       35         SnAgSnAff = f)z
Adds a result to the cache.

Args:
    *args: args to litellm.completion() or embedding()
    **kwargs: kwargs to litellm.completion() or embedding()

Returns:
    None
TNr)  #LiteLLM Cache: Excepton add_cache: r,   )r  r-  rz   	set_cacher#   r   	exceptionr   )r   r)  r   r   r+  r,  s         r%   	add_cacheCache.add_cache  s    	U$$.v.d:-1-B-B ..!'.*IF JJ  B6B 	U$$'J3q6(%STT	Us   A 4A 
B"A>>Bc                   #     U R                   " S0 UD6SLa  gU R                  S:X  a)  U R                  b  U R                  " U40 UD6I Sh  vN   gU R                  " SSU0UD6u  pEnUb  UR
                  " XE40 UD6I Sh  vN   gU R                  R
                  " XE40 UD6I Sh  vN   g Na N- N	! [         a,  n[        R                  " S[        U5       35         SnAgSnAff = f7f)z#
Async implementation of add_cache
TNredisr)  r0  r,   )r  rC   rb   batch_cache_writer-  async_set_cacherz   r#   r   r2  r   )r   r)  r  r   r   r+  r,  s          r%   async_add_cacheCache.async_add_cache  s    	U$$.v.d:yyG#(=(=(I,,V>v>>>151F1F 2!2%+2.	 (3.>>!28   **44YVvVVV ? W 	U$$'J3q6(%STT	Us   C.B5 C.3B5 B/B5 C.0B5 B1B5 C.	 B5 )B3*B5 .C./B5 1B5 3B5 5
C+?"C&!C.&C++C.embedding_responser   prompt_tokensprompt_tokens_detailsc                 
    [        U[        5      (       a6  UR                  S5      UR                  S5      UR                  S5      UUUS.$ [        US5      (       aF  UR	                  5       nUR                  S5      UR                  S5      UR                  S5      UUUS.$ [        U5      nUR                  S5      UR                  S5      UR                  S5      UUUS.$ ! [         a  n[        SU 35      eSnAff = f)zX
Convert any embedding response into the standardized CachedEmbedding TypedDict format.
r9   indexobject)r9   r?  r@  r   r<  r=  
model_dumpz,Missing expected key in embedding response: N)r~   r  r   hasattrrA  varsKeyError
ValueError)r   r;  r   r<  r=  datar,  s          r%   _convert_to_cached_embedding"Cache._convert_to_cached_embedding  s   	Q,d33!3!7!7!D/33G<044X>"%2-B  +\::)446!%+!6!XXg."hhx0"%2-B  ./!%+!6!XXg."hhx0"%2-B   	QKA3OPP	Qs&   A
C% AC% $A C% %
D/C==Dr)  idx_in_result_datac                    UR                   b  UR                   R                  c  gUR                   R                  n[        US5      (       a  UR                  SS9nO@[	        U[
        5      (       a*  UR                  5        VVs0 s H  u  pVUc  M
  XV_M     nnnOgU(       d  g[        UR                  5      nUS::  a  U$ 0 nUR                  5        HX  u  p[	        U
[        5      (       a  [        X5      u  pXU:  a  SOS-   X'   M7  [	        U
[        5      (       a  X-  X'   MT  XU	'   MZ     U(       a  U$ S$ s  snnf )a_  
Extract per-item prompt_tokens_details from a response for caching.

For single-item responses (common for multimodal providers like Bedrock Titan,
Nova, Vertex AI), returns the full prompt_tokens_details.
For multi-item responses, distributes integer fields evenly across items
so that summing all per-item details reconstructs the original totals.
NrA  T)exclude_noner   r   )usager=  rB  rA  r~   r  r   r   rF  intdivmodr  )r   r)  rI  detailsdetails_dictr   r   	num_itemsper_itemkeyr   quotient	remainders                r%   #_get_per_item_prompt_tokens_details)Cache._get_per_item_prompt_tokens_details  s!    <<6<<#E#E#M,,447L))"--4-@L&&-4]]_N_TQDAD_LNL$	> &,,.JC%%%&,U&># (i1OAUV WE5)) % 1 % / $x--+ Os   	D;D;c                     UR                   b  UR                   R                  c  gUR                   R                  n[        UR                  5      nUS::  a  U$ [	        X45      u  pVXRU:  a  S-   $ S-   $ )a	  
Extract the per-item prompt_tokens from a response for caching.

Single-item responses store the full usage.prompt_tokens. Multi-item
responses distribute it evenly (with remainder) so that summing all
per-item values on retrieval reconstructs the original total.
Nr   r   )rL  r<  r   rF  rN  )r   r)  rI  totalrQ  rT  rU  s          r%   _get_per_item_prompt_tokens!Cache._get_per_item_prompt_tokensJ  sn     <<6<<#=#=#E**$	>L$U6Y >1FFAFFr2   r   c                 
   U R                   " S0 0 UESU0ED6nXSS'   UR                  U   nU R                  UUS9nU R                  UUS9nUR                  n	U R                  UU	UUS9n
U R                  " SSU
0UD6u  pnXU4$ )Nr   r   )r)  rI  )r<  r=  r)  r,   )r   rF  rZ  rV  r   rG  r-  )r   r)  r   r   rI  r   r;  r<  r=  
model_nameembedding_dictr   r+  s                r%   add_embedding_response_to_cache%Cache.add_embedding_response_to_cachea  s      --K0J60J7E0JK.{#[[);< 881 9 
 !% H H1 !I !
 \\
*.*K*K'"7	 +L +
 *.)>)> *
!*
*
&	 v--r2   c                   #     U R                   " S0 UD6SLa  gU R                  b  U R                  US'   / n[        US   [        5      (       a@  [	        US   5       H-  u  pVU R                  XX55      u  nnnUR                  Xx45        M/     OB[        US   [        5      (       a*  U R                  XS   U5      u  pxnUR                  Xx45        Ub  UR                  " SSU0UD6I Sh  vN   gU R                  R                  " SSU0UD6I Sh  vN   g N, N! [         a,  n	[        R                  " S[        U	5       35         Sn	A	gSn	A	ff = f7f)zm
Async implementation of add_cache for Embedding calls

Does a bulk write, to prevent using too many clients
TNrI   r   
cache_listr0  r,   )r  rI   r~   list	enumerater_  r   r   async_set_cache_pipelinerz   r#   r   r2  )
r   r)  r  r   rb  idxr   r   r+  r,  s
             r%   async_add_cache_pipelineCache.async_add_cache_pipeline  ss     	U$$.v.d: xx# $uJ&/400'w8FC
 <<VT	!#%%y&>? 9 F7OS11151U1U7OV2.	 !!9":;#/*CC )-3   jj99 )-3  	  	U$$'J3q6(%STT	Usi   ED ECD ,D-D 1E2!D DD ED D 
E'"E	EEEc                     U R                   [        R                  :X  a  gUR                  SS5      n[        R
                  " SX5        U(       a+  [        U[        5      (       a  UR                  SS5      SL a  gg)z
Returns true if we should use the cache for LLM API calls

If cache is default_on then this is True
If cache is default_off then this is only true when user has opted in to use cache
Trz   Nz(should_use_cache: kwargs: %s; _cache: %sz	use-cacheF)rD   r(   r*   r   r   r   r~   r  )r   r   _caches      r%   r  Cache.should_use_cache  sd     99	,,, GT*GXj..zz+u-5r2   c                    #    U R                   " SSU0UD6u  p4nU R                  R                  " X440 UD6I S h  vN   g  N7f)Nr)  r,   )r-  rz   r7  )r   r)  r   r   r+  s        r%   r7  Cache.batch_cache_write  s@     )-)>)>)Wf)WPV)W&	jj**9LVLLLs   8AA Ac                 h   #    [        U R                  S5      nU(       a  U" 5       I S h  vN $ g  N7f)Npingr   rz   )r   
cache_pings     r%   ro  
Cache.ping  s+     TZZ0
#%% &s   (202c                 j   #    [        U R                  S5      nU(       a  U" U5      I S h  vN $ g  N7f)Ndelete_cache_keysrp  )r   keyscache_delete_cache_keyss      r%   rt  Cache.delete_cache_keys  s0     ")$**6I"J"0666 7s   )313c                    #    [        U R                  S5      (       a#  U R                  R                  5       I S h  vN   g g  N7f)N
disconnect)rB  rz   ry  r   s    r%   ry  Cache.disconnect  s4     4::|,,**''))) -)s   9AAAc                     g)z}
Internal method to check if the cache type supports async get/set operations

All cache types now support async operations

Tr,   r   s    r%   _supports_asyncCache._supports_async  s     r2   )rz   rD   rH   rb   rM   rI   rC   r   )NN)r   )>r-   r.   r/   r0   r{   r   r(   r*   r   r   r  r   CachingSupportedCallTypesboolr	   r   rM  r   r   r   __annotations__r   tupler   r  r   r   r   r   r   r   r   r   staticmethodr   r   r   r	  r   r  r  r   r  r%  r-  r3  r9  CachedEmbeddingrG  r   rV  rZ  r   r_  rg  r  r7  ro  rt  ry  r|  r1   r,   r2   r%   r4   r4   7   s    ,<+A+A   """&#'#150404K
 ,0.2(,(,(,%)04)-.226.2#'!%)-26"&4L9=5M7;*..2(,)-(,04485M;?-1*.yl2'(l2 
l2 sml2 sml2 3-l2 C=l2 e_l2  (l2 'uol2 'uol2 't,E'FGl2: $C=;l2< 'sm=l2> !?l2@ !Al2B !Cl2D TNEl2F E$),-Gl2H "#Il2J 'smKl2L #+3-Ml2N 'smOl2P C=Ql2R #Sl2T "#Ul2V #+3-Wl2X 3-Yl2Z /2[l2\ *2#]l2^ 03_l2` +.*al2b #3-cl2d &d^el2f !gl2h "#il2j !kl2l !)ml2n %-SMol2p 03ql2r ,4C=sl2v &c]wl2x #3-yl2b 8A'8)9 <'sCx 
D 
t  <  < |  
#	?T ?c ?"+3C=	#
D 
S 
Xc] 
P 
PRV 
P    $C c .5%}% %%N 	#d38n 	#c3h 	# 	# c3h>B38n	 +hy.A +\ ;?#$,Y$7#J<U, CGU,4Y,?U: (,04)Q)Q })Q  }	)Q
  (~)Q 
)QV).!).  ). 
$	).VG!G  G 
#	G8 #$".!". ". 	".
  ". 
sD$	".J CG(U,4Y,?(UT$M* r2   r4   r6   rC   rE   rF   rG   rM   c           	      $   [        S5        S[        R                  ;  a  [        R                  R                  S5        S[        R                  ;  a  [        R
                  R                  S5        S[        R                  ;  a  [        R
                  R                  S5        [        R                  c  [        SU UUUUS.UD6[        l	        [        S[        R                   35        [        S[        [        R                  5       35        g)a  
Enable cache with the specified configuration.

Args:
    type (Optional[Literal["local", "redis", "s3", "disk"]]): The type of cache to enable. Defaults to "local".
    host (Optional[str]): The host address of the cache server. Defaults to None.
    port (Optional[str]): The port number of the cache server. Defaults to None.
    password (Optional[str]): The password for the cache server. Defaults to None.
    supported_call_types (Optional[List[Literal["completion", "acompletion", "embedding", "aembedding"]]]):
        The supported call types for the cache. Defaults to ["completion", "acompletion", "embedding", "aembedding"].
    **kwargs: Additional keyword arguments.

Returns:
    None

Raises:
    None
zLiteLLM: Enabling Cacherz   NrC   rE   rF   rG   rM   z&LiteLLM: Cache enabled, litellm.cache=LiteLLM Cache: r,   )r&   r    r   r   r   r   r   r   r   rz   r4   rC  rC   rE   rF   rG   rM   r   s         r%   enable_cacher    s    N +,g,,,%%g.g...((EEgNg555((KKGT}} 
!5
 
 :7==/JKOD$7#89:r2   c           	          [        S5        [        SU UUUUS.UD6[        l        [        S[        R                   35        [        S[	        [        R                  5       35        g)a  
Update the cache for LiteLLM.

Args:
    type (Optional[Literal["local", "redis", "s3", "disk"]]): The type of cache. Defaults to "local".
    host (Optional[str]): The host of the cache. Defaults to None.
    port (Optional[str]): The port of the cache. Defaults to None.
    password (Optional[str]): The password for the cache. Defaults to None.
    supported_call_types (Optional[List[Literal["completion", "acompletion", "embedding", "aembedding"]]]):
        The supported call types for the cache. Defaults to ["completion", "acompletion", "embedding", "aembedding"].
    **kwargs: Additional keyword arguments for the cache.

Returns:
    None

zLiteLLM: Updating Cacher  z&LiteLLM: Cache Updated, litellm.cache=r  Nr,   )r&   r4   r    rz   rC  r  s         r%   update_cacher    sg    J +, 1 GM :7==/JKOD$7#89:r2   c                  z   SSK Jn   [        S5        U " [        5         [        R
                  R                  S5        [        R                  R                  S5        [        R                  R                  S5        SSS5        S[        l	        [        S[        R                   35        g! , (       d  f       N6= f)a*  
Disable the cache used by LiteLLM.

This function disables the cache used by the LiteLLM module. It removes the cache-related callbacks from the input_callback, success_callback, and _async_success_callback lists. It also sets the litellm.cache attribute to None.

Parameters:
None

Returns:
None
r   suppresszLiteLLM: Disabling Cacherz   Nz'LiteLLM: Cache disabled, litellm.cache=)

contextlibr  r&   rE  r    r   remover   r   rz   r  s    r%   disable_cacher  M  s     $,-	*	%%g.  ''0''..w7 

 GM;GMM?KL 
	s   AB,,
B:);r  r   r   r   r   enumr   typingr   r   r   r   r   r	   pydanticr
   r    litellm._loggingr   litellm.constantsr   -litellm.litellm_core_utils.model_param_helperr   litellm.types.cachinglitellm.types.utilsr   r   azure_blob_cacher   
base_cacher   
disk_cacher   
dual_cacher   	gcs_cacher   in_memory_cacher   qdrant_semantic_cacher   redis_cacher   redis_cluster_cacher   redis_semantic_cacher   s3_cacher   r&   r   r(   r4   r{   r   r~  r  r  r  r,   r2   r%   <module>r     sQ         : :   + : J # E , ! ! !  * 6 # 2 4  T  e eR (8'='="G9;
#
$9;
3-9; 3-9; sm	9;
 #4(A#BC9;z (8'='="G/;
#
$/;
3-/; 3-/; sm	/;
 #4(A#BC/;dMr2   