
    ^hh                         S r SSKrSSKrSSKJrJrJrJrJr  SSK	J
r
  SSKJr  SSKJrJr  \(       a  SSKJr  SSKJr  \r\rO\r\r " S	 S
\5      r " S S5      rg)zf
Wrapper around router cache. Meant to store model id when prompt caching supported prompt is called.
    N)TYPE_CHECKINGAnyListOptional	TypedDict)	DualCache)InMemoryCache)AllMessageValuesChatCompletionToolParam)Span)Routerc                        \ rS rSr% \\S'   Srg)PromptCachingCacheValue   model_id N)__name__
__module____qualname____firstlineno__str__annotations____static_attributes__r       c/home/james-whalen/.local/lib/python3.13/site-packages/litellm/router_utils/prompt_caching_cache.pyr   r      s    Mr   r   c                   H   \ rS rSrS\4S jr\S\S\4S j5       r\S\	\
\      S\	\
\      S\	\   4S	 j5       rS
\S\	\
\      S\	\
\      SS4S jrS
\S\	\
\      S\	\
\      SS4S jrS\	\
\      S\	\
\      S\	\   4S jrS\	\
\      S\	\
\      S\	\   4S jrSrg)PromptCachingCache   cachec                 .    Xl         [        5       U l        g N)r   r	   in_memory_cache)selfr   s     r   __init__PromptCachingCache.__init__   s    
,r   objreturnc                 t   [        U S5      (       a  U R                  5       $ [        U [        5      (       a  [        R                  " U SSS9$ [        U [
        5      (       a&  U  Vs/ s H  n[        R                  U5      PM     sn$ [        U [        [        [        45      (       a  U $ [        U 5      $ s  snf )zSHelper function to serialize Pydantic objects, dictionaries, or fallback to string.dictT,:	sort_keys
separators)hasattrr)   
isinstancejsondumpslistr   serialize_objectintfloatboolr   )r&   items     r   r5   #PromptCachingCache.serialize_object"   s     388:T""::t
  T""JMN#$&77=#NNc5$/00J3x Os   &B5messagestoolsc                     U c  Uc  g 0 nU b  [         R                  U 5      nX2S'   Ub  [         R                  U5      nXBS'   [        R                  " USSS9n[        R
                  " UR                  5       5      R                  5       nSU S3$ )Nr;   r<   Tr*   r-   zdeployment:z:prompt_caching)r   r5   r2   r3   hashlibsha256encode	hexdigest)r;   r<   data_to_hashserialized_messagesserialized_toolsdata_to_hash_strhashed_datas          r   get_prompt_caching_cache_key/PromptCachingCache.get_prompt_caching_cache_key5   s    
 "4"E"Eh"O':$1BB5I$4!  ::!
 nn%5%<%<%>?IIK[M99r   r   Nc                     Uc  Uc  g [         R                  X#5      nU R                  R                  U[	        US9SS9  g N)r   i,  )ttl)r   rG   r   	set_cacher   r#   r   r;   r<   	cache_keys        r   add_model_idPromptCachingCache.add_model_idP   sJ     &CCHT	

.As 	 	
 r   c                    #    Uc  Uc  g [         R                  X#5      nU R                  R                  U[	        US9SS9I S h  vN   g  N7frJ   )r   rG   r   async_set_cacher   rM   s        r   async_add_model_id%PromptCachingCache.async_add_model_id_   s]      &CCHT	jj((#X6 ) 
 	
 	

 	
s   AAA
Ac           	        #    Uc  Uc  g/ nUbx  [         R                  X5      nUR                  U5        [        S[	        S[        U5      5      5       H/  nUSU*  n[         R                  Xb5      nUR                  U5        M1     U R                  R                  US9I Sh  vN nUc  gU H
  n	U	c  M  U	s  $    g N7f)z
if messages is not none
- check full messages
- check messages[:-1]
- check messages[:-2]
- check messages[:-3]

use self.cache.async_batch_get_cache(keys=potential_cache_keys])
N      )keys)r   rG   appendrangeminlenr   async_batch_get_cache)
r#   r;   r<   potential_cache_keysfull_cache_keyipartial_messagespartial_cache_keycache_resultsresults
             r   async_get_model_id%PromptCachingCache.async_get_model_idp   s        "/LLN !''7 1c!S]34#+CaR= $6$S$S$%! %++,=> 5 #jj>>% ? 
 
   $F! $ 
s   B!B?#B=$B?6B?c                 p    Uc  Uc  g [         R                  X5      nU R                  R                  U5      $ r!   )r   rG   r   	get_cache)r#   r;   r<   rN   s       r   get_model_idPromptCachingCache.get_model_id   s6    
 &CCHT	zz##I..r   )r   r"   )r   r   r   r   r   r$   staticmethodr   r5   r   r   r
   r   r   rG   rO   rS   r   re   ri   r   r   r   r   r   r      su   /i / c c  $ :4 012:456: 
#: :4 4 012 456	
 
 4 012 456	
 
"04 0120 4560 
)	*	0d	/4 012	/ 456	/ 
)	*		/r   r   )__doc__r>   r2   typingr   r   r   r   r   litellm.caching.cachingr   litellm.caching.in_memory_cacher	   litellm.types.llms.openair
   r   opentelemetry.tracer   _Spanlitellm.routerr   litellm_routerr   r   r   r   r   <module>ru      sV      @ @ - 9 O1%NDDNi N/ N/r   