o
    ưi&                     @   s   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rKddlmZ dd	lmZ eZeeef ZneZeZG d
d deZG dd dZdS )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                   @   s   e Zd ZU eed< dS )PromptCachingCacheValuemodel_idN)__name__
__module____qualname__str__annotations__ r   r   `/home/app/Keep/.python/lib/python3.10/site-packages/litellm/router_utils/prompt_caching_cache.pyr      s   
 r   c                   @   s$  e Zd ZdefddZededefddZedee	 dee	 fd	d
Z
edeee	  deee  dee fddZdedeee	  deee  ddfddZdedeee	  deee  ddfddZdeee	  deee  dee fddZdeee	  deee  dee fddZdS )PromptCachingCachecachec                 C   s   || _ t | _d S N)r   r
   Zin_memory_cache)selfr   r   r   r   __init__    s   zPromptCachingCache.__init__objreturnc                 C   s`   t | dr	|  S t| trtj| dddS t| tr"dd | D S t| tttfr,| S t	| S )zSHelper function to serialize Pydantic objects, dictionaries, or fallback to string.dictT,:	sort_keys
separatorsc                 S   s   g | ]}t |qS r   )r   serialize_object).0itemr   r   r   
<listcomp>2   s    z7PromptCachingCache.serialize_object.<locals>.<listcomp>)
hasattrr   
isinstancejsondumpslistintfloatboolr   )r   r   r   r   r&   $   s   


z#PromptCachingCache.serialize_objectmessagesc              	   C   sT  | s| S d}d}t | D ]O\}}|d}|d}|dur.t|tr.|ddkr.|}d}t|ts4qt |D ]"\}}t|trZ|d}	|	durZt|	trZ|	ddkrZ|}|}q8q|du rbg S g }
t | D ]?\}}||k rv|
| qh||kr|d}t|tr|durtti |d|d|d  i}|
| qh|
| qh |
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typeZ	ephemeral   )	enumerategetr+   r   r.   appendr   r   )r2   Zlast_cacheable_message_idxZlast_cacheable_content_idxZmsg_idxmessager3   Zmessage_level_cache_controlZcontent_idxZcontent_blockr4   Zcacheable_prefixZmessage_copyr   r   r   extract_cacheable_prefix7   sV   





z+PromptCachingCache.extract_cacheable_prefixtoolsc                 C   s   | d u r
|d u r
d S d }| d urt | }|sd S i }|d ur(t |}||d< |d ur5t |}||d< tj|ddd}t|  }d| dS )Nr2   r<   Tr    r#   zdeployment:z:prompt_caching)	r   r;   r&   r,   r-   hashlibsha256encode	hexdigest)r2   r<   Zcacheable_messagesZdata_to_hashZserialized_messagesZserialized_toolsZdata_to_hash_strZhashed_datar   r   r   get_prompt_caching_cache_key   s*   


z/PromptCachingCache.get_prompt_caching_cache_keyr   Nc                 C   sH   |d u r
|d u r
d S t ||}|d u rd S | jj|t|ddd d S N)r   i,  )ttl)r   rA   r   Z	set_cacher   r   r   r2   r<   	cache_keyr   r   r   add_model_id   s   zPromptCachingCache.add_model_idc                    sP   |d u r|d u rd S t ||}|d u rd S | jj|t|dddI d H  d S rB   )r   rA   r   Zasync_set_cacher   rD   r   r   r   async_add_model_id   s   z%PromptCachingCache.async_add_model_idc                    sF   |du r|du rdS t ||}|du rdS | jj|dI dH }|S )a9  
        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   rA   r   Zasync_get_cache)r   r2   r<   rE   Zcache_resultr   r   r   async_get_model_id   s   z%PromptCachingCache.async_get_model_idc                 C   s8   |d u r
|d u r
d S t ||}|d u rd S | j|S r   )r   rA   r   Z	get_cache)r   r2   r<   rE   r   r   r   get_model_id   s   zPromptCachingCache.get_model_id)r   r   r   r	   r   staticmethodr   r&   r   r   r;   r   r   r   rA   rF   rG   r   rI   rJ   r   r   r   r   r      s\    S

#










r   )__doc__r=   r,   typingr   r   r   r   r   r   Ztyping_extensionsr   Zlitellm.caching.cachingr	   Zlitellm.caching.in_memory_cacher
   Zlitellm.types.llms.openair   r   Zopentelemetry.tracer   _SpanZlitellm.routerr   Zlitellm_routerr   r   r   r   r   r   <module>   s"     