
    i&                         d Z ddlZddlZddlmZmZmZmZmZm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZmZ erddlmZ dd	lmZ eZeeef   ZneZeZ G d
 de      Z G d d      Zy)zf
Wrapper around router cache. Meant to store model id when prompt caching supported prompt is called.
    N)TYPE_CHECKINGAnyListOptionalUnioncast)	TypedDict)	DualCache)InMemoryCache)AllMessageValuesChatCompletionToolParam)Span)Routerc                       e Zd ZU eed<   y)PromptCachingCacheValuemodel_idN)__name__
__module____qualname__str__annotations__     z/Users/manta/Documents/Projects/TheRoad-I1/.venv/lib/python3.12/site-packages/litellm/router_utils/prompt_caching_cache.pyr   r      s    Mr   r   c                   X   e Zd ZdefdZededefd       Zedee	   dee	   fd       Z
edeee	      deee      dee   fd	       Zd
edeee	      deee      ddfdZd
edeee	      deee      ddfdZdeee	      deee      dee   fdZdeee	      deee      dee   fdZy)PromptCachingCachecachec                 0    || _         t               | _        y N)r   r   in_memory_cache)selfr   s     r   __init__zPromptCachingCache.__init__    s    
,r   objreturnc                 J   t        | d      r| j                         S t        | t              rt        j                  | dd      S t        | t
              r#| D cg c]  }t        j                  |       c}S t        | t        t        t        f      r| S t        |       S c c}w )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   r2   z#PromptCachingCache.serialize_object$   s     388:T"::t
  T"JMN#$&77=#NNc5$/0J3x Os   B messagesc           
         | s| S d}d}t        |       D ]  \  }}|j                  d      }|j                  d      }|(t        |t              r|j                  d      dk(  r|}d}t        |t              sct        |      D ]T  \  }}t        |t              s|j                  d      }	|	+t        |	t              s<|	j                  d      dk(  sQ|}|}V  |g S g }
t        |       D ]  \  }}||k  r|
j                  |       ||k(  rc|j                  d      }t        |t              r0|.t        t        i |d|d|dz    i      }|
j                  |       s|
j                  |        |
S  |
S )a  
        Extract the cacheable prefix from messages.

        The cacheable prefix is everything UP TO AND INCLUDING the LAST content block
        (across all messages) that has cache_control. This includes ALL blocks before
        the last cacheable block (even if they don't have cache_control).

        Args:
            messages: List of messages to extract cacheable prefix from

        Returns:
            List of messages containing only the cacheable prefix
        Ncontentcache_controltype	ephemeral   )	enumerategetr.   r&   r1   appendr   r   )r7   last_cacheable_message_idxlast_cacheable_content_idxmsg_idxmessager9   message_level_cache_controlcontent_idxcontent_blockr:   cacheable_prefixmessage_copys               r   extract_cacheable_prefixz+PromptCachingCache.extract_cacheable_prefix7   s   " O &*"%)" )( 3GWkk),G
 +2++o*F'+7:DA/33F;{J-4* .2* gt,.7.@*]mT2$1$5$5o$FM%1&}d;)--f5D5<25@2 /A+ !4B &-I  )( 3GW33 ''066!++i0gt,1K1W#'(%%w/O1Ka1O'P$L %++L9 %++G4 1 !40  r   toolsc                 V   | |y d }| t         j                  |       }|sy i }|t         j                  |      }||d<   |t         j                  |      }||d<   t        j                  |dd      }t        j                  |j                               j                         }d| dS )Nr7   rK   Tr'   r*   zdeployment:z:prompt_caching)	r   rJ   r2   r/   r0   hashlibsha256encode	hexdigest)r7   rK   cacheable_messagesdata_to_hashserialized_messagesserialized_toolsdata_to_hash_strhashed_datas           r   get_prompt_caching_cache_keyz/PromptCachingCache.get_prompt_caching_cache_key   s    
  "!3!L!LX!V% )"4"E"E"# (;L$1BB5I$4L!  ::!
 nn%5%<%<%>?IIK[M99r   r   Nc                     ||y t         j                  ||      }|y | j                  j                  |t	        |      d       y N)r   i,  )ttl)r   rW   r   	set_cacher   r!   r   r7   rK   	cache_keys        r   add_model_idzPromptCachingCache.add_model_id   sU     &CCHeT	

.As 	 	
 r   c                    K   ||y t         j                  ||      }|y | j                  j                  |t	        |      d       d {    y 7 wrY   )r   rW   r   async_set_cacher   r\   s        r   async_add_model_idz%PromptCachingCache.async_add_model_id   si      &CCHeT	jj((#X6 ) 
 	
 	

 	
s   A
AAAc                    K   ||yt         j                  ||      }|y| j                  j                  |       d{   }|S 7 w)a1  
        Get model ID from cache using the cacheable prefix.

        The cache key is based on the cacheable prefix (everything up to and including
        the last cache_control block), so requests with the same cacheable prefix but
        different user messages will have the same cache key.
        N)key)r   rW   r   async_get_cache)r!   r7   rK   r]   cache_results        r   async_get_model_idz%PromptCachingCache.async_get_model_id   sZ       'CCHeT	 "ZZ77I7FF Gs   >A	 AA	c                 t    ||y t         j                  ||      }|y | j                  j                  |      S r   )r   rW   r   	get_cache)r!   r7   rK   r]   s       r   get_model_idzPromptCachingCache.get_model_id   sA    
 &CCHeT	zz##I..r   )r   r   r   r
   r"   staticmethodr   r2   r   r   rJ   r   r   r   rW   r^   ra   r   rf   ri   r   r   r   r   r      s   /i / c c  $ V '(V 		V  V p #:4 012#:456#: 
##: #:J 4 012 456	
 
& 4 012 456	
 
*4 012 456 
)	*	0/4 012/ 456/ 
)	*	/r   r   )__doc__rM   r/   typingr   r   r   r   r   r   typing_extensionsr	   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      sb      B B ' - 9 O1%NDDNi d/ d/r   