
    Ng                         d dl Z d dlmZmZmZ d dlmZ d dlmZm	Z	m
Z
 d dlmZ dedede
fd	Zd
eeeef                  dee	         fdZdS )    N)ListSequenceTuple)AgentAction)	AIMessageBaseMessageToolMessage)ToolAgentActionagent_actionobservationreturnc                     t          |t                    s7	 t          j        |d          }n!# t          $ r t          |          }Y nw xY w|}t          | j        |d| j        i          S )ak  Convert agent action and observation into a tool message.

    Args:
        agent_action: the tool invocation request from the agent.
        observation: the result of the tool invocation.
    Returns:
        ToolMessage that corresponds to the original tool invocation.

    Raises:
        ValueError: if the observation cannot be converted to a string.
    F)ensure_asciiname)tool_call_idcontentadditional_kwargs)
isinstancestrjsondumps	Exceptionr	   r   tool)r   r   r   s      d/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain/agents/format_scratchpad/tools.py_create_tool_messager      s     k3'' 	'j5AAAGG 	' 	' 	'+&&GGG	' !.!<#45   s   . A
	A
intermediate_stepsc                 &   g | D ]\  }}t          |t                    rHt          |j                  t	          ||          gz   }                    fd|D                        b                    t          |j                             S )zConvert (AgentAction, tool output) tuples into ToolMessages.

    Args:
        intermediate_steps: Steps the LLM has taken to date, along with observations.

    Returns:
        list of messages to send to the LLM for the next prediction.

    c                     g | ]}|v|	S  r   ).0newmessagess     r   
<listcomp>z+format_to_tool_messages.<locals>.<listcomp><   s#    PPPSCx<O<OS<O<O<O    )r   )	r   r
   listmessage_logr   extendappendr   log)r   r   r   new_messagesr"   s       @r   format_to_tool_messagesr+   *   s     H%7 A A!klO44 	A 899$\;??= L OOPPPPLPPPQQQQOOIl.>???@@@@Or$   )r   typingr   r   r   langchain_core.agentsr   langchain_core.messagesr   r   r	   %langchain.agents.output_parsers.toolsr
   r   r   r+   r   r$   r   <module>r0      s     ( ( ( ( ( ( ( ( ( ( - - - - - -          B A A A A A!03   8 {C'7!89	+     r$   