o
    g                      @  s   d Z ddlmZ ddl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mZmZmZ G dd	 d	eZG d
d deZG dd deZG dd deZdddZdddZdddZdS )a1  Schema definitions for representing agent actions, observations, and return values.

**ATTENTION** The schema definitions are provided for backwards compatibility.

    New agents should be built using the langgraph library
    (https://github.com/langchain-ai/langgraph)), which provides a simpler
    and more flexible way to define agents.

    Please see the migration guide for information on how to migrate existing
    agents to modern langgraph agents:
    https://python.langchain.com/docs/how_to/migrate_agent/

Agents use language models to choose a sequence of actions to take.

A basic agent works in the following manner:

1. Given a prompt an agent uses an LLM to request an action to take (e.g., a tool to run).
2. The agent executes the action (e.g., runs the tool), and receives an observation.
3. The agent returns the observation to the LLM, which can then be used to generate the next action.
4. When the agent reaches a stopping condition, it returns a final return value.

The schemas for the agents themselves are defined in langchain.agents.agent.
    )annotationsN)Sequence)AnyLiteralUnion)Serializable)	AIMessageBaseMessageFunctionMessageHumanMessagec                      sx   e Zd ZU dZded< 	 ded< 	 ded< 	 d Zded< d fddZedddZedddZ	e
dddZ  ZS )AgentActionzRepresents a request to execute an action by an agent.

    The action consists of the name of the tool to execute and the input to pass
    to the tool. The log is used to pass along extra information about the action.
    strtoolUnion[str, dict]
tool_inputlogzLiteral['AgentAction']typekwargsr   c                   s   t  jd|||d| d S )N)r   r   r    super__init__)selfr   r   r   r   	__class__r   Q/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/langchain_core/agents.pyr   >   s   zAgentAction.__init__returnboolc                 C     dS )zRReturn whether or not the class is serializable.
        Default is True.
        Tr   clsr   r   r   is_lc_serializableC   s   zAgentAction.is_lc_serializable	list[str]c                 C     g dS )zhGet the namespace of the langchain object.
        Default is ["langchain", "schema", "agent"].
        	langchainschemaagentr   r   r   r   r   get_lc_namespaceJ   s   zAgentAction.get_lc_namespaceSequence[BaseMessage]c                 C  s   t | S )z3Return the messages that correspond to this action.)!_convert_agent_action_to_messagesr   r   r   r   messagesQ      zAgentAction.messages)r   r   r   r   r   r   r   r   r   r   r   r"   r   r)   __name__
__module____qualname____doc____annotations__r   r   classmethodr!   r(   propertyr,   __classcell__r   r   r   r   r   (   s    
 r   c                   @  s(   e Zd ZU dZded< 	 d Zded< dS )AgentActionMessageLoga  Representation of an action to be executed by an agent.

    This is similar to AgentAction, but includes a message log consisting of
    chat messages. This is useful when working with ChatModels, and is used
    to reconstruct conversation history from the agent's perspective.
    r)   message_logz Literal['AgentActionMessageLog']r   N)r2   r3   r4   r5   r6   r   r   r   r   r   r:   W   s
   
 
r:   c                   @  s4   e Zd ZU dZded< 	 ded< 	 eddd	Zd
S )	AgentStepz!Result of running an AgentAction.r   actionr   observationr   r)   c                 C  s   t | j| jS )-Messages that correspond to this observation.)&_convert_agent_observation_to_messagesr=   r>   r+   r   r   r   r,   u      zAgentStep.messagesNr0   )r2   r3   r4   r5   r6   r8   r,   r   r   r   r   r<   m   s   
 r<   c                      sn   e Zd ZU dZded< 	 ded< 	 d Zded< d fd
dZedddZedddZ	e
dddZ  ZS )AgentFinishzyFinal return value of an ActionAgent.

    Agents return an AgentFinish when they have reached a stopping condition.
    dictreturn_valuesr   r   zLiteral['AgentFinish']r   r   r   c                   s   t  jd||d| dS )zGOverride init to support instantiation by position for backward compat.)rD   r   Nr   r   )r   rD   r   r   r   r   r   r      s   zAgentFinish.__init__r   r   c                 C  r   )z0Return whether or not the class is serializable.Tr   r   r   r   r   r!      s   zAgentFinish.is_lc_serializabler"   c                 C  r#   )z*Get the namespace of the langchain object.r$   r   r   r   r   r   r(      r-   zAgentFinish.get_lc_namespacer)   c                 C  s   t | jdgS )r?   content)r   r   r+   r   r   r   r,      rA   zAgentFinish.messages)rD   rC   r   r   r   r   r.   r/   r0   r1   r   r   r   r   rB   {   s   
 rB   agent_actionr   r)   c                 C  s   t | tr| jS t| jdgS )a  Convert an agent action to a message.

    This code is used to reconstruct the original AI message from the agent action.

    Args:
        agent_action: Agent action to convert.

    Returns:
        AIMessage that corresponds to the original tool invocation.
    rE   )
isinstancer:   r;   r   r   )rG   r   r   r   r*      s   
r*   r>   r   c                 C  s^   t | trt| |gS |}t |ts)z	tj|dd}W n ty(   t|}Y nw t|dgS )aE  Convert an agent action to a message.

    This code is used to reconstruct the original AI message from the agent action.

    Args:
        agent_action: Agent action to convert.
        observation: Observation to convert to a message.

    Returns:
        AIMessage that corresponds to the original tool invocation.
    Fensure_asciirE   )rH   r:   _create_function_messager   jsondumps	Exceptionr   rG   r>   rF   r   r   r   r@      s   

r@   r
   c                 C  sJ   t |tsz	tj|dd}W n ty   t|}Y nw |}t| j|dS )a  Convert agent action and observation into a function message.

    Args:
        agent_action: the tool invocation request from the agent.
        observation: the result of the tool invocation.

    Returns:
        FunctionMessage that corresponds to the original tool invocation.
    FrI   )namerF   )rH   r   rL   rM   rN   r
   r   rO   r   r   r   rK      s   
rK   )rG   r   r   r)   )rG   r   r>   r   r   r)   )rG   r   r>   r   r   r
   )r5   
__future__r   rL   collections.abcr   typingr   r   r    langchain_core.load.serializabler   langchain_core.messagesr   r	   r
   r   r   r:   r<   rB   r*   r@   rK   r   r   r   r   <module>   s    /
%
