
    g                     r    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	e      Zy)
    N)Optional)HTTPException)verbose_proxy_logger)	DualCache)CustomLogger)UserAPIKeyAuthc                   b    e Zd ZddZddZd Zddedee   fdZde	d	e
d
edefdZd
ede	fdZy)_PROXY_AzureContentSafetyNc                 x   	 ddl m} ddlm}m}m} ddlm} ddlm	}	 || _        || _        || _        || _        || _        |	| _        | j'                  |      | _         || j                   || j                              | _        y # t        $ r)}
t        d|
 dt        j                          d      d }
~
ww xY w)	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)azure.ai.contentsafety.aior   azure.ai.contentsafety.modelsr   r   r   azure.core.credentialsr   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   es              ]/var/www/openai/venv/lib/python3.12/site-packages/litellm/proxy/hooks/azure_content_safety.py__init__z"_PROXY_AzureContentSafety.__init__   s    	F 
 B?
 !)$6!(=% 144Z@)MM-dll;
  	 C  DE  CF  FH  IR  I]  I]  I_  H`  `g  h 	s   B 	B9$B44B9c                    | j                   j                  d| j                   j                  d| j                   j                  d| j                   j                  di}||S |j                         D ]  \  }}||vs|||<    |S )N   )r   HATE	SELF_HARMSEXUALVIOLENCEitems)r#   r!   default_thresholdskeydefaults        r%   r    z/_PROXY_AzureContentSafety._configure_thresholds-   s    ##Q((!%%q''	
 %%.446LC*$")
3 7     c                     i }|j                   D ci c]  }|j                  |j                   }}| j                  D ].  }|j	                  |      }||| j
                  |   k\  |d||<   0 |S c c}w )N)filteredseverity)categories_analysiscategoryr4   r   getr!   )r#   responseresultitemcategory_severityr6   r4   s          r%   _compute_resultz)_PROXY_AzureContentSafety._compute_result>   s     6>5Q5Q
5QTDMM4==(5Q 	 
 **H(,,X6H# (DOOH,E E ($x  + 
s   A0contentsourcec           	      J  K   t        j                  d|       | j                  || j                  j                        }	 | j
                  j                  |       d {   }| j                  |      }t        j                  d|       |j                         D ]  \  }}|d   st        dd|||d   d	
       y 7 ^# | j                  $ rQ t        j                  dt        j                                t        j                  t        j                                 w xY ww)Nz$Testing Azure Content-Safety for: %s)textoutput_typez!Error in Azure Content-Safety: %szAzure Content-Safety Result: %sr3   i  zViolated content safety policyr4   )errorr>   r6   r4   )status_codedetail)r   debugr   r   EIGHT_SEVERITY_LEVELSr"   analyze_textr   r   r   r<   r-   r   )r#   r=   r>   requestr8   r9   r/   values           r%   test_violationz(_PROXY_AzureContentSafety.test_violationN   s    ""#I7S ++55KK , 
	![[55g>>H %%h/""#DfM ,,.JCZ # #!A"($'$)*$5	  ) ?$$ 	 &&3Y5I5I5K !&&y';';'=>	s7   >D#C  B> C  $AD#'D#>C   A D  D#user_api_key_dictcachedata	call_typec                   K   t        j                  d       	 |dk(  rGd|v rB|d   D ]9  }d|v st        |d   t              s| j	                  |d   d       d {    ; y y y 7 	# t
        $ r}|d }~wt        $ r^}t        j                  dj                  t        |                   t        j                  t        j                                Y d }~y d }~ww xY ww)Nz)Inside Azure Content-Safety Pre-Call Hook
completionmessagesr=   inputr=   r>   zZlitellm.proxy.hooks.azure_content_safety.py::async_pre_call_hook(): Exception occured - {})r   rE   
isinstancestrrJ   r   r   rB   formatr   r   )r#   rK   rL   rM   rN   mr$   s          r%   async_pre_call_hookz-_PROXY_AzureContentSafety.async_pre_call_hookp   s      	""#NO	?L(Z4-?j)A A~*Qy\3*G"11!I,w1WWW * .@( X 	G 	? &&lssF
 !&&y';';'=>>	?sX   C A( A( A( A&A( #C &A( (	C1A33C?ACC CC c                 T  K   t        j                  d       t        |t        j                        rst        |j
                  d   t        j                  j                        rA| j                  |j
                  d   j                  j                  xs dd       d {    y y y 7 w)Nz*Inside Azure Content-Safety Post-Call Hookr    outputrS   )r   rE   rT   litellmModelResponsechoicesutilsChoicesrJ   messager=   )r#   rM   rK   r8   s       r%   async_post_call_success_hookz6_PROXY_AzureContentSafety.async_post_call_success_hook   s      	""#OPh 5 56:Q!6!6<
 %% ((+33;;Ar( &   <
6s   BB(B&B()N)__name__
__module____qualname__r&   r    r<   rU   r   rJ   r   r   dictrX   rb    r1   r%   r
   r
      sk    

6"  C  #  D?)? ? 	?
 ?0 *r1   r
   )r   typingr   fastapir   r\   litellm._loggingr   litellm.caching.cachingr   "litellm.integrations.custom_loggerr   litellm.proxy._typesr   r
   rg   r1   r%   <module>rn      s,      !  1 - ; /GGr1   