o
    粪g                     @  s^  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mZ dd	lmZ dd
l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rrddl&m'Z' e
deddG dd deZ(e
deddG dd dZ)e
deddG dd de(Z*e
de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)BaseToolTool)Field)AGENT_DEPRECATION_WARNING)AgentAgentExecutorAgentOutputParser)	AgentTypeReActOutputParserTEXTWORLD_PROMPTWIKI_PROMPT)validate_tools_single_input)Docstorez0.1.0z1.0)messageremovalc                      s   e Zd ZU dZeedZded< eddd	Z	e
dddZedddZed fddZe
dddZe
d ddZe
dddZ  ZS )!ReActDocstoreAgentzAgent for the ReAct chain.)default_factoryr   output_parserkwargsr   returnc                 K  s   t  S )Nr   )clsr!    r$   W/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/langchain/agents/react/base.py_get_default_output_parser$      z-ReActDocstoreAgent._get_default_output_parserstrc                 C  s   t jS )z#Return Identifier of an agent type.)r   REACT_DOCSTOREselfr$   r$   r%   _agent_type(   s   zReActDocstoreAgent._agent_typetoolsSequence[BaseTool]r   c                 C     t S zReturn default prompt.r   r#   r-   r$   r$   r%   create_prompt-      z ReActDocstoreAgent.create_promptNonec                   s^   t | j| t | t|dkrtd| dd |D }|ddhkr-td| d S )N   z-Exactly two tools must be specified, but got c                 S     h | ]}|j qS r$   name.0toolr$   r$   r%   	<setcomp>8       z5ReActDocstoreAgent._validate_tools.<locals>.<setcomp>LookupSearchz,Tool names should be Lookup and Search, got r   __name__super_validate_toolslen
ValueErrorr#   r-   
tool_names	__class__r$   r%   rC   2   s   z"ReActDocstoreAgent._validate_toolsc                 C     dS )z&Prefix to append the observation with.zObservation: r$   r*   r$   r$   r%   observation_prefix>   r3   z%ReActDocstoreAgent.observation_prefix	List[str]c                 C  s   dgS )Nz
Observation:r$   r*   r$   r$   r%   _stopC   r'   zReActDocstoreAgent._stopc                 C  rJ   )z#Prefix to append the LLM call with.zThought:r$   r*   r$   r$   r%   
llm_prefixG   r3   zReActDocstoreAgent.llm_prefix)r!   r   r"   r   r"   r(   r-   r.   r"   r   r-   r.   r"   r4   r"   rL   )rA   
__module____qualname____doc__r   r   r    __annotations__classmethodr&   propertyr,   r2   rC   rK   rM   rN   __classcell__r$   r$   rH   r%   r      s"   
 r   c                   @  sJ   e Zd ZdZdddZdd	d
ZdddZedddZedddZ	dS )DocstoreExplorerz5Class to assist with exploration of a document store.docstorer   c                 C  s   || _ d| _d| _d| _dS )z=Initialize with a docstore, and set initial document to None.N r   )r[   document
lookup_strlookup_index)r+   r[   r$   r$   r%   __init__U   s   
zDocstoreExplorer.__init__termr(   r"   c                 C  s,   | j |}t|tr|| _| jS d| _|S )z5Search for a term in the docstore, and if found save.N)r[   search
isinstancer	   r]   _summary)r+   ra   resultr$   r$   r%   rb   \   s   
zDocstoreExplorer.searchc                   s    j du r	td|  jkr|  _d _n  jd7  _ fdd jD }t|dkr2dS  jt|kr;dS d	 jd  d
t| d}| d| j  S )z%Lookup a term in document (if saved).Nz/Cannot lookup without a successful search firstr      c                   s   g | ]} j | v r|qS r$   )r^   lower)r:   pr*   r$   r%   
<listcomp>o   s    z+DocstoreExplorer.lookup.<locals>.<listcomp>z
No ResultszNo More Resultsz(Result /) )r]   rE   rg   r^   r_   _paragraphsrD   )r+   ra   lookupsresult_prefixr$   r*   r%   lookupf   s   

zDocstoreExplorer.lookupc                 C  s
   | j d S )Nr   )rm   r*   r$   r$   r%   rd   x   s   
zDocstoreExplorer._summaryrL   c                 C  s    | j d u r	td| j jdS )Nz(Cannot get paragraphs without a documentz

)r]   rE   page_contentsplitr*   r$   r$   r%   rm   |   s   
zDocstoreExplorer._paragraphsN)r[   r   )ra   r(   r"   r(   rO   rR   )
rA   rS   rT   rU   r`   rb   rp   rX   rd   rm   r$   r$   r$   r%   rZ   M   s    



rZ   c                      s4   e Zd ZdZedddZed fd	d
Z  ZS )ReActTextWorldAgentz$Agent for the ReAct TextWorld chain.r-   r.   r"   r   c                 C  r/   r0   r   r1   r$   r$   r%   r2      r3   z!ReActTextWorldAgent.create_promptr4   c                   s\   t | j| t | t|dkrtd| dd |D }|dhkr,td| d S )Nrf   z,Exactly one tool must be specified, but got c                 S  r6   r$   r7   r9   r$   r$   r%   r<      r=   z6ReActTextWorldAgent._validate_tools.<locals>.<setcomp>PlayzTool name should be Play, got r@   rF   rH   r$   r%   rC      s   
z#ReActTextWorldAgent._validate_toolsrP   rQ   )rA   rS   rT   rU   rW   r2   rC   rY   r$   r$   rH   r%   rs      s    rs   c                      s"   e Zd ZdZd
 fdd	Z  ZS )
ReActChainz3[Deprecated] Chain that implements the ReAct paper.llmr
   r[   r   r!   r   c                   sP   t |}td|jddtd|jddg}t||}t jd||d| dS )	z'Initialize with the LLM and a docstore.r?   z"Search for a term in the docstore.)r8   funcdescriptionr>   zLookup a term in the docstore.)agentr-   Nr$   )rZ   r   rb   rp   r   from_llm_and_toolsrB   r`   )r+   rv   r[   r!   docstore_explorerr-   ry   rH   r$   r%   r`      s   zReActChain.__init__)rv   r
   r[   r   r!   r   )rA   rS   rT   rU   r`   rY   r$   r$   rH   r%   ru      s    ru   N),rU   
__future__r   typingr   r   r   r   r   langchain_core._apir   langchain_core.documentsr	   langchain_core.language_modelsr
   langchain_core.promptsr   langchain_core.toolsr   r   pydanticr   langchain._api.deprecationr   langchain.agents.agentr   r   r   langchain.agents.agent_typesr   $langchain.agents.react.output_parserr   'langchain.agents.react.textworld_promptr   "langchain.agents.react.wiki_promptr   langchain.agents.utilsr   !langchain_community.docstore.baser   r   rZ   rs   ru   r$   r$   r$   r%   <module>   sT    .1