Ë
    š,€i·  ã                  óv  — d Z ddlmZ ddlmZmZ ddl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 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+  G d„ de«      Z, e
ded¬«       G d„ de«      «       Z- e
ded¬«       G d„ de«      «       Z.y)zOAttempt to implement MRKL systems as described in arxiv.org/pdf/2205.00445.pdf.é    )Úannotations)ÚCallableÚSequence)ÚAnyÚ
NamedTuple)Ú
deprecated)ÚBaseCallbackManager)ÚBaseLanguageModel)ÚPromptTemplate)ÚBaseToolÚTool)Úrender_text_description)ÚField)Úoverride)ÚAGENT_DEPRECATION_WARNING)ÚAgentÚAgentExecutorÚAgentOutputParser)Ú	AgentType©ÚMRKLOutputParser)ÚFORMAT_INSTRUCTIONSÚPREFIXÚSUFFIX)Úvalidate_tools_single_input)ÚLLMChainc                  ó0   — e Zd ZU dZded<   ded<   ded<   y)ÚChainConfigzÉConfiguration for a 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__© ó    úg/var/www/html/langgraph-service/venv/lib/python3.12/site-packages/langchain_classic/agents/mrkl/base.pyr   r      s   … ñð ÓØÓØÔr)   r   z0.1.0z1.0)ÚmessageÚremovalc                  ó  ‡ — e Zd ZU dZ ee¬«      Zded<   e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.

    Args:
        output_parser: Output parser for the agent.
    )Údefault_factoryr   Úoutput_parserc                ó   — t        «       S )Nr   )ÚclsÚkwargss     r*   Ú_get_default_output_parserz(ZeroShotAgent._get_default_output_parser6   s   € ô  Ó!Ð!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Ñ4Ð4r)   c                 ó   — y)z]Prefix to append the observation with.

        Returns:
            "Observation: "
        zObservation: r(   r7   s    r*   Úobservation_prefixz ZeroShotAgent.observation_prefix@   s   € ð r)   c                 ó   — y)zVPrefix to append the llm call with.

        Returns:
            "Thought: "
        zThought:r(   r7   s    r*   Ú
llm_prefixzZeroShotAgent.llm_prefixI   s   € ð r)   Nc                ó  — t        t        |«      «      }dj                  |D cg c]  }|j                  ‘Œ c}«      }|j	                  |¬«      }|› d|› d|› d|› }	|rt        |	|¬«      S t        j                  |	«      S c c}w )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.
            format_instructions: Instructions on how to use the tools.
            input_variables: List of input variables the final prompt will expect.


        Returns:
            A PromptTemplate with the template assembled from the pieces here.
        z, )Ú
tool_namesz

)ÚtemplateÚinput_variables)r   ÚlistÚjoinÚnameÚformatr   Úfrom_template)
r2   ÚtoolsÚprefixÚsuffixÚformat_instructionsrA   Útool_stringsÚtoolr?   r@   s
             r*   Úcreate_promptzZeroShotAgent.create_promptR   s‡   € ô. /¬t°E«{Ó;ˆØ—Y‘Y°eÖ<¨d §	£	Ò<Ó=ˆ
Ø1×8Ñ8ÀJÐ8ÓOÐØX˜T , ¨tÐ4GÐ3HÈÈVÈHÐUˆÙÜ!¨8À_ÔUÐUÜ×+Ñ+¨HÓ5Ð5ùò  =s   ¤Bc	                óè   — | j                  |«       | j                  |||||¬«      }
t        ||
|¬«      }|D cg c]  }|j                  ‘Œ }}|xs | j	                  «       } | d|||dœ|	¤ŽS c c}w )a  Construct an agent from an LLM and tools.

        Args:
            llm: The LLM to use as the agent LLM.
            tools: The tools to use.
            callback_manager: The callback manager to use.
            output_parser: The output parser to use.
            prefix: The prefix to use.
            suffix: The suffix to use.
            format_instructions: The format instructions to use.
            input_variables: The input variables to use.
            kwargs: Additional parameters to pass to the agent.
        )rH   rI   rJ   rA   )ÚllmÚpromptÚcallback_manager)Ú	llm_chainÚallowed_toolsr0   r(   )Ú_validate_toolsrM   r   rD   r4   )r2   rO   rG   rQ   r0   rH   rI   rJ   rA   r3   rP   rR   rL   r?   Ú_output_parsers                  r*   Úfrom_llm_and_toolsz ZeroShotAgent.from_llm_and_toolsq   s¡   € ð4 	×Ñ˜EÔ"Ø×"Ñ"ØØØØ 3Ø+ð #ó 
ˆô ØØØ-ô
ˆ	ð
 -2Ö2 Dd—i“iÐ2ˆ
Ð2Ø&ÒJ¨#×*HÑ*HÓ*JˆÙð 
ØØ$Ø(ñ
ð ñ	
ð 	
ùò 3s   ºA/c                óþ   •— t        | j                  |«       t        |«      dk(  rd| j                  › d}t        |«      ‚|D ])  }|j                  Œd|j
                  › d}t        |«      ‚ t        ‰|   |«       y )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Ú
ValueErrorÚdescriptionrD   ÚsuperrT   )r2   rG   ÚmsgrL   Ú	__class__s       €r*   rT   zZeroShotAgent._validate_tools¡   sŽ   ø€ ä# C§L¡L°%Ô8Üˆu‹:˜Š?à# C§L¡L >Ð1VÐWð ô ˜S“/Ð!Øò 	&ˆDØ×ÑÑ'à! $§)¡) ð -=ð >ð ô ! “oÐ%ð	&ô 	‰Ñ Õ&r)   )r3   r   Úreturnr   )r^   r   )rG   úSequence[BaseTool]rH   r   rI   r   rJ   r   rA   úlist[str] | Noner^   r   )rO   r
   rG   r_   rQ   zBaseCallbackManager | Noner0   zAgentOutputParser | NonerH   r   rI   r   rJ   r   rA   r`   r3   r   r^   r   )rG   r_   r^   ÚNone)r#   r$   r%   r&   r   r   r0   r'   Úclassmethodr   r4   Úpropertyr9   r;   r=   r   r   r   rM   rV   rT   Ú__classcell__)r]   s   @r*   r.   r.   (   sl  ø… ññ (-Ð=MÔ'N€MÐ$ÓNàØò"ó ó ð"ð ò5ó ð5ð òó ðð òó ðð ð ØØ#6Ø,0ð6à!ð6ð ð6ð ð	6ð
 !ð6ð *ð6ð 
ò6ó ð6ð< ð
 8<Ø26ØØØ#6Ø,0ð-
àð-
ð "ð-
ð 5ð	-
ð
 0ð-
ð ð-
ð ð-
ð !ð-
ð *ð-
ð ð-
ð 
ò-
ó ð-
ð^ ô'ó ô'r)   r.   c                  ó2   — e Zd ZdZe	 	 	 	 	 	 	 	 dd„«       Zy)Ú	MRKLChainz&Chain that implements the MRKL system.c                óÀ   — |D cg c].  }t        |j                  |j                  |j                  ¬«      ‘Œ0 }}t        j                  ||«      } | d||dœ|¤ŽS c c}w )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.
        )rD   ÚfuncrZ   )ÚagentrG   r(   )r   r    r!   r"   r.   rV   )r2   rO   Úchainsr3   ÚcrG   ri   s          r*   Úfrom_chainszMRKLChain.from_chains»   sm   € ð4 ö
ð ô Ø—]‘]Ø—X‘XØ×0Ñ0öð
ˆð 
ô ×0Ñ0°°eÓ<ˆÙÐ6˜ eÑ6¨vÑ6Ð6ùò
s   …3AN)rO   r
   rj   zlist[ChainConfig]r3   r   r^   r   )r#   r$   r%   r&   rb   rl   r(   r)   r*   rf   rf   ³   s?   „ ñ 1àð7àð7ð "ð7ð ð	7ð
 
ò7ó ñ7r)   rf   N)/r&   Ú
__future__r   Úcollections.abcr   r   Útypingr   r   Úlangchain_core._apir   Úlangchain_core.callbacksr	   Úlangchain_core.language_modelsr
   Úlangchain_core.promptsr   Úlangchain_core.toolsr   r   Úlangchain_core.tools.renderr   Úpydanticr   Útyping_extensionsr   Ú"langchain_classic._api.deprecationr   Úlangchain_classic.agents.agentr   r   r   Ú$langchain_classic.agents.agent_typesr   Ú+langchain_classic.agents.mrkl.output_parserr   Ú$langchain_classic.agents.mrkl.promptr   r   r   Úlangchain_classic.agents.utilsr   Úlangchain_classic.chainsr   r   r.   rf   r(   r)   r*   ú<module>r      s¦   ðÙ Uå "ç .ß "å *Ý 8Ý <Ý 1ß /Ý ?Ý Ý &å Hß RÑ RÝ :Ý Hß TÑ TÝ FÝ -ô*ô ñ ØØ%Øôô
C'Eó C'óð
C'ñL ØØ%Øôô
 7ó  7óñ
 7r)   