U
    h                     @  sJ  d Z ddlmZ ddlmZ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mZ dd
l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# erddl$m%Z% e
dddG dd deZ&e
dddG dd dZ'e
dddG dd de&Z(e
dddG dd deZ)dS )zPChain that implements the ReAct paper from https://arxiv.org/pdf/2210.03629.pdf.    )annotations)TYPE_CHECKINGAnyListOptionalSequence)
deprecated)Document)BaseLanguageModel)BasePromptTemplate)Field)BaseToolTool)AgentAgentExecutorAgentOutputParser)	AgentTypeReActOutputParserTEXTWORLD_PROMPTWIKI_PROMPT)validate_tools_single_input)Docstorez0.1.0z1.0)Zremovalc                      s   e Zd ZU dZeedZded< edddddZ	e
d	d
ddZedddddZeddd fddZe
d	d
ddZe
dd
ddZe
d	d
ddZ  ZS )ReActDocstoreAgentzAgent for the ReAct chain.)default_factoryr   output_parserr   )kwargsreturnc                 K  s   t  S )Nr   )clsr    r!   ?/tmp/pip-unpacked-wheel-bo69hh5q/langchain/agents/react/base.py_get_default_output_parser   s    z-ReActDocstoreAgent._get_default_output_parserstrr   c                 C  s   t jS )z#Return Identifier of an agent type.)r   ZREACT_DOCSTOREselfr!   r!   r"   _agent_type#   s    zReActDocstoreAgent._agent_typeSequence[BaseTool]r   toolsr   c                 C  s   t S zReturn default prompt.r   r    r+   r!   r!   r"   create_prompt(   s    z ReActDocstoreAgent.create_promptNonec                   s^   t | j| t | t|dkr2td| dd |D }|ddhkrZtd| d S )N   z-Exactly two tools must be specified, but got c                 S  s   h | ]
}|j qS r!   name.0Ztoolr!   r!   r"   	<setcomp>3   s     z5ReActDocstoreAgent._validate_tools.<locals>.<setcomp>LookupSearchz,Tool names should be Lookup and Search, got r   __name__super_validate_toolslen
ValueErrorr    r+   Z
tool_names	__class__r!   r"   r;   -   s    z"ReActDocstoreAgent._validate_toolsc                 C  s   dS )z&Prefix to append the observation with.zObservation: r!   r&   r!   r!   r"   observation_prefix9   s    z%ReActDocstoreAgent.observation_prefix	List[str]c                 C  s   dgS )Nz
Observation:r!   r&   r!   r!   r"   _stop>   s    zReActDocstoreAgent._stopc                 C  s   dS )z#Prefix to append the LLM call with.zThought:r!   r&   r!   r!   r"   
llm_prefixB   s    zReActDocstoreAgent.llm_prefix)r9   
__module____qualname____doc__r   r   r   __annotations__classmethodr#   propertyr(   r.   r;   rA   rC   rD   __classcell__r!   r!   r?   r"   r      s    
r   c                   @  sb   e Zd ZdZddddZddddd	Zdddd
dZeddddZeddddZ	dS )DocstoreExplorerz5Class to assist with exploration of a document store.r   )docstorec                 C  s   || _ d| _d| _d| _dS )z=Initialize with a docstore, and set initial document to None.N r   )rM   document
lookup_strlookup_index)r'   rM   r!   r!   r"   __init__L   s    zDocstoreExplorer.__init__r$   )termr   c                 C  s0   | j |}t|tr"|| _| jS d| _|S dS )z5Search for a term in the docstore, and if found save.N)rM   search
isinstancer	   rO   _summary)r'   rS   resultr!   r!   r"   rT   S   s    
zDocstoreExplorer.searchc                   s    j dkrtd|  jkr2|  _d _n  jd7  _ fdd jD }t|dkrddS  jt|krvdS d	 jd  d
t| d}| d| j  S dS )z%Lookup a term in document (if saved).Nz/Cannot lookup without a successful search firstr      c                   s   g | ]} j | kr|qS r!   )rP   lower)r4   pr&   r!   r"   
<listcomp>f   s      z+DocstoreExplorer.lookup.<locals>.<listcomp>z
No ResultszNo More Resultsz(Result /) )rO   r=   rY   rP   rQ   _paragraphsr<   )r'   rS   ZlookupsZresult_prefixr!   r&   r"   lookup]   s    

zDocstoreExplorer.lookupr%   c                 C  s
   | j d S )Nr   )r_   r&   r!   r!   r"   rV   o   s    zDocstoreExplorer._summaryrB   c                 C  s    | j d krtd| j jdS )Nz(Cannot get paragraphs without a documentz

)rO   r=   Zpage_contentsplitr&   r!   r!   r"   r_   s   s    
zDocstoreExplorer._paragraphsN)
r9   rE   rF   rG   rR   rT   r`   rJ   rV   r_   r!   r!   r!   r"   rL   H   s   
rL   c                      s@   e Zd ZdZedddddZeddd fdd	Z  ZS )
ReActTextWorldAgentz$Agent for the ReAct TextWorld chain.r)   r   r*   c                 C  s   t S r,   r   r-   r!   r!   r"   r.   ~   s    z!ReActTextWorldAgent.create_promptr/   c                   s\   t | j| t | t|dkr2td| dd |D }|dhkrXtd| d S )NrX   z,Exactly one tool must be specified, but got c                 S  s   h | ]
}|j qS r!   r1   r3   r!   r!   r"   r5      s     z6ReActTextWorldAgent._validate_tools.<locals>.<setcomp>ZPlayzTool name should be Play, got r8   r>   r?   r!   r"   r;      s    
z#ReActTextWorldAgent._validate_tools)r9   rE   rF   rG   rI   r.   r;   rK   r!   r!   r?   r"   rb   z   s
   rb   c                      s*   e Zd ZdZdddd fddZ  ZS )
ReActChainz3[Deprecated] Chain that implements the ReAct paper.r
   r   r   )llmrM   r   c                   sP   t |}td|jddtd|jddg}t||}t jf ||d| dS )z'Initialize with the LLM and a docstore.r7   z"Search for a term in the docstore.)r2   funcdescriptionr6   zLookup a term in the docstore.)agentr+   N)rL   r   rT   r`   r   Zfrom_llm_and_toolsr:   rR   )r'   rd   rM   r   Zdocstore_explorerr+   rg   r?   r!   r"   rR      s    zReActChain.__init__)r9   rE   rF   rG   rR   rK   r!   r!   r?   r"   rc      s   rc   N)*rG   
__future__r   typingr   r   r   r   r   Zlangchain_core._apir   Zlangchain_core.documentsr	   Zlangchain_core.language_modelsr
   Zlangchain_core.promptsr   Zlangchain_core.pydantic_v1r   Zlangchain_core.toolsr   r   Zlangchain.agents.agentr   r   r   Zlangchain.agents.agent_typesr   Z$langchain.agents.react.output_parserr   Z'langchain.agents.react.textworld_promptr   Z"langchain.agents.react.wiki_promptr   Zlangchain.agents.utilsr   Z!langchain_community.docstore.baser   r   rL   rb   rc   r!   r!   r!   r"   <module>   s0   
.
1

