o
    ưi;)                     @   s   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 deZdS )    )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                	   @   s   e Zd Z	ddee fddZdded fdd	Zdd
ee fddZ	de
e fddZefdededefddZdedededefddZdededed dee fddZdS )"_OPTIONAL_PromptInjectionDetectionNprompt_injection_paramsc                 C   s.   || _ d | _g d| _g d| _g d| _d S )N)IgnoreZ	DisregardZSkipZForgetZNeglectZOverlookZOmitZBypasszPay no attention tozDo not followzDo not obey) ZpriorpreviousZ	precedingZaboveZ	foregoingZearlierinitial)r   zand start overzand start anewzand begin afreshzand start from scratch)r   
llm_routerverbs
adjectivesprepositions)selfr    r   e/home/app/Keep/.python/lib/python3.10/site-packages/litellm/proxy/hooks/prompt_injection_detection.py__init__   s
   


z+_OPTIONAL_PromptInjectionDetection.__init__DEBUGlevel)INFOr   c                 C   s@   |dkr
t | n	|dkrt | tjdu rt| d S d S )Nr    r   T)r   infodebuglitellmZset_verboseprint)r   print_statementr   r   r   r   print_verboseD   s   

z0_OPTIONAL_PromptInjectionDetection.print_verboserouterc                 C   s|   || _ | jd ur8| jjdu r:| j d u rtd| d| j j d| jj  | jjd u s4| jj| j jvr<tdd S d S d S )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	Exceptionr&   Zmodel_namesllm_api_name)r   r'   r   r   r   update_environmentM   s(   

z5_OPTIONAL_PromptInjectionDetection.update_environmentreturnc              
   C   sd   g }| j D ]*}| jD ]$}| jD ]}dtd |||g }t| dkr-||	  qq
q|S )N    )
r   r   r   joinfilterstriplensplitappendlower)r   combinationsverbZadjprepphraser   r   r   generate_injection_keywordse   s   


z>_OPTIONAL_PromptInjectionDetection.generate_injection_keywords
user_inputsimilarity_thresholdc           
   	   C   s   |  }|  }|D ];}t|}tt|| d D ]*}||||  }td || }	|	|krD| jd| d|	 d| dd   dS qq
dS )	N   zRejected user input - z. z similar to r    )r%   r   TF)r5   r:   r2   ranger   ratior&   )
r   r;   r<   Zuser_input_lowerkeywordskeywordZkeyword_lengthiZ	substringZmatch_ratior   r   r   check_user_input_similarityq   s    z>_OPTIONAL_PromptInjectionDetection.check_user_input_similarityuser_api_key_dictcachedata	call_typec              
      sn  zf	 |  d z|dv sJ W n ty%   |  d| d | Y W S w t||d}d}| jd urS| jjdu rK| j|d}|du rKtd	d
did| jjdu rR	 n| j|d}|du retd	d
did|W S  ty } z'|jd	krt	|j
trd
|j
v r| jd ur| jjr|j
d
W  Y d }~S |d }~w ty } ztdt| W Y d }~d S d }~ww )Nz/Inside Prompt Injection Detection Pre-Call Hook)acompletion
completionZtext_completion
embeddingsimage_generation
moderationaudio_transcriptionzCall Type - zj, not in accepted list - ['completion','embeddings','image_generation','moderation','audio_transcription']rF   rG   FT)r;     error4Rejected message. This is a prompt injection attack.status_codedetailz`litellm.proxy.hooks.prompt_injection_detection.py::async_pre_call_hook(): Exception occured - {})r&   r)   r   r   Zheuristics_checkrC   r   Zvector_db_checkrS   
isinstancerT   dictZreject_as_responsegetr   	exceptionformatstr)r   rD   rE   rF   rG   formatted_promptis_prompt_attacker   r   r   async_pre_call_hook   sr   
	






z6_OPTIONAL_PromptInjectionDetection.async_pre_call_hook)rH   rI   rJ   rK   rL   rM   c                    s  |  d| j  | jd u rd S t||d}d}t| jdt }| jjdu ru| jjd uru| jd uru| jj| jjd|dd|dgd	I d H }|  d
|  |  d| jj	  t
|tjrut
|jd tjru| jj	|jd jjv rud}|du rtdddid|S )Nz:IN ASYNC MODERATION HOOK - self.prompt_injection_params = rN   FZllm_api_system_promptTsystem)Zrolecontentuser)modelmessagesz"Received LLM Moderation response: zllm_api_fail_call_string: r   rO   rP   rQ   rR   )r&   r   r   getattrr   r(   r*   r   rH   Zllm_api_fail_call_stringrU   r#   ZModelResponsechoicesZChoicesmessager`   r   )r   rF   rD   rG   r[   r\   Zprompt_injection_system_promptresponser   r   r   async_moderation_hook   sP   


z8_OPTIONAL_PromptInjectionDetection.async_moderation_hook)N)r   )__name__
__module____qualname__r   r   r   r   r&   r   r+   r   rZ   r:   r	   floatboolrC   r   r   rV   r^   rh   r   r   r   r   r      sH    
&	

Pr   )difflibr   typingr   r   r   Zfastapir   r#   Zlitellm._loggingr   Zlitellm.caching.cachingr   Zlitellm.constantsr	   Z"litellm.integrations.custom_loggerr
   Z3litellm.litellm_core_utils.prompt_templates.factoryr   Zlitellm.proxy._typesr   r   Zlitellm.routerr   Zlitellm.utilsr   r   r   r   r   r   <module>   s   	