
    gh}	                         d dl Z d dlmZ d dlmZmZ d dlmZmZm	Z	 dede
dee         fdZdede
de	fd	Zd
eeee
f                  dee         fdZeZdS )    N)Sequence)AgentActionAgentActionMessageLog)	AIMessageBaseMessageFunctionMessageagent_actionobservationreturnc                     t          | t                    r&t          | j                  t	          | |          gz   S t          | j                  gS )aF  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 or the previous messages plus a FunctionMessage that corresponds to
            the original tool invocation
    )content)
isinstancer   listmessage_log_create_function_messager   log)r	   r
   s     p/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/langchain/agents/format_scratchpad/openai_functions.py!_convert_agent_action_to_messagesr      sZ     , 566 5L,--$\;??1
 
 	
 ,"233344    c                     t          |t                    s7	 t          j        |d          }n!# t          $ r t          |          }Y nw xY w|}t          | j        |          S )ar  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_ascii)namer   )r   strjsondumps	Exceptionr   tool)r	   r
   r   s      r   r   r      s     k3'' 	'j5AAAGG 	' 	' 	'+&&GGG	'    s   . A
	A
intermediate_stepsc                 `    g }| D ](\  }}|                     t          ||                     )|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
   s       r   "format_to_openai_function_messagesr"   8   sD     H%7 V V!k9,TTUUUUOr   )r   collections.abcr   langchain_core.agentsr   r   langchain_core.messagesr   r   r   r   r   r   r   tupler"   format_to_openai_functions r   r   <module>r)      s     $ $ $ $ $ $ D D D D D D D D K K K K K K K K K K55,/5	+5 5 5 5,,/   4 {C'7!89	+   , @   r   