
    7|h                         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 j                  e      ZdedefdZ G d	 d
e
      Zy)    N)AnyCallableDictListOptional)CallbackManagerForLLMRun)LLM)model_validator	violationreturnc                     | j                  d      r| d   S | j                  d      rd| j                  d       nd}t        | d      )zDefault guardrail violation handler.

    Args:
        violation (dict): The violation dictionary.

    Returns:
        str: The canned response.
    canned_responseoffending_guardrailz
Guardrail zA guardrailz; was violated without a proper guardrail violation handler.)get
ValueError)r   guardrail_names     h/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/llms/layerup_security.py#default_guardrail_violation_handlerr      sf     }}&'*++ ==./ Y]]#89:; 
 
UV     c                   X   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      edeeef   defd              Zedefd       Z	 	 ddede	e
e      de	e   dedef
dZy)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                 j    	 ddl m}  ||d   |d         |d<   |S # t        $ r t        d      w xY w)Nr   )r   r   r   )api_keybase_urlr!   z[Could not import LayerupSecurity SDK. Please install it with `pip install LayerupSecurity`.)layerup_securityr   ImportError)clsr$   LayerupSecuritySDKs      r   validate_layerup_sdkz$LayerupSecurity.validate_layerup_sdk2   sV    	N101 67 F8   	H 	s    2c                      y)Nr(    )selfs    r   	_llm_typezLayerupSecurity._llm_typeC   s    !r   Npromptstoprun_managerkwargsc                 n   d|dg}d }| j                   r)| j                  j                  || j                        \  }}| j                  rH| j                  j                  | j                  ||| j                        }|d   s| j                  |      S  | j                  j                  |d   d   fd|i|}| j                   r
|r ||      }|j                  d|d       | j                  rH| j                  j                  | j                  ||| j                        }|d   s| j                  |      S |S )Nuser)rolecontentall_safer   r8   r3   	assistant)r   r!   mask_promptr   r   execute_guardrailsr   r   _callappendr   r    )	r/   r1   r2   r3   r4   messagesunmask_responsesecurity_responseresults	            r   r=   zLayerupSecurity._callG   s1    $7899(,(?(?$--(X%Ho!! $ > >&&&$--! %Z0==>OPPQK	"
0;
?E
 99$V,F@A## $ > >(((FDMM! %Z0??@QRRr   )NN)__name__
__module____qualname____doc__r	   __annotations__strr   r   r   r   r   r   boolr   r   r   r   r   r   dictr    r
   classmethodr,   propertyr0   r   r=   r.   r   r   r   r       s=   '	H ?#?-/xS	*//1$s),1D$)+HhtCH~&++ &x'<  	, (4&#+)>  K(#$sCx. S   $ "3 " " %):>	$$ tCy!$ 67	$
 $ 
$r   r   )loggingtypingr   r   r   r   r   langchain_core.callbacksr   #langchain_core.language_models.llmsr	   pydanticr
   	getLoggerrC   loggerrJ   rH   r   r   r.   r   r   <module>rT      sJ     6 6 = 3 $			8	$4 C *Kc Kr   