
    Ng                         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dS )    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dS )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 }n# t          $ r t          d          w xY w|j        t          d          t	          |ddd          }|st          d	          |S )
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       b/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/llms/opaqueprompts.pyvalidate_environmentz"OpaquePrompts.validate_environment(   s    	&&&&& 	 	 	F  	
 >!5  
 '+-Db
 
 
  	5   s    !Npromptstoprun_managerkwargsc                 T   ddl }|pt          j                    }|                    |g          }|j        d         }| j                            |                              |          }	t          |	t                    r|	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&Q&Q 24fX1F1F%6%Fq%I" }))t)44;;&
 
 lI.. 	0'/L 68]],; 6C 6
 6
 #33    c                     dS )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    X< %):>	)4 )4)4 tCy!)4 67	)4
 )4 
)4 )4 )4 )4V 3    X  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 3 3 3 3 3 - - - - - - ? ? ? ? ? ? ? ?      		8	$	$g g g g gC g g g g gr9   