U
    hz                     @  s   d Z ddlm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mZ ddlmZ d	d
iZG dd deZdS )z<Chain that hits a URL and then uses an LLM to parse results.    )annotations)AnyDictListOptional)LLMChain)Chain)CallbackManagerForChainRun)Fieldroot_validator)TextRequestsWrapperz
User-AgentzrMozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36c                   @  s   e Zd ZU dZded< edd 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ZeddddZeddddddd Zd)d"d#d"d$d%d&Zeddd'd(Zd!S )*LLMRequestsChainab  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.
    r   	llm_chainc                   C  s
   t tdS )N)headers)r   DEFAULT_HEADERS r   r   K/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/chains/llm_requests.py<lambda>!       zLLMRequestsChain.<lambda>T)default_factoryexcluder   requests_wrapperi@  inttext_lengthZrequests_resultstrrequests_keyurl	input_keyoutput
output_keyc                   @  s   e Zd ZdZdZdS )zLLMRequestsChain.ConfigTZforbidN)__name__
__module____qualname__Zarbitrary_types_allowedextrar   r   r   r   Config)   s   r$   z	List[str])returnc                 C  s   | j gS )zJWill be whatever keys the prompt expects.

        :meta private:
        r   selfr   r   r   
input_keys-   s    zLLMRequestsChain.input_keysc                 C  s   | j gS )z=Will always return text key.

        :meta private:
        )r   r'   r   r   r   output_keys5   s    zLLMRequestsChain.output_keys)prer   )valuesr%   c                 C  s2   zddl m} W n tk
r,   tdY nX |S )z?Validate that api key and python package exists in environment.r   BeautifulSoupzNCould not import bs4 python package. Please install it with `pip install bs4`.)bs4r.   ImportError)clsr,   r.   r   r   r   validate_environment=   s    
z%LLMRequestsChain.validate_environmentNzDict[str, Any]z$Optional[CallbackManagerForChainRun])inputsrun_managerr%   c           
        s   ddl m} |pt } fdd| D }| j } j|}||d}| d  j	 | j
<  jjf d| i|}	 j|	iS )Nr   r-   c                   s    i | ]\}}| j kr||qS r   r&   ).0kvr'   r   r   
<dictcomp>S   s     
  z*LLMRequestsChain._call.<locals>.<dictcomp>zhtml.parser	callbacks)r/   r.   r	   Zget_noop_manageritemsr   r   getZget_textr   r   r   ZpredictZ	get_childr   )
r(   r3   r4   r.   Z_run_managerZ
other_keysr   resZsoupresultr   r'   r   _callJ   s    


zLLMRequestsChain._callc                 C  s   dS )NZllm_requests_chainr   r'   r   r   r   _chain_type^   s    zLLMRequestsChain._chain_type)N)r    r!   r"   __doc____annotations__r
   r   r   r   r   r   r$   propertyr)   r*   r   r2   r>   r?   r   r   r   r   r      s*   
 r   N)r@   
__future__r   typingr   r   r   r   Zlangchain.chainsr   Zlangchain.chains.baser   Zlangchain_core.callbacksr	   Zlangchain_core.pydantic_v1r
   r   Z&langchain_community.utilities.requestsr   r   r   r   r   r   r   <module>   s    