
    h8                    6   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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       G d de"             Z.y)DAn agent designed to hold a conversation in addition to using tools.    )annotations)Sequence)AnyOptional)
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                  (    e Zd ZU dZ ee      Zded<   	 eZ	ded<   	 e
dd       Zedd       Zedd	       Zedd
       Ze
d fd       Ze
eeddf	 	 	 	 	 	 	 	 	 	 	 dd       Z	 	 	 	 ddZe
ddeedf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Z xZS )ConversationalChatAgentr   )default_factoryr   output_parserstrtemplate_tool_responsec                    t               S Nr   )clskwargss     g/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain/agents/conversational_chat/base.py_get_default_output_parserz2ConversationalChatAgent._get_default_output_parser,   s     ""    c                    t         r*   )NotImplementedErrorselfs    r-   _agent_typez#ConversationalChatAgent._agent_type0   s    !!r/   c                     y)z]Prefix to append the observation with.

        Returns:
            "Observation: "
        zObservation:  r2   s    r-   observation_prefixz*ConversationalChatAgent.observation_prefix4   s     r/   c                     y)zVPrefix to append the llm call with.

        Returns:
            "Thought: "
        zThought:r6   r2   s    r-   
llm_prefixz"ConversationalChatAgent.llm_prefix=   s     r/   c                P    t         |   |       t        | j                  |       y r*   )super_validate_toolsr   __name__)r+   tools	__class__s     r-   r<   z'ConversationalChatAgent._validate_toolsF   s    &#CLL%8r/   Nc           	        dj                  |D cg c]  }d|j                   d|j                     c}      }dj                  |D cg c]  }|j                   c}      }|xs | j                         }	|j	                  |	j                               }
|
j	                  ||      }|g d}t        j                  |      t        d	      t        j                  |      t        d
	      g}t        ||      S c c}w c c}w )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.
        
z> z: z, )format_instructions)
tool_namesr>   )inputchat_historyagent_scratchpadrE   )variable_namerF   )input_variablesmessages)joinnamedescriptionr.   formatget_format_instructionsr   from_templater   r   r   )r+   r>   system_messagehuman_messagerH   r&   tooltool_stringsrC   _output_parserrB   final_promptrI   s                r-   create_promptz%ConversationalChatAgent.create_promptK   s   . yy>CDdr$))Bt//01D
 YYe<d		<=
&J#*H*H*J+22 . F F H 3 
 +11! 2 
 "KO'55nEn=&44\B.@A	
 "/HUU' E<s   #C=	Dc                    g }|D ]a  \  }}|j                  t        |j                               t        | j                  j                  |            }|j                  |       c |S )zJConstruct the scratchpad that lets the agent continue its thought process.)content)observation)appendr   logr   r(   rM   )r3   intermediate_stepsthoughtsactionrY   rQ   s         r-   _construct_scratchpadz-ConversationalChatAgent._construct_scratchpadx   sh    
 ')#5 	+FKOOIfjj9:(33::{:SM OOM*	+ r/   c                    | j                  |       |xs | j                         }	| j                  |||||	      }
t        ||
|      }|D cg c]  }|j                   }} | d|||	d|S c c}w )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.
        )rP   rQ   rH   r&   )llmpromptcallback_manager)	llm_chainallowed_toolsr&   r6   )r<   r.   rV   r   rK   )r+   ra   r>   rc   r&   rP   rQ   rH   r,   rT   rb   rd   rR   rC   s                 r-   from_llm_and_toolsz*ConversationalChatAgent.from_llm_and_tools   s    6 	E"&J#*H*H*J"")'+( # 
 -
	
 -22Ddii2
2 
$(
 	
 	
 3s   A/)r,   r   returnr   )rg   r'   )r>   Sequence[BaseTool]rg   None)r>   rh   rP   r'   rQ   r'   rH   Optional[list[str]]r&   zOptional[BaseOutputParser]rg   r   )r\   zlist[tuple[AgentAction, str]]rg   zlist[BaseMessage])ra   r   r>   rh   rc   zOptional[BaseCallbackManager]r&   zOptional[AgentOutputParser]rP   r'   rQ   r'   rH   rj   r,   r   rg   r   )r=   
__module____qualname____doc__r   r   r&   __annotations__r   r(   classmethodr.   propertyr4   r7   r9   r<   r   r   rV   r_   rf   __classcell__)r?   s   @r-   r$   r$   #   s   N',=N'OM$O&"8C8)# # " "     9 9  %#/348*V!*V *V 	*V
 -*V 2*V 
*V *VX9 
 
 ;?59$#/3.
.
 ".
 8	.

 3.
 .
 .
 -.
 .
 
.
 .
r/   r$   N)/rm   
__future__r   collections.abcr   typingr   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$   r6   r/   r-   <module>r      s|    J " $   * - 8 < H H : 5  *  ; P 
 ? % G!95IQ
e Q
 JQ
r/   