
    h                         d 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mZ ddlmZmZ ddlmZ  G d	 d
e      Zy)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)DictListOptionalTupleUnioncast)CustomLogger)CustomPromptManagement)CacheControlInjectionPoint!CacheControlMessageInjectionPoint)AllMessageValuesChatCompletionCachedContent)StandardCallbackDynamicParamsc                      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ee   ef   fdZededee   d
ee   fd       Zededed
efd       Zed
efd       Zeded
efd       Zeded
ee   fd       Zy)AnthropicCacheControlHookNmodelmessagesnon_default_params	prompt_idprompt_variablesdynamic_callback_paramsprompt_labelprompt_versionreturnc	                     |j                  dg       }	|	s|||fS t        j                  |      }
|	D ]:  }|j                  d      dk(  st	        t
        |      }| j                  ||
      }
< ||
|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   injection_pointsprocessed_messagesr   s               m/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/integrations/anthropic_cache_control_hook.pyget_chat_completion_promptz4AnthropicCacheControlHook.get_chat_completion_prompt   s    ( >P=S=S,b>
  ($666 "]]84 &Eyy$	1>F%)%D%D*< &E &" & (*<<<    r   c                    | j                  dd      xs t        d      }| j                  dd      }d}t        |t              r|j	                         rt        |      }n|}| j                  dd      }|6d|cxk  rt        |      k  r n |S t        j                  ||   |      ||<   |S |3|D ].  }|j                  d      |k(  st        j                  ||      }0 |S )	z.Process message-level cache control injection.controlN	ephemeral)typeindexroler   )r   r,   )	r#   r   
isinstancestrisdigitintlenr   %_safe_insert_cache_control_in_message)r   r   r,   _targetted_indextargetted_indextargetted_rolemsgs          r(   r$   z4AnthropicCacheControlHook._process_message_injection>   s   
 05yyt0
 0;(k: 	 7<ii6N)-&,'')"%&6"7.O640 &O3c(m3  .SS 17 )  '776?n41WW$' X     r*   r   r,   c                     | j                  dd      }t        |t              r|| d<   | S t        |t              r|D ]  }t        |t              s||d<    | S )z
        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.
        contentNcache_control)r#   r1   r2   listdict)r   r,   message_contentcontent_items       r(   r6   z?AnthropicCacheControlHook._safe_insert_cache_control_in_messaged   sb     "++i6 os+'.GO$ 	 . /lD14;L1 !0 r*   c                      y)z*Return the integration name for this hook.anthropic_cache_control_hook )r%   s    r(   integration_namez*AnthropicCacheControlHook.integration_name}   s     .r*   c                 *    | j                  dd       ryy)Nr   TF)r#   )r   s    r(   'should_use_anthropic_cache_control_hookzAAnthropicCacheControlHook.should_use_anthropic_cache_control_hook   s    !!"BDIr*   c                 P    ddl m} t        j                  |       r |dd d       S y )Nr   )$_init_custom_logger_compatible_classrC   )logging_integrationinternal_usage_cache
llm_router)*litellm.litellm_core_utils.litellm_loggingrI   r   rG   )r   rI   s     r(   2get_custom_logger_for_anthropic_cache_control_hookzLAnthropicCacheControlHook.get_custom_logger_for_anthropic_cache_control_hook   s7    	
 %LL
 8$B%) 
 r*   )NN)__name__
__module____qualname__r2   r   r   r?   r   r   r4   r   r)   staticmethodr   r$   r   r6   propertyrE   r   boolrG   r	   rN   rD   r*   r(   r   r      s[    '+(,%=%= '(%= !	%=
 C=%= #4.%= "?%= sm%= !%= 
sD)*D0	1%=N #0#<@AQ<R#		# #J !,G	 0 .# . . D T  
  	,	 r*   r   )__doc__r!   typingr   r   r   r   r   r   "litellm.integrations.custom_loggerr	   -litellm.integrations.custom_prompt_managementr
   7litellm.types.integrations.anthropic_cache_control_hookr   r   litellm.types.llms.openair   r   litellm.types.utilsr   r   rD   r*   r(   <module>r\      s8     ; ; ; P T =B 6 Br*   