
    ^hw(                         S SK Jr  S SKJrJrJr  S SK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Jr  S S
KJr  S SKJr   " S S\5      rg)    )SequenceMatcher)ListLiteralOptional)HTTPExceptionN)verbose_proxy_logger)	DualCache)CustomLogger)%prompt_injection_detection_default_pt)LiteLLMPromptInjectionParamsUserAPIKeyAuth)Router)get_formatted_promptc            	           \ rS rSr SS\\   4S jjrSS\S   4S jjrSS\\	   4S	 jjr
S
\\   4S jr SS\S\S
\4S jjrS\S\S\S\4S jrS\S\S\S   S
\\   4S jrSrg)"_OPTIONAL_PromptInjectionDetection   Nprompt_injection_paramsc                 T    Xl         S U l        / SQU l        / SQU l        / SQU l        g )N)Ignore	DisregardSkipForgetNeglectOverlookOmitBypasszPay no attention tozDo not followzDo not obey) priorprevious	precedingabove	foregoingearlierinitial)r   zand start overzand start anewzand begin afreshzand start from scratch)r   
llm_routerverbs
adjectivesprepositions)selfr   s     h/home/james-whalen/.local/lib/python3.13/site-packages/litellm/proxy/hooks/prompt_injection_detection.py__init__+_OPTIONAL_PromptInjectionDetection.__init__   s-     (?$,0

	

    level)INFODEBUGc                     US:X  a  [         R                  " U5        OUS:X  a  [         R                  " U5        [        R                  SL a  [        U5        g g )Nr/   r0   T)r   infodebuglitellmset_verboseprint)r)   print_statementr.   s      r*   print_verbose0_OPTIONAL_PromptInjectionDetection.print_verboseC   sH    F? %%o6g &&7$&/" 'r-   routerc                    Xl         U R                  b  U R                  R                  SL a  U R                   c  [        S5      eU R	                  SU R                   R
                   SU R                  R                   35        U R                  R                  b.  U R                  R                  U R                   R
                  ;  a  [        S5      eg g g )NTzVPromptInjectionDetection: Model List not set. Required for Prompt Injection detection.zmodel_names: z-; self.prompt_injection_params.llm_api_name: zdPromptInjectionDetection: Invalid LLM API Name. LLM API Name must be a 'model_name' in 'model_list'.)r%   r   llm_api_check	Exceptionr8   model_namesllm_api_name)r)   r:   s     r*   update_environment5_OPTIONAL_PromptInjectionDetection.update_environmentL   s      ((4,,::dB&l   ; ;<<ijn  kG  kG  kT  kT  jU  V ,,99A//<<223  z 3 C 5r-   returnc                 N   / nU R                    H  nU R                   H  nU R                   Hl  nSR                  [	        S X#U/5      5      R                  5       n[        UR                  5       5      S:  d  MM  UR                  UR                  5       5        Mn     M     M     U$ )N    )
r&   r'   r(   joinfilterstriplensplitappendlower)r)   combinationsverbadjprepphrases         r*   generate_injection_keywords>_OPTIONAL_PromptInjectionDetection.generate_injection_keywordsd   s    JJD --D XXfTDt3D&EFLLNFFLLN+a/$++FLLN; . '  r-   
user_inputsimilarity_thresholdc           
      0   UR                  5       nU R                  5       nU Hp  n[        U5      n[        [        U5      U-
  S-   5       HD  nX7Xv-    n[	        S X5      R                  5       n	X:  d  M*  U R                  SU SU	 SU 3SS9      g   Mr     g)	N   zRejected user input - z. z similar to r/   )r7   r.   TF)rL   rR   rI   ranger   ratior8   )
r)   rT   rU   user_input_lowerkeywordskeywordkeyword_lengthi	substringmatch_ratios
             r*   check_user_input_similarity>_OPTIONAL_PromptInjectionDetection.check_user_input_similarityp   s     &++-335G \N3/0>AAEF,1CD	 .dIGMMO5&&*@B{m[ghogp(q$ '    G	    r-   user_api_key_dictcachedata	call_typec                 d  #      U R                  S5         US;   d   e [        X4S9nSnU R                  bT  U R                  R                  SL a   U R                  US9nUSL a  [        S	S
S0S9eU R                  R                  SL a   OU R                  US9nUSL a  [        S	S
S0S9eU$ ! [         a    U R                  SU S35        Us $ f = f! [         a  nUR                  S	:X  aw  [        UR                  [        5      (       aX  S
UR                  ;   aH  U R                  b;  U R                  R                  (       a   UR                  R                  S
5      s S nA$ UeS nAf[         a8  n[        R                  " SR!                  [#        U5      5      5         S nAg S nAff = f7f)Nz/Inside Prompt Injection Detection Pre-Call Hook)
completiontext_completion
embeddingsimage_generation
moderationaudio_transcriptionzCall Type - zj, not in accepted list - ['completion','embeddings','image_generation','moderation','audio_transcription']re   rf   FT)rT     error4Rejected message. This is a prompt injection attack.status_codedetailz`litellm.proxy.hooks.prompt_injection_detection.py::async_pre_call_hook(): Exception occured - {})r8   r=   r   r   heuristics_checkra   r   vector_db_checkrs   
isinstancert   dictreject_as_responsegetr   	exceptionformatstr)r)   rc   rd   re   rf   formatted_promptis_prompt_attackes           r*   async_pre_call_hook6_OPTIONAL_PromptInjectionDetection.async_pre_call_hook   s    G	 PQ  %     4S$++7//@@DH'+'G'G#3 (H ($ (4/+(+ ')_$  //??4G#'#C#C/ $D $   4'# #!W  KM  """9+  .X  Y 	P  
	 $qxx..qxx'00<00CCxx||G,,G 	 **ryyF 	sv   F0C B. BC -F0.!CC F0CC 
F-BE( F-!F0&E((F-5.F(#F0(F--F0)rh   rj   rk   rl   rm   c                 d  #    U R                  SU R                   35        U R                  c  g [        XS9nSn[        U R                  S[	        5       5      nU R                  R
                  SL Ga(  U R                  R                  Gb  U R                  Gb  U R                  R                  U R                  R                  SUS.SUS./S	9I S h  vN nU R                  S
U 35        U R                  SU R                  R                   35        [        U[        R                  5      (       ai  [        UR                  S   [        R                  5      (       a=  U R                  R                  UR                  S   R                  R                   ;   a  SnUSL a  [#        SSS0S9eU$  N7f)Nz:IN ASYNC MODERATION HOOK - self.prompt_injection_params = rn   Fllm_api_system_promptTsystem)rolecontentuser)modelmessagesz"Received LLM Moderation response: zllm_api_fail_call_string: r   ro   rp   rq   rr   )r8   r   r   getattrr   r<   r?   r%   acompletionllm_api_fail_call_stringrw   r4   ModelResponsechoicesChoicesmessager   r   )r)   re   rc   rf   r~   r   prompt_injection_system_promptresponses           r*   async_moderation_hook8_OPTIONAL_PromptInjectionDetection.async_moderation_hook   s     	HIeIeHfg	
 ''//TO )0((#13*
& ((66$>,,99E+ "__8822?? !)#A $0@A 9 	 	H !CH:NO,T-I-I-b-b,cd (G$9$9::z  #W__@ @ //HHHL\L\]^L_LgLgLoLoo'+$t#S   ;	s   CF0F.CF0)r'   r%   r(   r   r&   )N)r0   )gffffff?)__name__
__module____qualname____firstlineno__r   r   r+   r   r8   r   r@   r   r}   rR   floatboolra   r   r	   rx   r   r   __static_attributes__ r-   r*   r   r      s     KO$
!)*F!G$
L#GO4L #&)9 0
T#Y 
 >A5:	0N)N N 	N
 N`@ @  *@  #
	@  
$@ r-   r   )difflibr   typingr   r   r   fastapir   r4   litellm._loggingr   litellm.caching.cachingr	   "litellm.integrations.custom_loggerr
   3litellm.litellm_core_utils.prompt_templates.factoryr   litellm.proxy._typesr   r   litellm.routerr   litellm.utilsr   r   r   r-   r*   <module>r      s>    $ * * !  1 - ; N ! .}  } r-   