a
    •˜ëj�  ã                   @   s”   d dl Z d dl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 e e¡Zeeedœdd	„Zeeeef  ee
 d
œdd„ZdS )é    N)ÚSequence)ÚAny)ÚAgentAction)Ú	AIMessageÚBaseMessageÚToolMessage)ÚToolAgentAction)Úagent_actionÚobservationÚreturnc                 C   sv   t |tƒs\ztj|dd�}W q` ty6   t|ƒ}Y q` tyX   t d¡ t|ƒ}Y q`0 n|}t| j	|d| j
id�S )al  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_asciiz2Unexpected error converting observation to string.Úname)Útool_call_idÚcontentZadditional_kwargs)Ú
isinstanceÚstrÚjsonÚdumpsÚ	TypeErrorÚ	ExceptionÚ_loggerÚ	exceptionr   r   Ztool)r	   r
   r   © r   úg/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/agents/format_scratchpad/tools.pyÚ_create_tool_message   s    

ýr   )Úintermediate_stepsr   c                    sd   g ‰ | D ]V\}}t |tƒrLg t|jƒ¢t||ƒ‘}ˆ  ‡ fdd„|D ƒ¡ qˆ  t|jd�¡ qˆ S )zöConvert (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>F   ó    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_messages3   s    
ÿþr&   )r   ÚloggingÚcollections.abcr   Útypingr   Zlangchain_core.agentsr   Zlangchain_core.messagesr   r   r   Z%langchain.agents.output_parsers.toolsr   Ú	getLoggerÚ__name__r   r   Útupler   r"   r&   r   r   r   r   Ú<module>   s   
ý"þ