o
    Zh                     @   sx   d dl Z d dlmZmZmZmZmZ d dlmZ d dl	m
Z
 d dlmZ e eZdedefdd	ZG d
d de
ZdS )    N)AnyCallableDictListOptional)CallbackManagerForLLMRun)LLM)model_validator	violationreturnc                 C   s>   |  dr	| d S |  drd|  d nd}t| d)zDefault guardrail violation handler.

    Args:
        violation (dict): The violation dictionary.

    Returns:
        str: The canned response.
    Zcanned_responseZoffending_guardrailz
Guardrail zA guardrailz; was violated without a proper guardrail violation handler.)get
ValueError)r
   Zguardrail_name r   `/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/layerup_security.py#default_guardrail_violation_handler   s   
	r   c                   @   s  e Zd ZU dZeed< eed< dZeed< g Ze	e
e  ed< g Ze	e
e  ed< dZeed	< i Ze	eeef  ed
< eZeegef ed< eZeegef ed< eed< eddedeeef defddZedefddZ		ddede	e
e  de	e dedef
ddZdS )LayerupSecurityzLayerup Security LLM service.llmlayerup_api_keyzhttps://api.uselayerup.com/v1layerup_api_base_urlprompt_guardrailsresponse_guardrailsFmaskmetadata!handle_prompt_guardrail_violation#handle_response_guardrail_violationclientbefore)modevaluesr   c                 C   sB   zddl m} ||d |d d|d< W |S  ty    tdw )Nr   )r   r   r   )Zapi_keybase_urlr   z[Could not import LayerupSecurity SDK. Please install it with `pip install LayerupSecurity`.)layerup_securityr   ImportError)clsr   ZLayerupSecuritySDKr   r   r   validate_layerup_sdk2   s   	z$LayerupSecurity.validate_layerup_sdkc                 C   s   dS )Nr    r   )selfr   r   r   	_llm_typeC   s   zLayerupSecurity._llm_typeNpromptstoprun_managerkwargsc           	      K   s   d|dg}d }| j r| j|| j\}}| jr,| j| j||| j}|d s,| |S | jj|d d fd|i|}| j rE|rE||}|	d|d | j
rd| j| j
||| j}|d sd| |S |S )Nuser)ZrolecontentZall_safer   r+   r(   Z	assistant)r   r   Zmask_promptr   r   Zexecute_guardrailsr   r   _callappendr   r   )	r$   r&   r'   r(   r)   messagesZunmask_responseZsecurity_responseresultr   r   r   r,   G   s6   



zLayerupSecurity._call)NN)__name__
__module____qualname____doc__r   __annotations__strr   r   r   r   r   r   boolr   r   r   r   r   r   dictr   r	   classmethodr#   propertyr%   r   r,   r   r   r   r   r       s@   
 
r   )loggingtypingr   r   r   r   r   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr   Zpydanticr	   	getLoggerr0   loggerr7   r5   r   r   r   r   r   r   <module>   s    
