a
    ag                     @   s~   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 eee
dddZeeeef  ee	 d	d
dZdS )    N)ListSequenceTuple)AgentAction)	AIMessageBaseMessageToolMessage)ToolAgentAction)agent_actionobservationreturnc                 C   sT   t |ts:ztj|dd}W q> ty6   t|}Y q>0 n|}t| j|d| jid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contentZadditional_kwargs)
isinstancestrjsondumps	Exceptionr   r   Ztool)r
   r   r    r   v/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain/agents/format_scratchpad/tools.py_create_tool_message   s    
r   )intermediate_stepsr   c                    sb   g  | D ]T\}}t |trJt|jt||g }  fdd|D  q t|jd q 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                    s   g | ]}| vr|qS r   r   ).0newmessagesr   r   
<listcomp><       z+format_to_tool_messages.<locals>.<listcomp>)r   )	r   r	   listZmessage_logr   extendappendr   log)r   r
   r   Znew_messagesr   r   r   format_to_tool_messages*   s    
r$   )r   typingr   r   r   Zlangchain_core.agentsr   Zlangchain_core.messagesr   r   r   Z%langchain.agents.output_parsers.toolsr	   r   r   r$   r   r   r   r   <module>   s   