"""Implement an LLM driven browser."""from__future__importannotationsimportwarningsfromtypingimportAny,Dict,List,Optionalfromlangchain_core._apiimportdeprecatedfromlangchain_core.cachesimportBaseCacheasBaseCachefromlangchain_core.callbacksimportCallbackManagerForChainRunfromlangchain_core.callbacksimportCallbacksasCallbacksfromlangchain_core.language_modelsimportBaseLanguageModelfromlangchain_core.output_parsersimportStrOutputParserfromlangchain_core.runnablesimportRunnablefrompydanticimportConfigDict,model_validatorfromlangchain.chains.baseimportChainfromlangchain.chains.natbot.promptimportPROMPT
[docs]@deprecated(since="0.2.13",message=("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."),removal="1.0",)classNatBotChain(Chain):"""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.") """llm_chain:Runnableobjective:str"""Objective that NatBot is tasked with completing."""llm:Optional[BaseLanguageModel]=None"""[Deprecated] LLM wrapper to use."""input_url_key:str="url"#: :meta private:input_browser_content_key:str="browser_content"#: :meta private:previous_command:str=""#: :meta private:output_key:str="command"#: :meta private:model_config=ConfigDict(arbitrary_types_allowed=True,extra="forbid",)@model_validator(mode="before")@classmethoddefraise_deprecation(cls,values:Dict)->Any:if"llm"invalues:warnings.warn("Directly instantiating an NatBotChain with an llm is deprecated. ""Please instantiate with llm_chain argument or using the from_llm ""class method.")if"llm_chain"notinvaluesandvalues["llm"]isnotNone:values["llm_chain"]=PROMPT|values["llm"]|StrOutputParser()returnvalues
[docs]@classmethoddeffrom_default(cls,objective:str,**kwargs:Any)->NatBotChain:"""Load with default LLMChain."""raiseNotImplementedError("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)")
[docs]@classmethoddeffrom_llm(cls,llm:BaseLanguageModel,objective:str,**kwargs:Any)->NatBotChain:"""Load from LLM."""llm_chain=PROMPT|llm|StrOutputParser()returncls(llm_chain=llm_chain,objective=objective,**kwargs)
[docs]defexecute(self,url:str,browser_content:str)->str:"""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) """_inputs={self.input_url_key:url,self.input_browser_content_key:browser_content,}returnself(_inputs)[self.output_key]