
    ^h6                         S SK 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
Jr  S SKJr  SrSr0 4S	\S
\S\S\S\4
S jjrS\S\\   4S jrS\S\\   4S jrS\S\\\4   4S jrS\S\\   4S jrS\S\\   4S jrg)    )AnyDictListOptionalN)
get_secret)verbose_proxy_logger)CommonProxyErrorsLiteLLMPromptInjectionParams)get_instance_fnz[94mz[0mvaluepremium_userconfig_file_pathlitellm_settingscallback_specific_paramsc                    SSK JnJn  [        R                  " [
         SU  S[         35        [        U [        5      (       Ga  / nU  GH.  n[        U[        5      (       a'  U[        R                  ;   a  UR                  U5        M@  [        U[        5      (       a  US:X  a  SSKJn	  SSKJn
  0 n[        U["        5      (       a  SU;   a  US   nU	" S<0 UD6nS[        R$                  ;  a  [        R$                  R                  S5        UR                  U5        ['        U
SU5        M  [        U[        5      (       as  US	:X  am  SS
KJn  UR-                  SS 5      nUb  [/        U5      n0 nS	U;   a  [        US	   ["        5      (       a  US	   nSU0UEnU" S<0 UD6nUR                  U5        GMl  [        U[        5      (       aR  US:X  aL  SSKJn  USLa&  [5        S[6        R8                  R:                  -   5      eU" 5       nUR                  U5        GM  [        U[        5      (       aR  US:X  aL  SSKJn  USLa&  [5        S[6        R8                  R:                  -   5      eU" 5       nUR                  U5        GM:  [        U[        5      (       aR  US:X  aL  SSK J!n  USLa&  [5        S[6        R8                  R:                  -   5      eU" 5       nUR                  U5        GM  [        U[        5      (       a5  US:X  a/  SSK"J#n  0 nSU;   a  US   nU" S<0 UD6nUR                  U5        GM  [        U[        5      (       a'  US:X  a!  SSK$J%n  U" 5       nUR                  U5        GM'  [        U[        5      (       aR  US:X  aL  SSK&J'n  USLa&  [5        S[6        R8                  R:                  -   5      eU" 5       nUR                  U5        GM  [        U[        5      (       aR  US:X  aL  SSK(J)n  USLa&  [5        S [6        R8                  R:                  -   5      eU" 5       n UR                  U 5        GM  [        U[        5      (       aQ  US!:X  aK  SS"K*J+n!  USLa&  [5        S#[6        R8                  R:                  -   5      eU!" US$9n"UR                  U"5        GM[  [        U[        5      (       aR  US%:X  aL  SS&K,J-n#  USLa&  [5        S'[6        R8                  R:                  -   5      eU#" 5       n$UR                  U$5        GM  [        U[        5      (       a>  US(:X  a8  SS)K.J/n%  S n&S*U;   a  US*   n'[a        S<0 U'D6n&U%" U&S+9n(UR                  U(5        GM  [        U[        5      (       a'  US,:X  a!  SS-K1J2n)  U)" 5       n*UR                  U*5        GMQ  [        U[        5      (       a  US.:X  a  SS/K3J4n+  US0   n,U,Rk                  5        HH  u  n-n.U.c  M  [        U.[        5      (       d  M"  U.Rm                  S15      (       d  M:  [o        U.5      U,U-'   MJ     U+" S<0 U,D6n/UR                  U/5        GM  [        R                  " [
         S2U S3[         35        UR                  [q        UUS495        GM1     [        [        Rr                  [        5      (       a   [        Rr                  Ru                  U5        OU[        l9        S5U ;   aq  USLa1  [        Rv                  " S6[6        R8                  R:                   35        SS7K J<n0  [        R                  " S85        SS9K=J>n1  U1" 5       n2U0R                  S:U25        O[q        U US49/[        l9        [        R                  " [
         S;[        Rr                   S3[         35        g )=Nr   )callback_settingsprisma_clientzinitializing callbacks=z	 on proxyotel)OpenTelemetry)proxy_serveropen_telemetry_loggerpresidio)_OPTIONAL_PresidioPIIMaskingpresidio_logging_onlylogging_onlyllamaguard_moderations)_ENTERPRISE_LlamaGuardTzTrying to use Llama Guardhide_secrets)_ENTERPRISE_SecretDetectionzTrying to use secret hidingopenai_moderations)_ENTERPRISE_OpenAI_Moderationz&Trying to use OpenAI Moderations Checklakera_prompt_injection)lakeraAI_Moderationaporia_prompt_injection)AporiaGuardrailgoogle_text_moderation) _ENTERPRISE_GoogleTextModerationz$Trying to use Google Text Moderationllmguard_moderations)_ENTERPRISE_LLMGuardzTrying to use Llm Guardblocked_user_check)_ENTERPRISE_BlockedUserListz$Trying to use ENTERPRISE BlockedUser)r   banned_keywords)_ENTERPRISE_BannedKeywordsz&Trying to use ENTERPRISE BannedKeyworddetect_prompt_injection)"_OPTIONAL_PromptInjectionDetectionprompt_injection_params)r0   batch_redis_requests)_PROXY_BatchRedisRequestsazure_content_safety)_PROXY_AzureContentSafetyazure_content_safety_paramszos.environ/z% attempting to import custom calback= )r   r   
prometheusz9Prometheus metrics are only available for premium users. )appz'Starting Prometheus Metrics on /metrics)make_asgi_appz/metricsz Initialized Callbacks -  )@litellm.proxy.proxy_serverr   r   r   debugblue_color_codereset_color_code
isinstanceliststrlitellm)_known_custom_logger_compatible_callbacksappend"litellm.integrations.opentelemetryr   litellm.proxyr   dictservice_callbacksetattr1litellm.proxy.guardrails.guardrail_hooks.presidior   getbool'enterprise.enterprise_hooks.llama_guardr   	Exceptionr	   not_premium_userr   ,enterprise.enterprise_hooks.secret_detectionr   -enterprise.enterprise_hooks.openai_moderationr!   2litellm.proxy.guardrails.guardrail_hooks.lakera_air#   2litellm.proxy.guardrails.guardrail_hooks.aporia_air%   2enterprise.enterprise_hooks.google_text_moderationr'   %enterprise.enterprise_hooks.llm_guardr)   -enterprise.enterprise_hooks.blocked_user_listr+   +enterprise.enterprise_hooks.banned_keywordsr-   .litellm.proxy.hooks.prompt_injection_detectionr/   r
   #litellm.proxy.hooks.batch_redis_getr2   (litellm.proxy.hooks.azure_content_safetyr4   items
startswithr   r   	callbacksextendwarningr8   prometheus_clientr9   mount)3r   r   r   r   r   r   r   imported_listcallbackr   r   _otel_settingsr   r   r   _presidio_paramsparamspii_masking_objectr   llama_guard_objectr   _secret_detection_objectr!   openai_moderations_objectr#   init_paramslakera_moderations_objectr%   aporia_guardrail_objectr'   google_text_moderation_objr)   llm_guard_moderation_objr+   blocked_user_listr-   banned_keywords_objr/   r0   !prompt_injection_params_in_configprompt_injection_detection_objr2   batch_redis_objr4   r5   kvazure_content_safety_objr8   r9   metrics_apps3                                                      c/home/james-whalen/.local/lib/python3.13/site-packages/litellm/proxy/common_utils/callback_utils.pyinitialize_callbacks_on_proxyrz      s^    L
25'CSBTU %#%H8S)) Q QQ$$X.Hc**x6/AL6!#/666EV;V%6v%>N(5(G(G% !9!99,,33F;$$%:;&=?TUHc**x:/E 9I8L8L+T9% )4,0--) $& !99j,Z8$? ? (@
'K$ #$9*&* &B%KF%K"$$%78Hc**x;S/S  t+#3+<<BBC 
 &<%="$$%78Hc**x>/I  t+#5+<<BBC 
 ,G+H($$%=>Hc**x;O/O  t+#@+<<BBC 
 -J,K)$$%>?Hc**x;T/T !,0HH":;T"UK,?,N+,N)$$%>?Hc**x;T/T +:*;'$$%<=Hc**x;S/S  t+#>+<<BBC 
 .N-O*$$%?@Hc**x;Q/QVt+#1+<<BBC 
 ,@+A($$%=>Hc**x;O/O  t+#>+<<BBC 
 %@"/%! $$%67Hc**x;L/L  t+#@+<<BBC 
 'A&B#$$%89Hc**x;T/T +/',0@@8H195 /K /;/+ 2T,C2. $$%CDHc**x;Q/Q #<"=$$_5Hc**x;Q/Q /?1/+ 8==?DAq&q#..LL779CA3A6 @ ,E ,1,( $$%=>$**&''LXJVWXhWij $$#&)9Y d g''..$$]3 -G5 4'$,,OPaPrPrPxPxOyz 7 &&'PQ7 (/KIIj+. !1
 
4W5F5F4GqIYHZ[    kwargsreturnc                     U R                  SS 5      =(       d    0 nUR                  SS 5      =(       d    0 nUR                  SS 5      nUb  U$ g )Nlitellm_paramsmetadatamodel_grouprK   )r|   _litellm_params	_metadata_model_groups       ry   #get_model_group_from_litellm_kwargsr     sN    jj!148>BO##J5;I==5Lr{   datac                 h    U R                  SS 5      =(       d    0 nUR                  SS 5      nUb  U$ g )Nr   r   r   )r   r   r   s      ry   !get_model_group_from_request_datar     s6    T*0bI==5Lr{   c                     0 nU R                  SS5      =(       d    0 n[        U 5      nSU 3nUR                  US5      nU(       a  XQSU 3'   SU 3nUR                  US5      nU(       a  XqSU 3'   U$ )z
Helper function to return x-litellm-key-remaining-tokens-{model_group} and x-litellm-key-remaining-requests-{model_group}

Returns {} when api_key + model rpm/tpm limit is not set

r   Nzlitellm-key-remaining-requests-z!x-litellm-key-remaining-requests-zlitellm-key-remaining-tokens-zx-litellm-key-remaining-tokens-)rK   r   )r   headersr   r    remaining_requests_variable_nameremaining_requestsremaining_tokens_variable_nameremaining_tokenss           ry   3get_remaining_tokens_and_requests_from_request_datar     s     GT*0bI3D9K *I'V$"'GNEW3K=AB (E[M%R" }}%CTJCS1+?@Nr{   request_datac                     U R                  SS 5      =(       d    0 n0 nSU;   a  SR                  US   5      US'   SU;   a  [        US   5      US'   U$ )Nr   applied_guardrails,zx-litellm-applied-guardrailszsemantic-similarityzx-litellm-semantic-similarity)rK   joinrA   )r   r   r   s      ry   get_logging_caching_headersr   8  sh      T28bIGy(25((*+3
./ 	)36yAV7W3X/0Nr{   guardrail_namec                     Uc  g U R                  SS 5      =(       d    0 nSU;   a  US   R                  U5        g U/US'   g )Nr   r   )rK   rD   )r   r   r   s      ry   *add_guardrail_to_applied_guardrails_headerr   F  sN       T28bIy(&'..~>+9*:	&'r{   )typingr   r   r   r   rB   r   litellm._loggingr   litellm.proxy._typesr	   r
   litellm.proxy.utilsr   r=   r>   rL   rA   rG   rz   r   r   r   r   r   r:   r{   ry   <module>r      s    , ,   1 P /  &({{{ { 	{
 #{| # D Xc] d tCQTH~ 4d x~ 	;	;(0	;r{   