
    h(                         d dl mZ d dlmZmZmZ d dl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
lmZmZ d dlmZ d dlmZ  G d de      Zy)    )SequenceMatcher)ListLiteralOptional)HTTPExceptionN)verbose_proxy_logger)	DualCache)-DEFAULT_PROMPT_INJECTION_SIMILARITY_THRESHOLD)CustomLogger)%prompt_injection_detection_default_pt)LiteLLMPromptInjectionParamsUserAPIKeyAuth)Router)get_formatted_promptc            	           e Zd Z	 ddee   fdZdded   fdZddee   fdZ	d	e
e   fd
Zefdeded	efdZdedededefdZdededed   d	ee   fdZy)"_OPTIONAL_PromptInjectionDetectionNprompt_injection_paramsc                 V    || _         d | _        g d| _        g d| _        g d| _        y )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     j/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/proxy/hooks/prompt_injection_detection.py__init__z+_OPTIONAL_PromptInjectionDetection.__init__   s/     (?$,0

	

    level)INFODEBUGc                     |dk(  rt        j                  |       n|dk(  rt        j                  |       t        j                  du rt        |       y y )Nr.   r/   T)r   infodebuglitellmset_verboseprint)r)   print_statementr-   s      r*   print_verbosez0_OPTIONAL_PromptInjectionDetection.print_verboseD   sH    F? %%o6g &&7$&/" 'r,   routerc                    || _         | j                  | j                  j                  du r| j                   t        d      | j	                  d| j                   j
                   d| j                  j                          | j                  j                  ,| j                  j                  | j                   j
                  vrt        d      y y y )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	Exceptionr7   model_namesllm_api_name)r)   r8   s     r*   update_environmentz5_OPTIONAL_PromptInjectionDetection.update_environmentM   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                 @   g }| j                   D ]  }| j                  D ]{  }| j                  D ]j  }dj                  t	        d |||g            j                         }t        |j                               dkD  sL|j                  |j                                l }  |S )N    )
r&   r'   r(   joinfilterstriplensplitappendlower)r)   combinationsverbadjprepphrases         r*   generate_injection_keywordsz>_OPTIONAL_PromptInjectionDetection.generate_injection_keywordse   s    JJD --D XXfTD#t3D&EFLLNFFLLN+a/$++FLLN; . '  r,   
user_inputsimilarity_thresholdc           
      4   |j                         }| j                         }|D ]s  }t        |      }t        t        |      |z
  dz         D ]I  }||||z    }t	        d ||      j                         }	|	|kD  s,| j                  d| d|	 d| d         y u y)	N   zRejected user input - z. z similar to r.   )r6   r-   TF)rI   rO   rF   ranger   ratior7   )
r)   rP   rQ   user_input_lowerkeywordskeywordkeyword_lengthi	substringmatch_ratios
             r*   check_user_input_similarityz>_OPTIONAL_PromptInjectionDetection.check_user_input_similarityq   s    
 &++-335G \N3/0>AAEF,Q^1CD	 .dIwGMMO!55&&*@B{m[ghogp(q$ '    G	    r,   user_api_key_dictcachedata	call_typec                 P  K   	 	 | j                  d       	 |dv sJ 	 t        ||      }d}| j                  V| j                  j                  du r%| j                  |      }|du rt        d	d
di      | j                  j                  du rn| j                  |      }|du rt        d	d
di      |S # t        $ r | j                  d| d       |cY S w xY w# t        $ r}|j                  d	k(  rjt        |j                  t              rPd
|j                  v rB| j                  6| j                  j                  r |j                  j                  d
      cY d }~S |d }~wt        $ r7}t        j                  dj!                  t#        |                   Y d }~y d }~ww xY ww)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']r`   ra   FT)rP     error4Rejected message. This is a prompt injection attack.status_codedetailz`litellm.proxy.hooks.prompt_injection_detection.py::async_pre_call_hook(): Exception occured - {})r7   r;   r   r   heuristics_checkr]   r   vector_db_checkrn   
isinstancero   dictreject_as_responsegetr   	exceptionformatstr)r)   r^   r_   r`   ra   formatted_promptis_prompt_attackes           r*   async_pre_call_hookz6_OPTIONAL_PromptInjectionDetection.async_pre_call_hook   s    F	 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   F&C B6 BC 5F&6 CC F&CC 	F#%A3E F#F&E  F#,-FF&F##F&)rc   re   rf   rg   rh   c                 T  K   | j                  d| j                          | j                  y t        ||      }d}t        | j                  dt	                     }| j                  j
                  du r| j                  j                  | j                  | j                  j                  | j                  j                  d|dd|dg	       d {   }| j                  d
|        | j                  d| j                  j                          t        |t        j                        rbt        |j                  d   t        j                        r;| j                  j                  |j                  d   j                  j                   v rd}|du rt#        dddi      |S 7 ѭw)Nz:IN ASYNC MODERATION HOOK - self.prompt_injection_params = ri   Fllm_api_system_promptTsystem)rolecontentuser)modelmessagesz"Received LLM Moderation response: zllm_api_fail_call_string: r   rj   rk   rl   rm   )r7   r   r   getattrr   r:   r=   r%   acompletionllm_api_fail_call_stringrr   r3   ModelResponsechoicesChoicesmessager   r   )r)   r`   r^   ra   ry   rz   prompt_injection_system_promptresponses           r*   async_moderation_hookz8_OPTIONAL_PromptInjectionDetection.async_moderation_hook   s     	HIeIeHfg	
 ''//TY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F(F&CF()N)r/   )__name__
__module____qualname__r   r   r+   r   r7   r   r>   r   rx   rO   r
   floatboolr]   r   r	   rs   r|   r    r,   r*   r   r      s     KO$
!)*F!G$
L#GO4L #&)9 0
T#Y 
 'T $ 
	4M)M M 	M
 M^@ @  *@  #
	@  
$@ r,   r   )difflibr   typingr   r   r   fastapir   r3   litellm._loggingr   litellm.caching.cachingr	   litellm.constantsr
   "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      sA    $ * * !  1 - K ; N ! .~  ~ r,   