U
    h                     @  s   d Z ddlmZ 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 dd
lmZ ddlmZ ddlmZ e
ddddG dd deZdS )z Implement an LLM driven browser.    )annotationsN)AnyDictListOptional)
deprecated)CallbackManagerForChainRun)BaseLanguageModel)StrOutputParser)root_validator)Runnable)Chain)PROMPTz0.2.13zImporting NatBotChain from langchain is deprecated and will be removed in langchain 1.0. Please import from langchain_community instead: from langchain_community.chains.natbot import NatBotChain. You may need to pip install -U langchain-community.z1.0)ZsincemessageZremovalc                   @  s  e Zd ZU dZded< ded< dZded< d	Zded
< dZded< dZded< dZ	ded< G dd dZ
edddddddZeddd dddZedddd ddd Zed!d"d#d$Zed!d"d%d&Zd1d'd(d'd)d*d+Zdddd,d-d.Zedd"d/d0ZdS )2NatBotChaina  Implement an LLM driven browser.

    **Security Note**: This toolkit provides code to control a web-browser.

        The web-browser can be used to navigate to:

        - Any URL (including any internal network URLs)
        - And local files

        Exercise care if exposing this chain to end-users. Control who is able to
        access and use this chain, and isolate the network access of the server
        that hosts this chain.

        See https://python.langchain.com/docs/security for more information.

    Example:
        .. code-block:: python

            from langchain.chains import NatBotChain
            natbot = NatBotChain.from_default("Buy me a new hat.")
    r   	llm_chainstr	objectiveNzOptional[BaseLanguageModel]llmurlinput_url_keybrowser_contentinput_browser_content_key previous_commandcommand
output_keyc                   @  s   e Zd ZdZdZdS )zNatBotChain.ConfigTZforbidN)__name__
__module____qualname__Zarbitrary_types_allowedextra r!   r!   @/tmp/pip-unpacked-wheel-bo69hh5q/langchain/chains/natbot/base.pyConfig>   s   r#   T)prer   )valuesreturnc                 C  s@   d|kr<t d d|kr<|d d k	r<t|d B t B |d< |S )Nr   zDirectly instantiating an NatBotChain with an llm is deprecated. Please instantiate with llm_chain argument or using the from_llm class method.r   )warningswarnr   r
   )clsr%   r!   r!   r"   raise_deprecationB   s    zNatBotChain.raise_deprecationr   )r   kwargsr&   c                 K  s   t ddS )zLoad with default LLMChain.zThis method is no longer implemented. Please use from_llm.llm = OpenAI(temperature=0.5, best_of=10, n=3, max_tokens=50)For example, NatBotChain.from_llm(llm, objective)N)NotImplementedError)r)   r   r+   r!   r!   r"   from_defaultN   s    zNatBotChain.from_defaultr	   )r   r   r+   r&   c                 K  s"   t |B t B }| f ||d|S )zLoad from LLM.)r   r   )r   r
   )r)   r   r   r+   r   r!   r!   r"   from_llmW   s    zNatBotChain.from_llmz	List[str])r&   c                 C  s   | j | jgS )z@Expect url and browser content.

        :meta private:
        )r   r   selfr!   r!   r"   
input_keys_   s    zNatBotChain.input_keysc                 C  s   | j gS )z0Return command.

        :meta private:
        )r   r/   r!   r!   r"   output_keysg   s    zNatBotChain.output_keyszDict[str, str]z$Optional[CallbackManagerForChainRun])inputsrun_managerr&   c                 C  sn   |p
t  }|| j }|| j }| jj| j|d d | j|d d dd| id}|	 }|| _| j
|iS )Nd   i  )r   r   r   r   	callbacks)config)r   Zget_noop_managerr   r   r   Zinvoker   r   Z	get_childstripr   )r0   r3   r4   Z_run_managerr   r   Zllm_cmdr!   r!   r"   _callo   s    




	zNatBotChain._call)r   r   r&   c                 C  s   | j || j|i}| || j S )a  Figure out next browser command to run.

        Args:
            url: URL of the site currently on.
            browser_content: Content of the page as currently displayed by the browser.

        Returns:
            Next browser command to run.

        Example:
            .. code-block:: python

                browser_content = "...."
                llm_command = natbot.run("www.google.com", browser_content)
        )r   r   r   )r0   r   r   Z_inputsr!   r!   r"   execute   s      zNatBotChain.executec                 C  s   dS )NZnat_bot_chainr!   r/   r!   r!   r"   _chain_type   s    zNatBotChain._chain_type)N)r   r   r   __doc____annotations__r   r   r   r   r   r#   r   r*   classmethodr-   r.   propertyr1   r2   r9   r:   r;   r!   r!   r!   r"   r      s0   

 r   )r<   
__future__r   r'   typingr   r   r   r   Zlangchain_core._apir   Zlangchain_core.callbacksr   Zlangchain_core.language_modelsr	   Zlangchain_core.output_parsersr
   Zlangchain_core.pydantic_v1r   Zlangchain_core.runnablesr   Zlangchain.chains.baser   Zlangchain.chains.natbot.promptr   r   r!   r!   r!   r"   <module>   s"   
