o
    ưi5                     @   s   d dl mZmZ d dl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 G d
d de
ZdS )    )LiteralOptionalN)HTTPException)verbose_proxy_logger)CustomLogger)UserAPIKeyAuth)get_secret_str)CallTypesLiteral)get_formatted_promptc                   @   s|   e Zd Z		ddedee fddZdd Zd	efd
dZ	de
dedefddZdede
defddZde
defddZdS )_ENTERPRISE_LLMGuardFNmock_testingmock_redacted_textc                 C   sZ   || _ tj| _|du rd S tdd | _| jd u rtd| jds+|  jd7  _d S d S )NTZLLM_GUARD_API_BASEz-Missing `LLM_GUARD_API_BASE` from environment/)r   litellmllm_guard_moder   llm_guard_api_base	Exceptionendswith)selfr   r    r   h/home/app/Keep/.python/lib/python3.10/site-packages/litellm_enterprise/enterprise_callbacks/llm_guard.py__init__   s   
z_ENTERPRISE_LLMGuard.__init__c                 C   s:   zt | tjrt| W d S W d S  ty   Y d S w )N)r   debugr   Zset_verboseprintr   )r   Zprint_statementr   r   r   print_verbose)   s   
z"_ENTERPRISE_LLMGuard.print_verbosetextc              
      s^  zt  4 I dH }| jdur| j}n:| j d}td| d|i}d}|j||d4 I dH }| I dH }W d  I dH  n1 I dH sIw   Y  td|  |durr|dddurp|d du rpt	d	d
did	 nt	dd
d| idW d  I dH  W dS 1 I dH sw   Y  W dS  t
y } ztdt| |d}~ww )zO
        [TODO] make this more performant for high-throughput scenario
        Nzanalyze/promptzMaking request to: %sprompt)jsonzLLM Guard: Received response - Zis_validFi  errorzViolated content safety policy)status_codedetaili  z%Invalid content moderation response: zYlitellm.enterprise.enterprise_hooks.llm_guard::moderation_check - Exception occurred - {})aiohttpZClientSessionr   r   r   r   postr   getr   r   	exceptionformatstr)r   r   sessionZredacted_textZanalyze_urlZanalyze_payloadresponseer   r   r   moderation_check1   sR   
(
2"z%_ENTERPRISE_LLMGuard.moderation_checkuser_api_key_dictdatareturnc                 C   s   | j dkr| d|j  |jdddu rdS dS | j dkr"dS | j dkrK| d|d	i   |d	i }|d
i }d|v rK|d du rKdS dS )Nzkey-specificzuser_api_key_dict.permissions: Zenable_llm_guard_checkFTallzrequest-specificzreceived metadata: metadatapermissions)r   r   r0   r#   )r   r+   r,   r/   r0   r   r   r   should_proceed`   s&   



z#_ENTERPRISE_LLMGuard.should_proceed	call_typec                    s   |  d| j  | j||d}|du rdS |  d z|dv s#J W n ty8   |  d| d | Y S w t||d	}|  d
|  | j|dI dH S )z
        - Calls the LLM Guard Endpoint
        - Rejects request if it fails safety check
        - Use the sanitized prompt returned
            - LLM Guard can handle things like PII Masking, etc.
        z0Inside LLM Guard Pre-Call Hook - llm_guard_mode=)r+   r,   FNzMakes LLM Guard Check)
completionZ
embeddingsZimage_generationZ
moderationZaudio_transcriptionzCall Type - zj, not in accepted list - ['completion','embeddings','image_generation','moderation','audio_transcription'])r,   r2   zLLM Guard, formatted_prompt: r   )r   r   r1   r   r
   r*   )r   r,   r+   r2   Z_proceedZformatted_promptr   r   r   async_moderation_hookx   s&   


z*_ENTERPRISE_LLMGuard.async_moderation_hookr(   c                    s    |d ur| j |dI d H  |S )Nr4   )r*   )r   r+   r(   r   r   r   async_post_call_streaming_hook   s   z3_ENTERPRISE_LLMGuard.async_post_call_streaming_hook)FN)__name__
__module____qualname__boolr   dictr   r   r&   r*   r   r1   r	   r5   r6   r   r   r   r   r      s.    
/
'r   )typingr   r   r!   Zfastapir   r   Zlitellm._loggingr   Z"litellm.integrations.custom_loggerr   Zlitellm.proxy._typesr   Zlitellm.secret_managers.mainr   Zlitellm.types.utilsr	   Zlitellm.utilsr
   r   r   r   r   r   <module>   s   	