Source code for langchain_community.chains.llm_requests
"""Chain that hits a URL and then uses an LLM to parse results."""from__future__importannotationsfromtypingimportAny,Dict,List,Optionalfromlangchain.chainsimportLLMChainfromlangchain.chains.baseimportChainfromlangchain_core.callbacksimportCallbackManagerForChainRunfrompydanticimportConfigDict,Field,model_validatorfromlangchain_community.utilities.requestsimportTextRequestsWrapperDEFAULT_HEADERS={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"# noqa: E501}
[docs]classLLMRequestsChain(Chain):"""Chain that requests a URL and then uses an LLM to parse results. **Security Note**: This chain can make GET requests to arbitrary URLs, including internal URLs. Control access to who can run this chain and what network access this chain has. See https://python.langchain.com/docs/security for more information. """llm_chain:LLMChain# type: ignore[valid-type]requests_wrapper:TextRequestsWrapper=Field(default_factory=lambda:TextRequestsWrapper(headers=DEFAULT_HEADERS),exclude=True,)text_length:int=8000requests_key:str="requests_result"#: :meta private:input_key:str="url"#: :meta private:output_key:str="output"#: :meta private:model_config=ConfigDict(arbitrary_types_allowed=True,extra="forbid",)@propertydefinput_keys(self)->List[str]:"""Will be whatever keys the prompt expects. :meta private: """return[self.input_key]@propertydefoutput_keys(self)->List[str]:"""Will always return text key. :meta private: """return[self.output_key]@model_validator(mode="before")@classmethoddefvalidate_environment(cls,values:Dict)->Any:"""Validate that api key and python package exists in environment."""try:frombs4importBeautifulSoup# noqa: F401exceptImportError:raiseImportError("Could not import bs4 python package. ""Please install it with `pip install bs4`.")returnvaluesdef_call(self,inputs:Dict[str,Any],run_manager:Optional[CallbackManagerForChainRun]=None,)->Dict[str,Any]:frombs4importBeautifulSoup_run_manager=run_managerorCallbackManagerForChainRun.get_noop_manager()# Other keys are assumed to be needed for LLM predictionother_keys={k:vfork,vininputs.items()ifk!=self.input_key}url=inputs[self.input_key]res=self.requests_wrapper.get(url)# extract the text from the htmlsoup=BeautifulSoup(res,"html.parser")other_keys[self.requests_key]=soup.get_text()[:self.text_length]result=self.llm_chain.predict(# type: ignore[attr-defined]callbacks=_run_manager.get_child(),**other_keys)return{self.output_key:result}@propertydef_chain_type(self)->str:return"llm_requests_chain"