
    dh                         S SK r S SKJrJrJrJr  S SKJr  S SKJ	r	  S SK
Jr  S SKJr  S SKJrJr  S SKJr  \ R&                  " \5      r " S	 S
\5      rg)    N)AnyDictListOptional)CallbackManagerForLLMRun)BaseLanguageModel)LLM)	AIMessage)get_from_dict_or_envpre_init)
ConfigDictc                       \ rS rSr% Sr\\S'    \" SS9r\	S\
S\
4S j5       r  SS
\S\\\      S\\   S\S\4
S jjr\S\4S j5       rSrg	)OpaquePrompts   a>  LLM that uses OpaquePrompts to sanitize prompts.

Wraps another LLM and sanitizes prompts before passing it to the LLM, then
    de-sanitizes the response.

To use, you should have the ``opaqueprompts`` python package installed,
and the environment variable ``OPAQUEPROMPTS_API_KEY`` set with
your API key, or pass it as a named parameter to the constructor.

Example:
    .. code-block:: python

        from langchain_community.llms import OpaquePrompts
        from langchain_community.chat_models import ChatOpenAI

        op_llm = OpaquePrompts(base_llm=ChatOpenAI())
base_llmforbid)extravaluesreturnc                      SSK nUR                  c  [        S5      e[	        USSSS9nU(       d  [        S	5      eU$ ! [         a    [        S5      ef = f)
zFValidates that the OpaquePrompts API key and the Python package exist.r   NzhCould not import the `opaqueprompts` Python package, please install it with `pip install opaqueprompts`.zMCould not properly import `opaqueprompts`, opaqueprompts.__package__ is None.opaqueprompts_api_keyOPAQUEPROMPTS_API_KEY )defaultzCould not find OPAQUEPROMPTS_API_KEY in the environment. Please set it to your OpaquePrompts API key.You can get it by creating an account on the OpaquePrompts website: https://opaqueprompts.opaque.co/ .)opaquepromptsImportError__package__
ValueErrorr   )clsr   opapi_keys       ^/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/llms/opaqueprompts.pyvalidate_environment"OpaquePrompts.validate_environment(   s    	& >>!5 
 '+-Db
 5  +  	F 	s	   > ANpromptstoprun_managerkwargsc                 f   SSK nU=(       d    [        R                  " 5       nUR                  U/5      nUR                  S   nU R
                  R                  US9R                  U5      n	[        U	[        5      (       a  U	R                  n	UR                  U	UR                  S9n
U
R                  $ )a  Call base LLM with sanitization before and de-sanitization after.

Args:
    prompt: The prompt to pass into the model.

Returns:
    The string generated by the model.

Example:
    .. code-block:: python

        response = op_llm.invoke("Tell me a joke.")
r   N)r&   )secure_context)r   r   get_noop_managersanitizesanitized_textsr   bindinvoke
isinstancer
   content
desanitizer*   desanitized_text)selfr%   r&   r'   r(   r    _run_managersanitize_responsesanitized_prompt_value_strllm_responsedesanitize_responses              r"   _callOpaquePrompts._callD   s    ( 	#"Q&>&O&O&Q 24fX1F%6%F%Fq%I" }}))t)4;;&
 lI..'//L 68]],;; 6C 6
 #333    c                     g)zCReturn type of LLM.

This is an override of the base class method.
r    )r4   s    r"   	_llm_typeOpaquePrompts._llm_typeo   s     r<   r>   )NN)__name__
__module____qualname____firstlineno____doc__r   __annotations__r   model_configr   r   r#   strr   r   r   r   r:   propertyr?   __static_attributes__r>   r<   r"   r   r      s    $  L $ 4  < %):>	)4)4 tCy!)4 67	)4
 )4 
)4V 3  r<   r   )loggingtypingr   r   r   r   langchain_core.callbacksr   langchain_core.language_modelsr   #langchain_core.language_models.llmsr	   langchain_core.messagesr
   langchain_core.utilsr   r   pydanticr   	getLoggerrA   loggerr   r>   r<   r"   <module>rU      s>     , , = < 3 - ? 			8	$gC gr<   