o
    ưi                     @   sl   d dl Z d dl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 G dd	 d	eZdS )
    N)Optional)HTTPException)verbose_proxy_logger)	DualCache)CustomLogger)UserAPIKeyAuthc                   @   sl   e Zd ZdddZdddZdd Zdded	ee fd
dZde	de
dedefddZdede	fddZdS )_PROXY_AzureContentSafetyNc              
   C   s   zddl m} ddlm}m}m} ddlm} ddlm	}	 W n t
y7 }
 zt
d|
 dt  dd }
~
ww || _|| _|| _|| _|| _|	| _| || _|| j|| j| _d S )	Nr   )ContentSafetyClient)AnalyzeTextOptionsAnalyzeTextOutputTypeTextCategory)AzureKeyCredential)HttpResponseErrorzm[91mAzure Content-Safety not installed, try running 'pip install azure-ai-contentsafety' to fix this error: 
z[0m)Zazure.ai.contentsafety.aior	   Zazure.ai.contentsafety.modelsr
   r   r   Zazure.core.credentialsr   Zazure.core.exceptionsr   	Exception	traceback
format_excendpointapi_keytext_categoryanalyze_text_optionsanalyze_text_output_typeazure_http_error_configure_thresholds
thresholdsclient)selfr   r   r   r	   r
   r   r   r   r   e r   _/home/app/Keep/.python/lib/python3.10/site-packages/litellm/proxy/hooks/azure_content_safety.py__init__   s*   
z"_PROXY_AzureContentSafety.__init__c                 C   sV   | j jd| j jd| j jd| j jdi}|d u r|S | D ]\}}||vr(|||< q|S )N   )r   ZHATEZ	SELF_HARMZSEXUALZVIOLENCEitems)r   r   Zdefault_thresholdskeydefaultr   r   r   r   -   s   z/_PROXY_AzureContentSafety._configure_thresholdsc                 C   sN   i }dd |j D }| jD ]}||}|d ur$|| j| k|d||< q|S )Nc                 S   s   i | ]}|j |jqS r   )categoryseverity).0itemr   r   r   
<dictcomp>A   s    
z=_PROXY_AzureContentSafety._compute_result.<locals>.<dictcomp>)filteredr&   )Zcategories_analysisr   getr   )r   responseresultZcategory_severityr%   r&   r   r   r   _compute_result>   s   


z)_PROXY_AzureContentSafety._compute_resultcontentsourcec                    s   t d| | j|| jjd}z| j|I d H }W n | jy3   t dt	  t t	   w | 
|}t d| | D ]\}}|d rXtdd|||d d	d
qCd S )Nz$Testing Azure Content-Safety for: %s)textoutput_typez!Error in Azure Content-Safety: %szAzure Content-Safety Result: %sr*   i  zViolated content safety policyr&   )errorr0   r%   r&   )status_codedetail)r   debugr   r   ZEIGHT_SEVERITY_LEVELSr   Zanalyze_textr   r   r   r.   r"   r   )r   r/   r0   requestr,   r-   r#   valuer   r   r   test_violationN   s:   
z(_PROXY_AzureContentSafety.test_violationuser_api_key_dictcachedata	call_typec              
      s   t d z/|dkr-d|v r0|d D ]}d|v r,t|d tr,| j|d ddI d H  qW d S W d S W d S  tyC } z|d }~w tyf } zt dt| t t	
  W Y d }~d S d }~ww )Nz)Inside Azure Content-Safety Pre-Call Hook
completionmessagesr/   inputr/   r0   zZlitellm.proxy.hooks.azure_content_safety.py::async_pre_call_hook(): Exception occured - {})r   r6   
isinstancestrr9   r   r   r3   formatr   r   )r   r:   r;   r<   r=   mr   r   r   r   async_pre_call_hookp   s,   
z-_PROXY_AzureContentSafety.async_pre_call_hookc                    sZ   t d t|tjr)t|jd tjjr+| j|jd j	j
p dddI d H  d S d S d S )Nz*Inside Azure Content-Safety Post-Call Hookr    outputrA   )r   r6   rB   litellmZModelResponsechoicesutilsZChoicesr9   messager/   )r   r<   r:   r,   r   r   r   async_post_call_success_hook   s   
z6_PROXY_AzureContentSafety.async_post_call_success_hook)N)__name__
__module____qualname__r    r   r.   rC   r   r9   r   r   dictrF   rM   r   r   r   r   r      s&    

"
r   )r   typingr   Zfastapir   rI   Zlitellm._loggingr   Zlitellm.caching.cachingr   Z"litellm.integrations.custom_loggerr   Zlitellm.proxy._typesr   r   r   r   r   r   <module>   s    
