o
    Zh                     @  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)messageZ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#   R/var/www/html/lang_env/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   Z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.0Z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+   Z
tool_names	__class__r#   r$   r@   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>   r1   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  rF   )z#Prefix to append the LLM call with.zThought:r#   r(   r#   r#   r$   
llm_prefixG   r1   zReActDocstoreAgent.llm_prefix)r    r   r!   r   r!   r'   r+   r,   r!   r   r+   r,   r!   r2   r!   rH   )r>   
__module____qualname____doc__r   r   r   __annotations__classmethodr%   propertyr*   r0   r@   rG   rI   rJ   __classcell__r#   r#   rD   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   )rW   document
lookup_strlookup_index)r)   rW   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)rW   search
isinstancer	   rY   _summary)r)   r]   resultr#   r#   r$   r^   \   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#   )rZ   lower)r8   pr(   r#   r$   
<listcomp>o   s    z+DocstoreExplorer.lookup.<locals>.<listcomp>z
No ResultszNo More Resultsz(Result /) )rY   rB   rc   rZ   r[   _paragraphsrA   )r)   r]   lookupsZresult_prefixr#   r(   r$   lookupf   s   

zDocstoreExplorer.lookupc                 C  s
   | j d S )Nr   )ri   r(   r#   r#   r$   r`   x   s   
zDocstoreExplorer._summaryrH   c                 C  s    | j d u r	td| j jdS )Nz(Cannot get paragraphs without a documentz

)rY   rB   Zpage_contentsplitr(   r#   r#   r$   ri   |   s   
zDocstoreExplorer._paragraphsN)rW   r   )r]   r'   r!   r'   rK   rN   )
r>   rO   rP   rQ   r\   r^   rk   rT   r`   ri   r#   r#   r#   r$   rV   M   s    



rV   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-   r.   r   r/   r#   r#   r$   r0      r1   z!ReActTextWorldAgent.create_promptr2   c                   s\   t | j| t | t|dkrtd| dd |D }|dhkr,td| d S )Nrb   z,Exactly one tool must be specified, but got c                 S  r4   r#   r5   r7   r#   r#   r$   r9      r:   z6ReActTextWorldAgent._validate_tools.<locals>.<setcomp>ZPlayzTool name should be Play, got r=   rC   rD   r#   r$   r@      s   
z#ReActTextWorldAgent._validate_toolsrL   rM   )r>   rO   rP   rQ   rS   r0   r@   rU   r#   r#   rD   r$   rm      s    rm   c                      s"   e Zd ZdZd
 fdd	Z  ZS )
ReActChainz3[Deprecated] Chain that implements the ReAct paper.llmr
   rW   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.)r6   funcdescriptionr;   zLookup a term in the docstore.)agentr+   Nr#   )rV   r   r^   rk   r   Zfrom_llm_and_toolsr?   r\   )r)   ro   rW   r    Zdocstore_explorerr+   rr   rD   r#   r$   r\      s   zReActChain.__init__)ro   r
   rW   r   r    r   )r>   rO   rP   rQ   r\   rU   r#   r#   rD   r$   rn      s    rn   N),rQ   
__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.toolsr   r   Zpydanticr   Zlangchain._api.deprecationr   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   rV   rm   rn   r#   r#   r#   r$   <module>   sT    .1