a
    ag9                     @  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mZ ddlmZ ddlmZ e
ddddG dd deZe  dS )z Implement an LLM driven browser.    )annotationsN)AnyDictListOptional)
deprecated)	BaseCache)CallbackManagerForChainRun)	Callbacks)BaseLanguageModel)StrOutputParser)Runnable)
ConfigDictmodel_validator)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< 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)moder   r   )valuesreturnc                 C  s@   d|v r<t d d|vr<|d d u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(   j/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain/chains/natbot/base.pyraise_deprecationE   s    zNatBotChain.raise_deprecation)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_defaultR   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_keysc   s    zNatBotChain.input_keysc                 C  s   | j gS )z0Return command.

        :meta private:
        )r   r/   r(   r(   r)   output_keysk   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)   _calls   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   r   r   Zlangchain_core._apir   Zlangchain_core.cachesr   Zlangchain_core.callbacksr	   r
   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   Zmodel_rebuildr(   r(   r(   r)   <module>   s*   
 