Ë
    tPêj“  ã                   óÈ   — U d dl 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 d d dœZeeeef      ed	<    G d
„ de«      Z G d„ de«      Z G d„ de«      Zy)é    )ÚDictÚFinalÚOptionalÚUnionN)Úverbose_proxy_logger)Ú	DualCache)ÚCustomGuardrail)ÚUserAPIKeyAuth)ÚCallTypesLiteral)ÚAÚBÚguardrail_lb_call_countc                   ó>   — e Zd ZdZdededededee	e
eef      f
d„Zy)	ÚGuardrailForLBTestingAz'Guardrail A for load balancing testing.Úuser_api_key_dictÚcacheÚdataÚ	call_typeÚreturnc              ƒ   ój   K  — t         dxx   dz  cc<   t        j                  dt         d   «       |S ­w)Nr   é   z0GuardrailForLBTestingA called. Total A calls: %s©r   r   Úinfo©Úselfr   r   r   r   s        úw/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/proxy/example_config_yaml/custom_guardrail.pyÚasync_pre_call_hookz*GuardrailForLBTestingA.async_pre_call_hook   ó5   è ø€ ô 	  Ó$¨Ñ)Ó$Ü×!Ñ!Ð"TÔVmÐnqÑVrÔsØˆùó   ‚13N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   Údictr   r   r   Ú	ExceptionÚstrr   © ó    r   r   r      óK   „ Ù1ð	à)ð	ð ð	ð ð		ð
 $ð	ð 
�%˜	 3¨Ð,Ñ-Ñ	.ô	r)   r   c                   ó>   — e Zd ZdZdededededee	e
eef      f
d„Zy)	ÚGuardrailForLBTestingBz'Guardrail B for load balancing testing.r   r   r   r   r   c              ƒ   ój   K  — t         dxx   dz  cc<   t        j                  dt         d   «       |S ­w)Nr   r   z0GuardrailForLBTestingB called. Total B calls: %sr   r   s        r   r   z*GuardrailForLBTestingB.async_pre_call_hook    r   r   Nr    r(   r)   r   r,   r,      r*   r)   r,   c                   óp   ‡ — e Zd Zˆ fd„Zdededededee	e
eef      f
d„Zdededefd„Zdedefd	„Zˆ xZS )
ÚmyCustomGuardrailc                 ó2   •— || _         t        ‰| �  di |¤Ž y )Nr(   )Úoptional_paramsÚsuperÚ__init__)r   ÚkwargsÚ	__class__s     €r   r3   zmyCustomGuardrail.__init__-   s   ø€ ð
  &ˆÔä‰ÑÑ"˜6Ó"r)   r   r   r   r   r   c              ƒ   ó  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Ú
isinstancer'   ÚlowerÚreplacer   Údebug)r   r   r   r   r   Ú	_messagesÚmessageÚ_contents           r   r   z%myCustomGuardrail.async_pre_call_hook6   s„   è ø€ ð  Ÿ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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
        r7   r8   r9   z+Guardrail failed words - `litellm` detectedN)r:   r;   r'   r<   Ú
ValueError)r   r   r   r   r?   r@   rA   s          r   Úasync_moderation_hookz'myCustomGuardrail.async_moderation_hookQ   sb   è ø€ ð  Ÿ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;   r9   ÚModelResponseÚchoicesÚChoicesr@   r8   r'   rC   )r   r   r   ÚresponseÚchoices        r   Úasync_post_call_success_hookz.myCustomGuardrail.async_post_call_success_hookh   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)r!   r"   r#   r3   r
   r   r%   r   r   r   r&   r'   r   rD   rL   Ú__classcell__)r5   s   @r   r/   r/   ,   s�   ø„ ô#ðà)ðð ðð ð	ð
 $ðð 
�%˜	 3¨Ð,Ñ-Ñ	.óð6XàðXð *ðXð $ó	Xð.MàðMð *÷Mr)   r/   )Útypingr   r   r   r   r9   Úlitellm._loggingr   Úlitellm.caching.cachingr   Ú%litellm.integrations.custom_guardrailr	   Úlitellm.proxy._typesr
   Úlitellm.types.utilsr   r   r'   ÚintÚ__annotations__r   r,   r/   r(   r)   r   ú<module>rV      sd   ðß /Ô /ã Ý 1Ý -Ý AÝ /Ý 0ð 89¸qÑ1AÐ ˜˜t C¨ H™~Ñ.Ó Aô˜_ô ô˜_ô ôSM˜õ SMr)   