
    gh                         d Z ddlZddlZddl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mZ erddlmZ ddlmZ eZe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	TypedDict)	DualCache)InMemoryCache)AllMessageValuesChatCompletionToolParam)Span)Routerc                       e Zd ZU eed<   y)PromptCachingCacheValuemodel_idN)__name__
__module____qualname__str__annotations__     ^/var/www/openai/venv/lib/python3.12/site-packages/litellm/router_utils/prompt_caching_cache.pyr   r      s    Mr   r   c                   2   e Zd ZdefdZeded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   r0   z#PromptCachingCache.serialize_object"   s     388:T"::t
  T"JMN#$&77=#NNc5$/0J3x Os   B messagestoolsc                    | |y i }| t         j                  |       }||d<   |t         j                  |      }||d<   t        j                  |dd      }t	        j
                  |j                               j                         }d| dS )Nr5   r6   Tr%   r(   zdeployment:z:prompt_caching)r   r0   r-   r.   hashlibsha256encode	hexdigest)r5   r6   data_to_hashserialized_messagesserialized_toolsdata_to_hash_strhashed_datas          r   get_prompt_caching_cache_keyz/PromptCachingCache.get_prompt_caching_cache_key5   s    
 "4"E"Eh"O':L$1BB5I$4L!  ::!
 nn%5%<%<%>?IIK[M99r   r   Nc                     ||y t         j                  ||      }| j                  j                  |t	        |      d       y N)r   i,  )ttl)r   rA   r   	set_cacher   r   r   r5   r6   	cache_keys        r   add_model_idzPromptCachingCache.add_model_idP   sL     &CCHeT	

.As 	 	
 r   c                    K   ||y t         j                  ||      }| j                  j                  |t	        |      d       d {    y 7 wrC   )r   rA   r   async_set_cacher   rF   s        r   async_add_model_idz%PromptCachingCache.async_add_model_id_   s`      &CCHeT	jj((#X6 ) 
 	
 	

 	
s   AA	A
Ac           	      z  K   ||yg }|xt         j                  ||      }|j                  |       t        dt	        dt        |                  D ]/  }|d|  }t         j                  ||      }|j                  |       1 | j                  j                  |       d{   }|y|D ]  }	|	|	c S  y7 w)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   rA   appendrangeminlenr   async_batch_get_cache)
r   r5   r6   potential_cache_keysfull_cache_keyipartial_messagespartial_cache_keycache_resultsresults
             r   async_get_model_idz%PromptCachingCache.async_get_model_idp   s        "/LL%N !''7 1c!S]34#+CaR= $6$S$S$e%! %++,=> 5 #jj>>% ? 
 
   $F! $ 
s   B!B;#B9$B;3B;c                 n    ||y t         j                  ||      }| j                  j                  |      S r   )r   rA   r   	get_cache)r   r5   r6   rG   s       r   get_model_idzPromptCachingCache.get_model_id   s8    
 &CCHeT	zz##I..r   )r   r   r   r   r    staticmethodr   r0   r   r   r
   r   r   rA   rH   rK   r   r\   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__r8   r-   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>rj      sV      @ @ - 9 O1%NDDNi N/ N/r   