§
    è˜ºh¨  ã                  ó„  — d Z ddlmZ ddlmZ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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'  G d„ de¦  «        Z( eddd¬¦  «         G d„ de¦  «        ¦   «         Z) edd¬¦  «         G d„ de¦  «        ¦   «         Z*dS )zOAttempt to implement MRKL systems as described in arxiv.org/pdf/2205.00445.pdf.é    )Úannotations)ÚAnyÚCallableÚListÚ
NamedTupleÚOptionalÚSequence)Ú
deprecated)ÚBaseLanguageModel)ÚPromptTemplate)ÚField)ÚBaseTool)ÚAgentÚAgentExecutorÚAgentOutputParser)Ú	AgentType©ÚMRKLOutputParser)ÚFORMAT_INSTRUCTIONSÚPREFIXÚSUFFIX)ÚTool)Úvalidate_tools_single_input)ÚBaseCallbackManager)ÚLLMChainc                  ó2   — e Zd ZU dZded<   ded<   ded<   dS )ÚChainConfigzÇConfiguration for chain to use in MRKL system.

    Args:
        action_name: Name of the action.
        action: Action function to call.
        action_description: Description of the action.
    ÚstrÚaction_namer   ÚactionÚaction_descriptionN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__© ó    úb/var/www/html/services/rag-service/venv/lib/python3.11/site-packages/langchain/agents/mrkl/base.pyr   r      sB   € € € € € € ðð ð ÐÐÑØÐÐÑØÐÐÑÐÐr(   r   z0.1.0Úcreate_react_agentz0.2.0)ÚalternativeÚremovalc                  ó   ‡ — e Zd ZU dZ ee¬¦  «        Zded<   ed d„¦   «         Z	e
d!d
„¦   «         Ze
d!d„¦   «         Ze
d!d„¦   «         Zeeeedfd"d„¦   «         Zeddeeedfd#d„¦   «         Zed$ˆ fd„¦   «         Zˆ xZS )%ÚZeroShotAgentzAgent for the MRKL chain.)Údefault_factoryr   Úoutput_parserÚkwargsr   Úreturnc                ó   — t          ¦   «         S )Nr   )Úclsr1   s     r)   Ú_get_default_output_parserz(ZeroShotAgent._get_default_output_parser*   s   € åÑ!Ô!Ð!r(   r   c                ó   — t           j        S )z Return Identifier of agent type.)r   ÚZERO_SHOT_REACT_DESCRIPTION©Úselfs    r)   Ú_agent_typezZeroShotAgent._agent_type.   s   € õ Ô4Ð4r(   c                ó   — dS )z&Prefix to append the observation with.zObservation: r'   r8   s    r)   Úobservation_prefixz ZeroShotAgent.observation_prefix3   s	   € ð ˆr(   c                ó   — dS )z#Prefix to append the llm call with.zThought:r'   r8   s    r)   Ú
llm_prefixzZeroShotAgent.llm_prefix8   s	   € ð ˆzr(   NÚtoolsúSequence[BaseTool]ÚprefixÚsuffixÚformat_instructionsÚinput_variablesúOptional[List[str]]r   c                ó
  — d                      d„ |D ¦   «         ¦  «        }d                      d„ |D ¦   «         ¦  «        }|                     |¬¦  «        }d                      ||||g¦  «        }|€dd	g}t          ||¬
¦  «        S )aà  Create prompt in the style of the zero shot agent.

        Args:
            tools: List of tools the agent will have access to, used to format the
                prompt.
            prefix: String to put before the list of tools.
            suffix: String to put after the list of tools.
            input_variables: List of input variables the final prompt will expect.

        Returns:
            A PromptTemplate with the template assembled from the pieces here.
        ú
c                ó0   — g | ]}|j         › d |j        › �‘ŒS )z: )ÚnameÚdescription©Ú.0Útools     r)   ú
<listcomp>z/ZeroShotAgent.create_prompt.<locals>.<listcomp>R   s+   € Ð!WÐ!WÐ!WÈ T¤YÐ"DÐ"D°$Ô2BÐ"DÐ"DÐ!WÐ!WÐ!Wr(   z, c                ó   — g | ]	}|j         ‘Œ
S r'   ©rI   rK   s     r)   rN   z/ZeroShotAgent.create_prompt.<locals>.<listcomp>S   s   € Ð<Ð<Ð<¨d ¤	Ð<Ð<Ð<r(   )Ú
tool_namesz

NÚinputÚagent_scratchpad)ÚtemplaterD   )ÚjoinÚformatr   )	r4   r?   rA   rB   rC   rD   Útool_stringsrQ   rT   s	            r)   Úcreate_promptzZeroShotAgent.create_prompt=   s›   € ð* —y’yÐ!WÐ!WÐQVÐ!WÑ!WÔ!WÑXÔXˆØ—Y’YÐ<Ð<°eÐ<Ñ<Ô<Ñ=Ô=ˆ
Ø1×8Ò8ÀJÐ8ÑOÔOÐØ—;’; ¨Ð6IÈ6ÐRÑSÔSˆØÐ"Ø&Ð(:Ð;ˆOÝ xÀÐQÑQÔQÐQr(   Úllmr   Úcallback_managerúOptional[BaseCallbackManager]úOptional[AgentOutputParser]r   c	                óà   — |                       |¦  «         |                      |||||¬¦  «        }
t          ||
|¬¦  «        }d„ |D ¦   «         }|p|                      ¦   «         } | d|||dœ|	¤ŽS )z)Construct an agent from an LLM and tools.)rA   rB   rC   rD   )rY   ÚpromptrZ   c                ó   — g | ]	}|j         ‘Œ
S r'   rP   rK   s     r)   rN   z4ZeroShotAgent.from_llm_and_tools.<locals>.<listcomp>u   s   € Ð2Ð2Ð2 D�d”iÐ2Ð2Ð2r(   )Ú	llm_chainÚallowed_toolsr0   r'   )Ú_validate_toolsrX   r   r5   )r4   rY   r?   rZ   r0   rA   rB   rC   rD   r1   r^   r`   rQ   Ú_output_parsers                 r)   Úfrom_llm_and_toolsz ZeroShotAgent.from_llm_and_toolsZ   s¼   € ð 	×Ò˜EÑ"Ô"Ð"Ø×"Ò"ØØØØ 3Ø+ð #ñ 
ô 
ˆõ ØØØ-ð
ñ 
ô 
ˆ	ð
 3Ð2¨EÐ2Ñ2Ô2ˆ
Ø&ÐJ¨#×*HÒ*HÑ*JÔ*JˆØˆsð 
ØØ$Ø(ð
ð 
ð ð	
ð 
ð 	
r(   ÚNonec                ó  •— t          | j        |¦  «         t          |¦  «        dk    rt          d| j        › d�¦  «        ‚|D ]!}|j        €t          d|j        › d�¦  «        ‚Œ"t          ¦   «                              |¦  «         d S )Nr   zGot no tools for z%. At least one tool must be provided.zGot a tool zN without a description. For this agent, a description must always be provided.)r   r"   ÚlenÚ
ValueErrorrJ   rI   Úsuperrb   )r4   r?   rM   Ú	__class__s      €r)   rb   zZeroShotAgent._validate_tools~   s¯   ø€ å# C¤L°%Ñ8Ô8Ð8Ýˆu‰:Œ:˜Š?ˆ?ÝØW C¤LÐWÐWÐWñô ð ð ð 	ð 	ˆDØÔÐ'Ý ð> $¤)ð >ð >ð >ñô ð ð (õ
 	‰Œ×Ò Ñ&Ô&Ð&Ð&Ð&r(   )r1   r   r2   r   )r2   r   )r?   r@   rA   r   rB   r   rC   r   rD   rE   r2   r   )rY   r   r?   r@   rZ   r[   r0   r\   rA   r   rB   r   rC   r   rD   rE   r1   r   r2   r   )r?   r@   r2   re   )r"   r#   r$   r%   r   r   r0   r&   Úclassmethodr5   Úpropertyr:   r<   r>   r   r   r   rX   rd   rb   Ú__classcell__)rj   s   @r)   r.   r.   $   s`  ø€ € € € € € à#Ð#à', uÐ=MÐ'NÑ'NÔ'N€MÐNÐNÐNÑNàð"ð "ð "ñ „[ð"ð ð5ð 5ð 5ñ „Xð5ð ðð ð ñ „Xðð ðð ð ñ „Xðð ð ØØ#6Ø/3ðRð Rð Rð Rñ „[ðRð8 ð
 ;?Ø59ØØØ#6Ø/3ð!
ð !
ð !
ð !
ñ „[ð!
ðF ð'ð 'ð 'ð 'ð 'ñ „[ð'ð 'ð 'ð 'ð 'r(   r.   )r,   c                  ó*   — e Zd ZdZedd
„¦   «         ZdS )Ú	MRKLChainz3[Deprecated] Chain that implements the MRKL system.rY   r   ÚchainsúList[ChainConfig]r1   r   r2   r   c                óf   — d„ |D ¦   «         }t                                ||¦  «        } | d||dœ|¤ŽS )a�  User friendly way to initialize the MRKL chain.

        This is intended to be an easy way to get up and running with the
        MRKL chain.

        Args:
            llm: The LLM to use as the agent LLM.
            chains: The chains the MRKL system has access to.
            **kwargs: parameters to be passed to initialization.

        Returns:
            An initialized MRKL chain.
        c                óP   — g | ]#}t          |j        |j        |j        ¬ ¦  «        ‘Œ$S ))rI   ÚfuncrJ   )r   r   r    r!   )rL   Úcs     r)   rN   z)MRKLChain.from_chains.<locals>.<listcomp>£   sI   € ð 
ð 
ð 
ð õ Ø”]Ø”XØÔ0ðñ ô ð
ð 
ð 
r(   )Úagentr?   r'   )r.   rd   )r4   rY   rp   r1   r?   rv   s         r)   Úfrom_chainszMRKLChain.from_chains’   sV   € ð"
ð 
ð ð
ñ 
ô 
ˆõ ×0Ò0°°eÑ<Ô<ˆØˆsÐ6˜ eÐ6Ð6¨vÐ6Ð6Ð6r(   N)rY   r   rp   rq   r1   r   r2   r   )r"   r#   r$   r%   rk   rw   r'   r(   r)   ro   ro   Ž   s8   € € € € € à=Ð=àð7ð 7ð 7ñ „[ð7ð 7ð 7r(   ro   N)+r%   Ú
__future__r   Útypingr   r   r   r   r   r	   Úlangchain_core._apir
   Úlangchain_core.language_modelsr   Úlangchain_core.promptsr   Úlangchain_core.pydantic_v1r   Úlangchain_core.toolsr   Úlangchain.agents.agentr   r   r   Úlangchain.agents.agent_typesr   Ú#langchain.agents.mrkl.output_parserr   Úlangchain.agents.mrkl.promptr   r   r   Úlangchain.agents.toolsr   Úlangchain.agents.utilsr   Úlangchain.callbacks.baser   Úlangchain.chainsr   r   r.   ro   r'   r(   r)   ú<module>r‡      s  ðØ UÐ UØ "Ð "Ð "Ð "Ð "Ð "à FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ Fà *Ð *Ð *Ð *Ð *Ð *Ø <Ð <Ð <Ð <Ð <Ð <Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø )Ð )Ð )Ð )Ð )Ð )à JÐ JÐ JÐ JÐ JÐ JÐ JÐ JÐ JÐ JØ 2Ð 2Ð 2Ð 2Ð 2Ð 2Ø @Ð @Ð @Ð @Ð @Ð @Ø LÐ LÐ LÐ LÐ LÐ LÐ LÐ LÐ LÐ LØ 'Ð 'Ð 'Ð 'Ð 'Ð 'Ø >Ð >Ð >Ð >Ð >Ð >Ø 8Ð 8Ð 8Ð 8Ð 8Ð 8Ø %Ð %Ð %Ð %Ð %Ð %ðð ð ð ð �*ñ ô ð ð €ˆGÐ!5¸wÐGÑGÔGðf'ð f'ð f'ð f'ð f'�Eñ f'ô f'ñ HÔGðf'ðR €ˆG˜WÐ%Ñ%Ô%ð7ð 7ð 7ð 7ð 7�ñ 7ô 7ñ &Ô%ð7ð 7ð 7r(   