o
    ZhA                     @  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.0Zcreate_json_chat_agentz1.0)alternativeZ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+   `/var/www/html/lang_env/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+   r/   r+   r+   r,   observation_prefix3      z*ConversationalChatAgent.observation_prefixc                 C  r2   )zVPrefix to append the llm call with.

        Returns:
            "Thought: "
        zThought:r+   r/   r+   r+   r,   
llm_prefix<   r4   z"ConversationalChatAgent.llm_prefixtoolsSequence[BaseTool]Nonec                   s   t  | t| j| d S r)   )super_validate_toolsr   __name__)r*   r6   	__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.0Ztoolr+   r+   r,   
<listcomp>b   s     z9ConversationalChatAgent.create_prompt.<locals>.<listcomp>z, c                 S     g | ]}|j qS r+   rD   rF   r+   r+   r,   rH   d       )format_instructions)
tool_namesr6   N)inputchat_historyagent_scratchpadrO   )Zvariable_namerP   )r@   messages)	joinr-   formatZget_format_instructionsr   Zfrom_templater   r   r   )r*   r6   r>   r?   r@   r$   Ztool_stringsrM   _output_parserrL   Zfinal_promptrQ   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&   rS   )r0   rV   ZthoughtsactionrZ   r?   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>   r?   r@   r$   )r_   promptr`   c                 S  rI   r+   rJ   rF   r+   r+   r,   rH      rK   z>ConversationalChatAgent.from_llm_and_tools.<locals>.<listcomp>)	llm_chainZallowed_toolsr$   Nr+   )r:   r-   rU   r    )r*   r_   r6   r`   r$   r>   r?   r@   r'   rT   rc   rd   rM   r+   r+   r,   from_llm_and_tools   s,   
z*ConversationalChatAgent.from_llm_and_tools)r'   r   r(   r   )r(   r%   )r6   r7   r(   r8   )r6   r7   r>   r%   r?   r%   r@   rA   r$   rB   r(   r   )rV   rW   r(   rX   )r_   r   r6   r7   r`   ra   r$   rb   r>   r%   r?   r%   r@   rA   r'   r   r(   r   )r;   
__module____qualname____doc__r   r   r$   __annotations__r   r&   classmethodr-   propertyr1   r3   r5   r:   r   r   rU   r^   re   __classcell__r+   r+   r<   r,   r"   "   s<   
 
+r"   N)0rh   
__future__r   typingr   r   r   r   r   Zlangchain_core._apir	   Zlangchain_core.agentsr
   Zlangchain_core.callbacksr   Zlangchain_core.language_modelsr   Zlangchain_core.messagesr   r   r   Zlangchain_core.output_parsersr   Zlangchain_core.promptsr   Zlangchain_core.prompts.chatr   r   r   r   Zlangchain_core.toolsr   Zpydanticr   Zlangchain.agents.agentr   r   Z2langchain.agents.conversational_chat.output_parserr   Z+langchain.agents.conversational_chat.promptr   r   r   Zlangchain.agents.utilsr   Zlangchain.chainsr    r"   r+   r+   r+   r,   <module>   s(    