o
    ưi'                     @   s   d Z ddlZddlmZmZ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 ddlmZmZ dd	lmZmZ dd
lmZ ddlmZ erWddlmZ neZG dd deZ dS )a  
This hook is used to inject cache control directives into the messages of a chat completion.

Users can define
- `cache_control_injection_points` in the completion params and litellm will inject the cache control directives into the messages at the specified injection points.

    N)TYPE_CHECKINGAnyDictListOptionalTupleUnioncast)verbose_logger)CustomLogger)CustomPromptManagementPromptManagementClient)CacheControlInjectionPoint!CacheControlMessageInjectionPoint)AllMessageValuesChatCompletionCachedContent)
PromptSpec)StandardCallbackDynamicParams)Loggingc                    @   s(  e Zd Z					d(dedee dedee dee ded	ee	 d
ee dee
 dee dee deeee ef fddZededee dee fddZedededefddZedefddZdee d	ee	 dedefddZ		d)dee d	ee	 dee ded
ee dee
 defddZ			d*dee dee ded	ee	 d
ee dee
 defddZ						d+dedee dedee dee ded ed	ee	 d!eee  d
ee dee
 dee dee deeee ef fd"d#Zededefd$d%Zededee fd&d'ZdS ),AnthropicCacheControlHookNFmodelmessagesnon_default_params	prompt_idprompt_variablesdynamic_callback_paramsprompt_specprompt_labelprompt_versionignore_prompt_manager_model%ignore_prompt_manager_optional_paramsreturnc                 C   s^   | dg }|s|||fS t|}|D ]}|ddkr)tt|}| j||d}q|||fS )a,  
        Apply cache control directives based on specified injection points.

        Returns:
        - model: str - the model to use
        - messages: List[AllMessageValues] - messages with applied cache controls
        - non_default_params: dict - params with any global cache controls
        cache_control_injection_pointslocationmessage)pointr   )popcopydeepcopygetr	   r   _process_message_injection)selfr   r   r   r   r   r   r   r   r   r    r!   Zinjection_pointsZprocessed_messagesr&    r-   h/home/app/Keep/.python/lib/python3.10/site-packages/litellm/integrations/anthropic_cache_control_hook.pyget_chat_completion_prompt   s   



z4AnthropicCacheControlHook.get_chat_completion_promptr&   c              	   C   s   |  ddp
tdd}|  dd}d}t|tr!| r t|}n|}|  dd}|durf|}|dk r9|t|7 }d|  krEt|k rSn nt|| |||< |S t	
d| d	t| d
| d |S |dur}|D ]}| d|kr|tj||d}ql|S )z.Process message-level cache control injection.controlNZ	ephemeral)typeindexZroler   z*AnthropicCacheControlHook: Provided index z- is out of bounds for message list of length z. Targeted index was z2. Skipping cache control injection for this point.)r%   r0   )r*   r   
isinstancestrisdigitintlenr   %_safe_insert_cache_control_in_messager
   warning)r&   r   r0   Z_targetted_indexZtargetted_indexZtargetted_roleZoriginal_indexmsgr-   r-   r.   r+   I   sL   
z4AnthropicCacheControlHook._process_message_injectionr%   r0   c                 C   sV   |  dd}t|tr|| d< | S t|tr)t|dkr)t|d tr)||d d< | S )ap  
        Safe way to insert cache control in a message

        OpenAI Message content can be either:
            - string
            - list of objects

        This method handles inserting cache control in both cases.
        Per Anthropic's API specification, when using multiple content blocks,
        only the last content block can have cache_control.
        contentNZcache_controlr   )r*   r3   r4   listr7   dict)r%   r0   Zmessage_contentr-   r-   r.   r8   y   s   

z?AnthropicCacheControlHook._safe_insert_cache_control_in_messagec                 C      dS )z*Return the integration name for this hook.anthropic_cache_control_hookr-   )r,   r-   r-   r.   integration_name   s   z*AnthropicCacheControlHook.integration_namec                 C   r?   )zFAlways return False since this is not a true prompt management system.Fr-   )r,   r   r   r   r-   r-   r.   should_run_prompt_management   s   z6AnthropicCacheControlHook.should_run_prompt_managementc                 C   s   t |g ddddS )CNot used - this hook only modifies messages, doesn't fetch prompts.N)r   Zprompt_templateZprompt_template_modelZprompt_template_optional_paramsZcompleted_messagesr   )r,   r   r   r   r   r   r   r-   r-   r.   _compile_prompt_helper   s   
z0AnthropicCacheControlHook._compile_prompt_helperc                    s   | j ||||||dS )rC   )r   r   r   r   r   r   )rD   )r,   r   r   r   r   r   r   r-   r-   r.   async_compile_prompt_helper   s   
z5AnthropicCacheControlHook.async_compile_prompt_helperlitellm_logging_objtoolsc                    s"   | j ||||||||
|||dS )zCAsync version - delegates to sync since no async operations needed.)r   r   r   r   r   r   r   r   r   r    r!   )r/   )r,   r   r   r   r   r   r   rF   r   rG   r   r   r    r!   r-   r-   r.    async_get_chat_completion_prompt   s   z:AnthropicCacheControlHook.async_get_chat_completion_promptc                 C   s   |  dd rdS dS )Nr#   TF)r*   )r   r-   r-   r.   'should_use_anthropic_cache_control_hook   s   zAAnthropicCacheControlHook.should_use_anthropic_cache_control_hookc                 C   s(   ddl m} t| r|dd d dS d S )Nr   )$_init_custom_logger_compatible_classr@   )Zlogging_integrationZinternal_usage_cacheZ
llm_router)*litellm.litellm_core_utils.litellm_loggingrJ   r   rI   )r   rJ   r-   r-   r.   2get_custom_logger_for_anthropic_cache_control_hook   s   zLAnthropicCacheControlHook.get_custom_logger_for_anthropic_cache_control_hook)NNNFF)NN)NNN)NNNNFF)__name__
__module____qualname__r4   r   r   r>   r   r   r   r6   boolr   r/   staticmethodr   r+   r   r8   propertyrA   rB   r   rD   rE   LiteLLMLoggingObjr   rH   rI   r   rL   r-   r-   r-   r.   r      s   		

*/


	


r   )!__doc__r(   typingr   r   r   r   r   r   r   r	   Zlitellm._loggingr
   Z"litellm.integrations.custom_loggerr   Z-litellm.integrations.custom_prompt_managementr   Z+litellm.integrations.prompt_management_baser   Z7litellm.types.integrations.anthropic_cache_control_hookr   r   Zlitellm.types.llms.openair   r   Z"litellm.types.prompts.init_promptsr   Zlitellm.types.utilsr   rK   r   rS   r   r-   r-   r-   r.   <module>   s    (