
    g                     ~    d dl mZmZmZmZmZmZ d dl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  G d d	e      Zy)
    )AnyDictListLiteralOptionalUnionN)verbose_proxy_logger)	DualCache)CustomGuardrail)UserAPIKeyAuth) should_proceed_based_on_metadatac                   |     e Zd Z fdZdedededed   dee	e
eef      f
dZdededed	   fd
ZdedefdZ xZS )myCustomGuardrailc                 2    || _         t        |   di | y )N )optional_paramssuper__init__)selfkwargs	__class__s     g/var/www/openai/venv/lib/python3.12/site-packages/litellm/proxy/example_config_yaml/custom_guardrail.pyr   z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                   K   |j                  d      }|rS|D ]N  }|j                  d      }t        |t              s%d|j                         v s8|j	                  dd      }||d<   P t        j                  d|       |S w)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_hookz%myCustomGuardrail.async_pre_call_hook   s     . HHZ(	$";;y1h, HNN$44#+#3#3Iz#J-5	* % 	"";Y	
 s   :BB1B)r   r    r!   r"   r#   c                    K   |j                  d      }|rG|D ]A  }|j                  d      }t        |t              s%d|j                         v s8t	        d       yyw)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-   r.   
ValueError)r   r   r   r   r1   r2   r3   s          r   async_moderation_hookz'myCustomGuardrail.async_moderation_hook;   s[     * HHZ(	$";;y1h, HNN$44()VWW	 % s   :AAAc                   K   t        j                  d|       t        |t        j                        r|j
                  D ]  }t        |t        j                        st        j                  d|       |j                  j                  sKt        |j                  j                  t              spd|j                  j                  v st        d       yyw)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	   r0   r,   r*   ModelResponsechoicesChoicesr2   r)   r-   r6   )r   r   r   responsechoices        r   async_post_call_success_hookz.myCustomGuardrail.async_post_call_success_hookX   s      	""#ExPh 5 56"**fgoo6(../OQWX..&v~~'='=sC$(>(>>()KLL + 7s   AC,C
$C/CC)__name__
__module____qualname__r   r   r
   dictr   r   r   	Exceptionr-   r4   r7   r?   __classcell__)r   s   @r   r   r      s    #$)$ $ 	$
 	
$ 
%	3,-	.$LXX *X #
	X: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>rL      s-    < <  1 - A / WdM dMr   