a
    •˜ëj  ã                   @  s  d 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 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 m!Z!m"Z" ddl#m$Z$ ddl%m&Z& e	dedd�G dd„ deƒƒZ'dS )zDAn agent designed to hold a conversation in addition to using tools.é    )Úannotations)ÚSequence)ÚAnyÚOptional)Ú
deprecated)ÚBaseCallbackManager)ÚBaseLanguageModel)ÚPromptTemplate)ÚBaseTool)ÚField)Úoverride)ÚAGENT_DEPRECATION_WARNING)ÚAgentÚAgentOutputParser)Ú	AgentType©ÚConvoOutputParser)ÚFORMAT_INSTRUCTIONSÚPREFIXÚSUFFIX)Úvalidate_tools_single_input)ÚLLMChainz0.1.0z1.0)ÚmessageZremovalc                      s  e Zd ZU dZdZded< eed�Zded< e	e
d&dddd	œd
d„ƒƒZeddœdd„ƒZeddœdd„ƒZeddœdd„ƒZe	eeedddfdddddddddœdd„ƒZe	dddœ‡ fdd„ƒZe	ddeeedddfddd d!dddddddd"d#œd$d%„ƒZ‡  ZS )'ÚConversationalAgentz>An agent that holds a conversation in addition to using tools.ÚAIÚstrÚ	ai_prefix)Údefault_factoryr   Úoutput_parserr   )r   ÚkwargsÚreturnc                 K  s
   t |d�S )N©r   r   )Úclsr   r   © r#   úc/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/agents/conversational/base.pyÚ_get_default_output_parser&   s    z.ConversationalAgent._get_default_output_parser)r    c                 C  s   t jS )z Return Identifier of agent type.)r   Z CONVERSATIONAL_REACT_DESCRIPTION©Úselfr#   r#   r$   Ú_agent_type/   s    zConversationalAgent._agent_typec                 C  s   dS )z]Prefix to append the observation with.

        Returns:
            "Observation: "
        zObservation: r#   r&   r#   r#   r$   Úobservation_prefix4   s    z&ConversationalAgent.observation_prefixc                 C  s   dS )zVPrefix to append the llm call with.

        Returns:
            "Thought: "
        zThought:r#   r&   r#   r#   r$   Ú
llm_prefix=   s    zConversationalAgent.llm_prefixZHumanNzSequence[BaseTool]zOptional[list[str]]r	   )ÚtoolsÚprefixÚsuffixÚformat_instructionsr   Úhuman_prefixÚinput_variablesr    c                 C  sn   d  dd„ |D ƒ¡}d  dd„ |D ƒ¡}	|j|	||d�}|› d|› d|› d|› �}
|du rbg d	¢}t|
|d
�S )au  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. Defaults to PREFIX.
            suffix: String to put after the list of tools. Defaults to SUFFIX.
            format_instructions: Instructions on how to use the tools. Defaults to
                FORMAT_INSTRUCTIONS
            ai_prefix: String to use before AI output. Defaults to "AI".
            human_prefix: String to use before human output.
                Defaults to "Human".
            input_variables: List of input variables the final prompt will expect.
                Defaults to ["input", "chat_history", "agent_scratchpad"].

        Returns:
            A PromptTemplate with the template assembled from the pieces here.
        Ú
c                 S  s    g | ]}d |j › d|j› �‘qS )z> z: )ÚnameÚdescription©Ú.0Ztoolr#   r#   r$   Ú
<listcomp>d   ó    z5ConversationalAgent.create_prompt.<locals>.<listcomp>z, c                 S  s   g | ]
}|j ‘qS r#   ©r2   r4   r#   r#   r$   r6   f   r7   )Ú
tool_namesr   r/   z

N)ÚinputZchat_historyZagent_scratchpad)Útemplater0   )ÚjoinÚformatr	   )r"   r+   r,   r-   r.   r   r/   r0   Ztool_stringsr9   r;   r#   r#   r$   Úcreate_promptF   s    ÿýz!ConversationalAgent.create_promptÚNone)r+   r    c                   s   t ƒ  |¡ t| j|ƒ d S )N)ÚsuperÚ_validate_toolsr   Ú__name__)r"   r+   ©Ú	__class__r#   r$   rA   q   s    z#ConversationalAgent._validate_toolsr   zOptional[BaseCallbackManager]zOptional[AgentOutputParser]r   )Úllmr+   Úcallback_managerr   r,   r-   r.   r   r/   r0   r   r    c              	   K  sf   |   |¡ | j|||	||||
d�}t|||d�}dd„ |D ƒ}|pL| j|d�}| f ||||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.
            prefix: The prefix to use in the prompt. Default is PREFIX.
            suffix: The suffix to use in the prompt. Default is SUFFIX.
            format_instructions: The format instructions to use.
                Default is FORMAT_INSTRUCTIONS.
            ai_prefix: The prefix to use before AI output. Default is "AI".
            human_prefix: The prefix to use before human output.
                Default is "Human".
            input_variables: The input variables to use. Default is None.
            **kwargs: Any additional keyword arguments to pass to the agent.

        Returns:
            An agent.
        )r   r/   r,   r-   r.   r0   )rE   ÚpromptrF   c                 S  s   g | ]
}|j ‘qS r#   r8   r4   r#   r#   r$   r6   ¨   r7   z:ConversationalAgent.from_llm_and_tools.<locals>.<listcomp>r!   )Ú	llm_chainZallowed_toolsr   r   )rA   r>   r   r%   )r"   rE   r+   rF   r   r,   r-   r.   r   r/   r0   r   rG   rH   r9   Z_output_parserr#   r#   r$   Úfrom_llm_and_toolsv   s6    #
ù	ýÿüûz&ConversationalAgent.from_llm_and_tools)r   )rB   Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r   r   r   Úclassmethodr   r%   Úpropertyr(   r)   r*   r   r   r   r>   rA   rI   Ú__classcell__r#   r#   rC   r$   r      sB   
 þø *õr   N)(rL   Ú
__future__r   Úcollections.abcr   Útypingr   r   Zlangchain_core._apir   Zlangchain_core.callbacksr   Zlangchain_core.language_modelsr   Zlangchain_core.promptsr	   Zlangchain_core.toolsr
   Zpydanticr   Ztyping_extensionsr   Zlangchain._api.deprecationr   Zlangchain.agents.agentr   r   Zlangchain.agents.agent_typesr   Z-langchain.agents.conversational.output_parserr   Z&langchain.agents.conversational.promptr   r   r   Zlangchain.agents.utilsr   Zlangchain.chainsr   r   r#   r#   r#   r$   Ú<module>   s.   ý