o
    `+ i5                     @  sj  d 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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rtddl&m'Z' ddh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)Sequence)TYPE_CHECKINGAnyOptional)
deprecated)Document)BaseLanguageModel)BasePromptTemplate)BaseToolTool)Field)AGENT_DEPRECATION_WARNING)AgentAgentExecutorAgentOutputParser)	AgentTypeReActOutputParserTEXTWORLD_PROMPTWIKI_PROMPT)validate_tools_single_input)DocstoreLookupSearchz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$   g/home/app/PaddleOCR-VL-test/.venv_paddleocr/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_prompt1      z ReActDocstoreAgent.create_promptNonec                   sf   t | j| t | t|ttkrd| }t|dd |D }|tkr1d| }t|d S )Nz-Exactly two tools must be specified, but got c                 S     h | ]}|j qS r$   name.0Ztoolr$   r$   r%   	<setcomp>=       z5ReActDocstoreAgent._validate_tools.<locals>.<setcomp>z,Tool names should be Lookup and Search, got )r   __name__super_validate_toolslen_LOOKUP_AND_SEARCH_TOOLS
ValueErrorr#   r,   msgZ
tool_names	__class__r$   r%   r=   6   s   

z"ReActDocstoreAgent._validate_toolsc                 C     dS )z&Prefix to append the observation with.zObservation: r$   r)   r$   r$   r%   observation_prefixB   r2   z%ReActDocstoreAgent.observation_prefix	list[str]c                 C  s   dgS )Nz
Observation:r$   r)   r$   r$   r%   _stopG   r'   zReActDocstoreAgent._stopc                 C  rE   )z#Prefix to append the LLM call with.zThought:r$   r)   r$   r$   r%   
llm_prefixK   r2   zReActDocstoreAgent.llm_prefix)r!   r   r"   r   r"   r(   r,   r-   r"   r
   r,   r-   r"   r3   r"   rG   )r;   
__module____qualname____doc__r   r   r    __annotations__classmethodr&   propertyr+   r1   r=   rF   rH   rI   __classcell__r$   r$   rC   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   )rV   document
lookup_strlookup_index)r*   rV   r$   r$   r%   __init__Y   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)rV   search
isinstancer   rX   _summary)r*   r\   resultr$   r$   r%   r]   `   s   
zDocstoreExplorer.searchc                   s    j du rd}t||  jkr|  _d _n  jd7  _ fdd jD }t|dkr4d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$   )rY   lower)r8   pr)   r$   r%   
<listcomp>s   s    z+DocstoreExplorer.lookup.<locals>.<listcomp>z
No ResultszNo More Resultsz(Result /) )rX   r@   rb   rY   rZ   _paragraphsr>   )r*   r\   rB   lookupsZresult_prefixr$   r)   r%   lookupi   s   

zDocstoreExplorer.lookupc                 C  s
   | j d S )Nr   )rh   r)   r$   r$   r%   r_   {   s   
zDocstoreExplorer._summaryrG   c                 C  s$   | j d u rd}t|| j jdS )Nz(Cannot get paragraphs without a documentz

)rX   r@   Zpage_contentsplit)r*   rB   r$   r$   r%   rh      s   
zDocstoreExplorer._paragraphsN)rV   r   )r\   r(   r"   r(   rJ   rM   )
r;   rN   rO   rP   r[   r]   rj   rS   r_   rh   r$   r$   r$   r%   rU   Q   s    


	rU   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   r0   r$   r$   r%   r1      r2   z!ReActTextWorldAgent.create_promptr3   c                   sd   t | j| t | t|dkrd| }t|dd |D }|dhkr0d| }t|d S )Nra   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   r;   r<   r=   r>   r@   rA   rC   r$   r%   r=      s   


z#ReActTextWorldAgent._validate_toolsrK   rL   )r;   rN   rO   rP   rR   r1   r=   rT   r$   r$   rC   r%   rl      s    rl   c                      s"   e Zd ZdZd
 fdd	Z  ZS )
ReActChainz3[Deprecated] Chain that implements the ReAct paper.llmr	   rV   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$   )rU   r   r]   rj   r   Zfrom_llm_and_toolsr<   r[   )r*   rn   rV   r!   Zdocstore_explorerr,   rq   rC   r$   r%   r[      s   zReActChain.__init__)rn   r	   rV   r   r!   r   )r;   rN   rO   rP   r[   rT   r$   r$   rC   r%   rm      s    rm   N)-rP   
__future__r   collections.abcr   typingr   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?   r   rU   rl   rm   r$   r$   r$   r%   <module>   sX    .1