o
    JjgA                     @  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mZ dd	lmZ dd
lmZ ddlmZmZmZ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(m)Z)m*Z* ddl+m,Z, ddl-m.Z. e
ddddG dd de#Z/dS )DAn agent designed to hold a conversation in addition to using tools.    )annotations)AnyListOptionalSequenceTuple)
deprecated)AgentAction)BaseCallbackManager)BaseLanguageModel)	AIMessageBaseMessageHumanMessage)BaseOutputParser)BasePromptTemplate)ChatPromptTemplateHumanMessagePromptTemplateMessagesPlaceholderSystemMessagePromptTemplate)BaseTool)Field)AgentAgentOutputParserConvoOutputParser)PREFIXSUFFIXTEMPLATE_TOOL_RESPONSE)validate_tools_single_input)LLMChainz0.1.0create_json_chat_agentz1.0)alternativeremovalc                      s   e Zd ZU dZeedZded< 	 eZ	ded< 	 e
d-d
dZed.ddZed.ddZed.ddZe
d/ fddZe
eeddfd0ddZd1d#d$Ze
ddeedfd2d+d,Z  ZS )3ConversationalChatAgentr   )default_factoryr   output_parserstrtemplate_tool_responsekwargsr   returnc                 K  s   t  S Nr   )clsr)    r-   a/var/www/html/zoom/venv/lib/python3.10/site-packages/langchain/agents/conversational_chat/base.py_get_default_output_parser+   s   z2ConversationalChatAgent._get_default_output_parserc                 C  s   t r+   )NotImplementedErrorselfr-   r-   r.   _agent_type/   s   z#ConversationalChatAgent._agent_typec                 C     dS )z]Prefix to append the observation with.

        Returns:
            "Observation: "
        zObservation: r-   r1   r-   r-   r.   observation_prefix3      z*ConversationalChatAgent.observation_prefixc                 C  r4   )zVPrefix to append the llm call with.

        Returns:
            "Thought: "
        zThought:r-   r1   r-   r-   r.   
llm_prefix<   r6   z"ConversationalChatAgent.llm_prefixtoolsSequence[BaseTool]Nonec                   s   t  | t| j| d S r+   )super_validate_toolsr   __name__)r,   r8   	__class__r-   r.   r<   E   s   z'ConversationalChatAgent._validate_toolsNsystem_messagehuman_messageinput_variablesOptional[List[str]]Optional[BaseOutputParser]r   c                 C  s   d dd |D }d dd |D }|p|  }|j| d}	|	j||d}
|du r1g d	}t|td
dt|
tddg}t||dS )a  Create a prompt for the agent.

        Args:
            tools: The tools to use.
            system_message: The system message to use.
                Defaults to the PREFIX.
            human_message: The human message to use.
                Defaults to the SUFFIX.
            input_variables: The input variables to use. Defaults to None.
            output_parser: The output parser to use. Defaults to None.

        Returns:
            A PromptTemplate.
        
c                 S  s    g | ]}d |j  d|j qS )z> z: )namedescription.0toolr-   r-   r.   
<listcomp>b   s     z9ConversationalChatAgent.create_prompt.<locals>.<listcomp>z, c                 S     g | ]}|j qS r-   rF   rH   r-   r-   r.   rK   d       )format_instructions)
tool_namesr8   N)inputchat_historyagent_scratchpadrR   )variable_namerS   )rB   messages)	joinr/   formatget_format_instructionsr   from_templater   r   r   )r,   r8   r@   rA   rB   r&   tool_stringsrP   _output_parserrO   final_promptrU   r-   r-   r.   create_promptJ   s&   z%ConversationalChatAgent.create_promptintermediate_stepsList[Tuple[AgentAction, str]]List[BaseMessage]c                 C  sF   g }|D ]\}}| t|jd t| jj|dd}| | q|S )zJConstruct the scratchpad that lets the agent continue its thought process.)content)observation)appendr   logr   r(   rW   )r2   r^   thoughtsactionrb   rA   r-   r-   r.   _construct_scratchpadv   s   z-ConversationalChatAgent._construct_scratchpadllmr   callback_managerOptional[BaseCallbackManager]Optional[AgentOutputParser]r   c                 K  s\   |  | |p
|  }	| j|||||	d}
t||
|d}dd |D }| d|||	d|S )a^  Construct an agent from an LLM and tools.

        Args:
            llm: The language model to use.
            tools: A list of tools to use.
            callback_manager: The callback manager to use. Default is None.
            output_parser: The output parser to use. Default is None.
            system_message: The system message to use. Default is PREFIX.
            human_message: The human message to use. Default is SUFFIX.
            input_variables: The input variables to use. Default is None.
            **kwargs: Any additional arguments.

        Returns:
            An agent.
        )r@   rA   rB   r&   )rh   promptri   c                 S  rL   r-   rM   rH   r-   r-   r.   rK      rN   z>ConversationalChatAgent.from_llm_and_tools.<locals>.<listcomp>)	llm_chainallowed_toolsr&   Nr-   )r<   r/   r]   r    )r,   rh   r8   ri   r&   r@   rA   rB   r)   r[   rl   rm   rP   r-   r-   r.   from_llm_and_tools   s,   
z*ConversationalChatAgent.from_llm_and_tools)r)   r   r*   r   )r*   r'   )r8   r9   r*   r:   )r8   r9   r@   r'   rA   r'   rB   rC   r&   rD   r*   r   )r^   r_   r*   r`   )rh   r   r8   r9   ri   rj   r&   rk   r@   r'   rA   r'   rB   rC   r)   r   r*   r   )r=   
__module____qualname____doc__r   r   r&   __annotations__r   r(   classmethodr/   propertyr3   r5   r7   r<   r   r   r]   rg   ro   __classcell__r-   r-   r>   r.   r$   "   s<   
 
+r$   N)0rr   
__future__r   typingr   r   r   r   r   langchain_core._apir	   langchain_core.agentsr
   langchain_core.callbacksr   langchain_core.language_modelsr   langchain_core.messagesr   r   r   langchain_core.output_parsersr   langchain_core.promptsr   langchain_core.prompts.chatr   r   r   r   langchain_core.toolsr   pydanticr   langchain.agents.agentr   r   2langchain.agents.conversational_chat.output_parserr   +langchain.agents.conversational_chat.promptr   r   r   langchain.agents.utilsr   langchain.chainsr    r$   r-   r-   r-   r.   <module>   s(    