
    7|h                         d dl Z d dlmZmZmZmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZ d dlmZ  e j&                  e      Z G d	 d
e      Zy)    N)AnyDictListOptional)CallbackManagerForLLMRun)BaseLanguageModel)LLM)	AIMessage)get_from_dict_or_envpre_init)
ConfigDictc                       e Zd ZU dZeed<   	  ed      Zede	de	fd       Z
	 	 dd	ed
eee      dee   dedef
dZedefd       Zy)OpaquePromptsaj  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                     	 ddl }|j                  t        d      t	        |ddd      }|st        d	      |S # t        $ r t        d      w xY w)
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       e/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/llms/opaqueprompts.pyvalidate_environmentz"OpaquePrompts.validate_environment(   s    	& >>!5 
 '+-Db
 5  +  	F 	s	   ; ANpromptstoprun_managerkwargsc                 \   ddl }|xs t        j                         }|j                  |g      }|j                  d   }| j
                  j                  |      j                  |      }	t        |	t              r|	j                  }	|j                  |	|j                        }
|
j                  S )aC  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!   _callzOpaquePrompts._callD   s    ( 	#"Q&>&O&O&Q 24fX1F%6%F%Fq%I" }}))t)4;;&
 lI.'//L 68]],;; 6C 6
 #333    c                      y)zSReturn type of LLM.

        This is an override of the base class method.
        r    )r2   s    r!   	_llm_typezOpaquePrompts._llm_typeo   s     r9   )NN)__name__
__module____qualname____doc__r   __annotations__r   model_configr   r   r"   strr   r   r   r   r8   propertyr<   r;   r9   r!   r   r      s    $  L $ 4  < %):>	)4)4 tCy!)4 67	)4
 )4 
)4V 3  r9   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   	getLoggerr=   loggerr   r;   r9   r!   <module>rO      s@     , , = < 3 - ? 			8	$gC gr9   