a
    •˜ëjB  ã                   @   s¾   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 d dlmZmZ d dlmZ d d	lmZ d d
lmZ eeeeef  gee	 f Zedœeee eeedœdd„ZdS )é    )ÚSequence)ÚCallable)ÚAgentAction)ÚBaseLanguageModel)ÚBaseMessage)ÚChatPromptTemplate)ÚRunnableÚRunnablePassthrough)ÚBaseTool)Úformat_to_tool_messages)ÚToolsAgentOutputParser©Úmessage_formatter)ÚllmÚtoolsÚpromptr   Úreturnc                   sp   dh  |jt|jƒ ¡}|r.d|› �}t|ƒ‚t| dƒsDd}t|ƒ‚|  |¡}tj‡ fdd„d�|B |B t	ƒ B S )a/	  Create an agent that uses tools.

    Args:
        llm: LLM to use as the agent.
        tools: Tools this agent has access to.
        prompt: The prompt to use. See Prompt section below for more on the expected
            input variables.
        message_formatter: Formatter function to convert (AgentAction, tool output)
            tuples into FunctionMessages.

    Returns:
        A Runnable sequence representing an agent. It takes as input all the same input
        variables as the prompt passed in does. It returns as output either an
        AgentAction or AgentFinish.

    Example:

        .. code-block:: python

            from langchain.agents import AgentExecutor, create_tool_calling_agent, tool
            from langchain_anthropic import ChatAnthropic
            from langchain_core.prompts import ChatPromptTemplate

            prompt = ChatPromptTemplate.from_messages(
                [
                    ("system", "You are a helpful assistant"),
                    ("placeholder", "{chat_history}"),
                    ("human", "{input}"),
                    ("placeholder", "{agent_scratchpad}"),
                ]
            )
            model = ChatAnthropic(model="claude-3-opus-20240229")

            @tool
            def magic_function(input: int) -> int:
                """Applies a magic function to an input."""
                return input + 2

            tools = [magic_function]

            agent = create_tool_calling_agent(model, tools, prompt)
            agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

            agent_executor.invoke({"input": "what is the value of magic_function(3)?"})

            # Using with chat history
            from langchain_core.messages import AIMessage, HumanMessage
            agent_executor.invoke(
                {
                    "input": "what's my name?",
                    "chat_history": [
                        HumanMessage(content="hi! my name is bob"),
                        AIMessage(content="Hello Bob! How can I assist you today?"),
                    ],
                }
            )

    Prompt:

        The agent prompt must have an `agent_scratchpad` key that is a
            ``MessagesPlaceholder``. Intermediate agent actions and tool output
            messages will be passed in here.

    Úagent_scratchpadz#Prompt missing required variables: Ú
bind_toolszGThis function requires a bind_tools() method be implemented on the LLM.c                    s   ˆ | d ƒS )NZintermediate_steps© )Úxr   r   úg/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/agents/tool_calling_agent/base.pyÚ<lambda>j   ó    z+create_tool_calling_agent.<locals>.<lambda>)r   )
Ú
differenceZinput_variablesÚlistZpartial_variablesÚ
ValueErrorÚhasattrr   r	   Zassignr   )r   r   r   r   Zmissing_varsÚmsgZllm_with_toolsr   r   r   Úcreate_tool_calling_agent   s,    Gÿ

ÿ

ÿýüûÿr   N)Úcollections.abcr   Útypingr   Zlangchain_core.agentsr   Zlangchain_core.language_modelsr   Zlangchain_core.messagesr   Zlangchain_core.prompts.chatr   Zlangchain_core.runnablesr   r	   Zlangchain_core.toolsr
   Z(langchain.agents.format_scratchpad.toolsr   Z%langchain.agents.output_parsers.toolsr   ÚtupleÚstrr   ZMessageFormatterr   r   r   r   r   Ú<module>   s$   ûú