Ë
    tPêj]  ã                   óf   — d dl mZmZ d dlZd dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZ  G d„ de	«      Zy)	é    )ÚFinalÚLiteralN)Úverbose_proxy_logger)Ú	DualCache)ÚCustomGuardrailÚlog_guardrail_information)ÚUserAPIKeyAuthc                   óš   ‡ — e Zd Zˆ fd„Zededededed   de	e
z  ez  dz  f
d	„«       Zedededed
   fd„«       Ze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     €ú~/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/proxy/guardrails/guardrail_hooks/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Úmcp_callÚanthropic_messagesÚreturnNc              ƒ   ó  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„   è ø€ ð4  Ÿ8™8 JÓ/ˆ	ÙØ$ò 6�Ø"Ÿ;™; yÓ1�Ü˜h¬Õ,Ø  H§N¡NÓ$4Ò4Ø#+×#3Ñ#3°I¸zÓ#J˜Ø-5˜ 	Ò*ð6ô 	×"Ñ"Ð#RÐT]Ô^àˆùs   ‚:B½BÁ1B)r   r   r   r   r   Ú	responsesr"   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   r/   r0   r1   s          r   Úasync_moderation_hookz'myCustomGuardrail.async_moderation_hook>   sb   è ø€ ð2  Ÿ8™8 JÓ/ˆ	ÙØ$ò X�Ø"Ÿ;™; yÓ1�Ü˜h¬Õ,Ø  H§N¡NÓ$4Ò4Ü(Ð)VÓWÐWñ	Xð ù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   r.   r*   r(   ÚModelResponseÚchoicesÚChoicesr0   r'   r+   r5   )r   r   r   ÚresponseÚchoices        r   Úasync_post_call_success_hookz.myCustomGuardrail.async_post_call_success_hook_   s¤   è ø€ ô 	×"Ñ"Ð#EÀxÔPÜ�h¤× 5Ñ 5Ô6Ø"×*Ñ*ò M�Ü˜f¤g§o¡oÕ6Ü(×.Ñ.Ð/OÐQWÔXàŸ™×.Ó.Ü& v§~¡~×'=Ñ'=¼sÕCØ$¨¯©×(>Ñ(>Ò>ä(Ð)KÓLÐLñMð 7ùs   ‚ACÁ,CÂ
$CÂ/CÃC)Ú__name__Ú
__module__Ú__qualname__r   r   r	   r   Údictr   Ú	Exceptionr+   r2   r6   r>   Ú__classcell__)r   s   @r   r   r      sÏ   ø„ ô#ð ð$à)ð$ð ð$ð ð	$ð
 ð	"ñ
ð$ð" 
�S‰˜4Ñ	 $Ñ	&ò#$ó ð$ðL ðXàðXð *ðXð ð"ñ	
ò	Xó ðXð@ ðMàðMð *òMó ôMr   r   )Útypingr   r   r(   Úlitellm._loggingr   Úlitellm.caching.cachingr   Ú%litellm.integrations.custom_guardrailr   r   Úlitellm.proxy._typesr	   r   r   r   r   ú<module>rJ      s+   ðß !ã Ý 1Ý -÷õ 0ôjM˜õ jMr   