
    ^h                     ~    S SK JrJrJrJrJrJr  S SKrS SKJ	r	  S SK
Jr  S SKJr  S SKJr  S SKJr   " S S	\5      rg)
    )AnyDictListLiteralOptionalUnionN)verbose_proxy_logger)	DualCache)CustomGuardrail)UserAPIKeyAuth) should_proceed_based_on_metadatac                      ^  \ rS rSrU 4S jrS\S\S\S\S   S\	\
\\\4      4
S	 jrS\S\S\S
   4S jrS\S\4S jrSrU =r$ )myCustomGuardrail   c                 2   > Xl         [        TU ]  " S0 UD6  g )N )optional_paramssuper__init__)selfkwargs	__class__s     l/home/james-whalen/.local/lib/python3.13/site-packages/litellm/proxy/example_config_yaml/custom_guardrail.pyr   myCustomGuardrail.__init__   s    
  &"6"    user_api_key_dictcachedata	call_type)
completiontext_completion
embeddingsimage_generation
moderationaudio_transcriptionpass_through_endpointrerankreturnc                 &  #    UR                  S5      nU(       a]  U HW  nUR                  S5      n[        U[        5      (       d  M+  SUR                  5       ;   d  MA  UR	                  SS5      nXvS'   MY     [
        R                  " SU5        U$ 7f)zZ
Runs before the LLM API call
Runs on only Input
Use this if you want to MODIFY the input
messagescontentlitellmz********z-async_pre_call_hook: Message after masking %s)get
isinstancestrlowerreplacer	   debug)r   r   r   r   r   	_messagesmessage_contents           r   async_pre_call_hook%myCustomGuardrail.async_pre_call_hook   s     . HHZ(	$";;y1h,, HNN$44#+#3#3Iz#J-5	* % 	"";Y	
 s   ABB3B)r    r"   r#   r$   r%   c                    #    UR                  S5      nU(       aQ  U HJ  nUR                  S5      n[        U[        5      (       d  M+  SUR                  5       ;   d  MA  [	        S5      e   gg7f)z
Runs in parallel to LLM API call
Runs on only Input

This can NOT modify the input, only used to reject or accept a call before going to LLM API
r*   r+   r,   z+Guardrail failed words - `litellm` detectedN)r-   r.   r/   r0   
ValueError)r   r   r   r   r3   r4   r5   s          r   async_moderation_hook'myCustomGuardrail.async_moderation_hook;   s]     * HHZ(	$";;y1h,, HNN$44()VWW	 % s   AA-A-A-c                   #    [         R                  " SU5        [        U[        R                  5      (       a  UR
                   H  n[        U[        R                  5      (       d  M$  [         R                  " SU5        UR                  R                  (       d  MX  [        UR                  R                  [        5      (       d  M  SUR                  R                  ;   d  M  [        S5      e   gg7f)z
Runs on response from LLM API call

It can be used to reject a response

If a response contains the word "coffee" -> we will raise an exception
z async_pre_call_hook response: %szasync_pre_call_hook choice: %scoffeez Guardrail failed Coffee DetectedN)r	   r2   r.   r,   ModelResponsechoicesChoicesr4   r+   r/   r9   )r   r   r   responsechoices        r   async_post_call_success_hook.myCustomGuardrail.async_post_call_success_hookX   s      	""#ExPh 5 566"**fgoo66(../OQWX...&v~~'='=sCC$(>(>>()KLL + 7s   A#C3)0C3'C3C3$C3)r   )__name__
__module____qualname____firstlineno__r   r   r
   dictr   r   r   	Exceptionr/   r6   r:   rC   __static_attributes____classcell__)r   s   @r   r   r      s    #$)$ $ 	$
 	
$ 
%	3,-	.$LXX *X #
	X:MM *M Mr   r   )typingr   r   r   r   r   r   r,   litellm._loggingr	   litellm.caching.cachingr
   %litellm.integrations.custom_guardrailr   litellm.proxy._typesr   *litellm.proxy.guardrails.guardrail_helpersr   r   r   r   r   <module>rS      s-    < <  1 - A / WdM dMr   