o
    ưi$                     @   s   d dl mZmZmZmZ d dlZd dlmZ d dlT de	de
fddZde	de
fd	d
Zde	de
fddZde	de
fddZde	de
fddZde	de
fddZde	de
fddZdd ZdS )    )AnyDictListOptionalN)CommonProxyErrors)*litellm_params	guardrailc                 C   s   ddl m} |di d|ddd| jd| jd| jd| jd	| jd
| jd| j	d| j
d| jd| jd| jd| jd| jd| jd| jd| jd| jd| j}tj| |S )Nr   )BedrockGuardrailguardrail_name 
event_hookguardrailIdentifierguardrailVersion
default_ondisable_exception_on_blockmask_request_contentmask_response_contentaws_region_nameaws_access_key_idaws_secret_access_keyaws_session_tokenaws_session_nameaws_profile_nameaws_role_nameaws_web_identity_tokenaws_sts_endpointaws_bedrock_runtime_endpoint)experimental_use_latest_role_message_only )Z;litellm.proxy.guardrails.guardrail_hooks.bedrock_guardrailsr
   getmoder   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   litellmlogging_callback_manageradd_litellm_callback)r   r	   r
   Z_bedrock_callbackr   r   f/home/app/Keep/.python/lib/python3.10/site-packages/litellm/proxy/guardrails/guardrail_initializers.pyinitialize_bedrock	   sT   	
r&   c                 C   sB   ddl m} || j| j|dd| j| j| jd}tj	
| |S )Nr   )lakeraAI_Moderationr   r   )api_baseapi_keyr   r   category_thresholdsr   )Z2litellm.proxy.guardrails.guardrail_hooks.lakera_air'   r(   r)   r    r!   r*   r   r"   r#   r$   )r   r	   r'   Z_lakera_callbackr   r   r%   initialize_lakera'   s   
r+   c                 C   sV   ddl m} || j| j|dd| j| j| j| j| j	| j
| j| jd}tj| |S )Nr   )LakeraAIGuardrailr   r   )r(   r)   r   r   r   
project_idpayload	breakdownmetadatadev_info
on_flagged)Z5litellm.proxy.guardrails.guardrail_hooks.lakera_ai_v2r,   r(   r)   r    r!   r   r-   r.   r/   r0   r1   r2   r"   r#   r$   )r   r	   r,   Z_lakera_v2_callbackr   r   r%   initialize_lakera_v26   s    
r3   c                    s   ddl m  tdd pd}|dv }|dv } fdd}d }|r0| }jr0|d	tjjd
 |rA|d	tjjdd}|d u rA|}|S )Nr   )_OPTIONAL_PresidioPIIMaskingZpresidio_filter_scopeboth)inputr5   )outputr5   c                     sh   t ddjjjjjjjj	j
jjdd}||   di |}tj| |S )Nr   r   F)r   r   output_parse_piipresidio_ad_hoc_recognizersmock_redacted_textr   pii_entities_configpresidio_score_thresholdspresidio_analyzer_api_basepresidio_anonymizer_api_basepresidio_languagepresidio_entities_deny_listapply_to_outputr   )dictr    r!   r8   r9   r:   r   r;   r<   r=   r>   r?   r@   updater"   r#   r$   )Z	overridesparamscallbackr4   r	   r   r   r%   _make_presidio_callbackS   s&   

z4initialize_presidio.<locals>._make_presidio_callbackT)r8   r   F)rA   r   r8   )Z1litellm.proxy.guardrails.guardrail_hooks.presidior4   getattrr8   ZGuardrailEventHooksZ	post_callvalue)r   r	   Zfilter_scopeZ	run_inputZ
run_outputrG   Zprimary_callbackZoutput_callbackr   rF   r%   initialize_presidioJ   s,   rJ   c                 C   s^   zddl m} W n ty   tdtjj w || j| j|	dd| j
d}tj| |S )Nr   )_ENTERPRISE_SecretDetectionzTrying to use Secret Detectionr   r   )detect_secrets_configr   r   r   )Z8litellm_enterprise.enterprise_callbacks.secret_detectionrK   ImportError	Exceptionr   Zmissing_enterprise_packagerI   rL   r!   r    r   r"   r#   r$   )r   r	   rK   Z_secret_detection_objectr   r   r%   initialize_hide_secrets   s"   
rO   c              	   C   s   ddl m} d }| jr'g }| jD ]}t|dr||  q|t| q||dd| j|t	| ddt	| dd	| j
| jd
}tj| |S )Nr   )ToolPermissionGuardrail
model_dumpr   r   default_actionZdenyon_disallowed_actionblock)r   r   rulesrR   rS   r   violation_message_template)Z8litellm.proxy.guardrails.guardrail_hooks.tool_permissionrP   rU   hasattrappendrQ   rB   r    r!   rH   r   rV   r"   r#   r$   )r   r	   rP   rU   ruleZ_tool_permission_callbackr   r   r%   initialize_tool_permission   s&   




	rZ   c              
   C   sJ   ddl m} ||dd| j| j| j| j| j| j| j	d}t
j| |S )Nr   )LassoGuardrailr   r   )r   Zlasso_api_keyr(   Zuser_idZconversation_idmaskr   r   )Z.litellm.proxy.guardrails.guardrail_hooks.lassor[   r    r)   r(   Zlasso_user_idZlasso_conversation_idr\   r!   r   r"   r#   r$   )r   r	   r[   Z_lasso_callbackr   r   r%   initialize_lasso   s   

r]   c                 C   s   ddl m} | jstd| jstd||dd| j| jp d| j| jt| dd	t| d
d	t| dd	t| dd t| ddt	t| dd| j
d}tj| |S )Nr   )PanwPrismaAirsHandlerz%PANW Prisma AIRS: api_key is requiredz*PANW Prisma AIRS: profile_name is requiredr   Zpanw_prisma_airszHhttps://service.api.aisecurity.paloaltonetworks.com/v1/scan/sync/requestmask_on_blockFr   r   app_namefallback_on_errorrT   timeoutg      $@)r   r)   r(   profile_namer   r_   r   r   r`   ra   rb   rV   )Z9litellm.proxy.guardrails.guardrail_hooks.panw_prisma_airsr^   r)   
ValueErrorrc   r    r(   r   rH   floatrV   r"   r#   r$   )r   r	   r^   Z_panw_callbackr   r   r%   initialize_panw_prisma_airs   s0   




rf   )typingr   r   r   r   r"   Zlitellm.proxy._typesr   Zlitellm.types.guardrailsZLitellmParamsZ	Guardrailr&   r+   r3   rJ   rO   rZ   r]   rf   r   r   r   r%   <module>   s    5
