a
    •˜ëjê  ã                   @  s®   d Z ddlmZ ddlZddl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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ÚOptional)Ú
deprecated)ÚCallbackManagerForChainRun)ÚBaseLanguageModel)ÚStrOutputParser)ÚRunnable)Ú
ConfigDictÚmodel_validator)ÚChain)ÚPROMPTz0.2.13z÷Importing 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< e
ddd�Zedd�e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d2d(d)d(d*œd+d,„Zdddd-œd.d/„Zedd#œd0d1„ƒZdS )3Ú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_keyTZforbid)Zarbitrary_types_allowedÚextraÚbefore)ÚmodeÚdictr   )ÚvaluesÚreturnc                 C  sD   d|v r@t jddd� d|vr@|d d ur@t|d B tƒ B |d< |S )Nr   z�Directly instantiating an NatBotChain with an llm is deprecated. Please instantiate with llm_chain argument or using the from_llm class method.é   )Ú
stacklevelr   )ÚwarningsÚwarnr   r   )Úclsr    © r'   ú[/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/chains/natbot/base.pyÚ_raise_deprecationE   s    üzNatBotChain._raise_deprecation)r   Úkwargsr!   c                 K  s   d}t |ƒ‚dS )zLoad with default LLMChain.z¨This 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*   Úmsgr'   r'   r(   Úfrom_defaultS   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_llm]   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_keysh   s    zNatBotChain.input_keysc                 C  s   | j gS )z0Return command.

        :meta private:
        )r   r/   r'   r'   r(   Úoutput_keysp   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 )Néd   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(   Ú_callx   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)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   r   r   r   r   r
   Zmodel_configr   Úclassmethodr)   r-   r.   Úpropertyr1   r2   r9   r:   r;   r'   r'   r'   r(   r      s8   
þ	

 ýr   )r?   Ú
__future__r   r$   Útypingr   r   Zlangchain_core._apir   Zlangchain_core.callbacksr   Zlangchain_core.language_modelsr   Zlangchain_core.output_parsersr   Zlangchain_core.runnablesr	   Zpydanticr
   r   Zlangchain.chains.baser   Zlangchain.chains.natbot.promptr   r   r'   r'   r'   r(   Ú<module>   s"   ø
