
    Ng                         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dS )    N)AnyCallableDictListOptional)CallbackManagerForLLMRun)LLM)model_validator	violationreturnc                     |                      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.
    canned_responseoffending_guardrailz
Guardrail zA guardrailz; was violated without a proper guardrail violation handler.)get
ValueError)r   guardrail_names     e/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/llms/layerup_security.py#default_guardrail_violation_handlerr      s     }}&'' ,*++ ==.//	;Y]]#899;;; 
 VVV      c                      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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                     	 ddl m}  ||d         |d                   |d<   n# t          $ r t          d          w xY w|S )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   s    	NNNNNN1101 67     F8  	 	 	H  	
 s   "% ?c                     dS )Nr(    )selfs    r   	_llm_typezLayerupSecurity._llm_typeC   s    !!r   Npromptstoprun_managerkwargsc                 0   d|dg}d }| j         r#| j                            || j                  \  }}| j        rD| j                            | j        ||| j                  }|d         s|                     |          S  | j        j        |d         d         fd|i|}| j         r|r ||          }|	                    d|d           | j
        rD| j                            | j
        ||| j                  }|d         s|                     |          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   sg    $7789 	Y(,(?(?$-(X(X%Ho! 	Q $ > >&&$-! ! %Z0 Q==>OPPPQK	"
 
0;
?E
 
 9 	- 	-$_V,,F@@AAA# 	S $ > >((FDM! ! %Z0 S??@QR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        ''	HHH ?#???-/xS	*////1$s),111D$)+HhtCH~&++++ &x'<    	, (4&#+)>    KKK_(###$sCx. S    [ $# "3 " " " X" %):>	$ $$ 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      s     6 6 6 6 6 6 6 6 6 6 6 6 6 6 = = = = = = 3 3 3 3 3 3 $ $ $ $ $ $		8	$	$4 C    *K K K K Kc K K K K Kr   