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mZ d dl	m
Z
mZmZ e e¡Zeeee dœdd„Zeeedœd	d
„Zeeeef  ee dœdd„ZeZdS )é    N)ÚSequence)ÚAny)ÚAgentActionÚAgentActionMessageLog)Ú	AIMessageÚBaseMessageÚFunctionMessage)Úagent_actionÚobservationÚreturnc                 C   s0   t | tƒr"g t| jƒ¢t| |ƒ‘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.
        observation: The result of the tool invocation.

    Returns:
        AIMessage or the previous messages plus a FunctionMessage that corresponds to
            the original tool invocation
    )Úcontent)Ú
isinstancer   ÚlistZmessage_logÚ_create_function_messager   Úlog)r	   r
   © r   úr/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/agents/format_scratchpad/openai_functions.pyÚ!_convert_agent_action_to_messages   s    
ÿþr   c                 C   sn   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�S )at  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.

    Raises:
        ValueError: if the observation cannot be converted to a string.
    F)Úensure_asciiz2Unexpected error converting observation to string.)Únamer   )
r   ÚstrÚjsonÚdumpsÚ	TypeErrorÚ	ExceptionÚ_loggerÚ	exceptionr   Ztool)r	   r
   r   r   r   r   r   $   s    

þr   )Úintermediate_stepsr   c                 C   s&   g }| D ]\}}|  t||ƒ¡ q|S )aK  Convert (AgentAction, tool output) tuples into FunctionMessages.

    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
    Raises:
        ValueError: if the observation cannot be converted to a string.
    )Úextendr   )r   Úmessagesr	   r
   r   r   r   Ú"format_to_openai_function_messagesD   s    r    )r   ÚloggingÚcollections.abcr   Útypingr   Zlangchain_core.agentsr   r   Zlangchain_core.messagesr   r   r   Ú	getLoggerÚ__name__r   r   r   r   r   Útupler    Zformat_to_openai_functionsr   r   r   r   Ú<module>   s$   
ýý!þ