a
    bg                     @   sv   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eedddZG d	d
 d
e
ZdS )    N)AnyCallableDictListOptional)CallbackManagerForLLMRun)LLM)model_validator)	violationreturnc                 C   sB   |  dr| d S |  dr,d|  d nd}t| ddS )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.N)get
ValueError)r
   Zguardrail_name r   w/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/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eeef edddZeedddZdee	e
e  e	e eed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   sH   z(ddl m} ||d |d d|d< W n tyB   tdY n0 |S )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_sdk)r   c                 C   s   dS )Nr    r   )selfr   r   r   	_llm_typeC   s    zLayerupSecurity._llm_typeN)promptstoprun_managerkwargsr   c           	      K   s   d|dg}d }| j r*| j|| j\}}| jrX| j| j||| j}|d sX| |S | jj|d d fd|i|}| j r|r||}|	d|d | j
r| j| j
||| j}|d s| |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       s6   
  
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   
