
    }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 dl	m
Z
 d dlmZmZ d dlmZmZ d dlmZ  G d	 d
e      Zy)    N)JSONDecodeError)ListUnion)AgentActionAgentActionMessageLogAgentFinish)OutputParserException)	AIMessageBaseMessage)ChatGeneration
Generation)AgentOutputParserc            	           e Zd ZdZedefd       Zedede	e
ef   fd       Zdddee   d	ede	e
ef   fd
Zdede	e
ef   fdZy) OpenAIFunctionsAgentOutputParserar  Parses a message into agent action/finish.

    Is meant to be used with OpenAI models, as it relies on the specific
    function_call parameter from OpenAI to convey what tools to use.

    If a function_call parameter is passed, then that is used to get
    the tool and tool input.

    If one is not passed, then the AIMessage is assumed to be the final output.
    returnc                      y)Nzopenai-functions-agent )selfs    j/var/www/html/engine/venv/lib/python3.12/site-packages/langchain/agents/output_parsers/openai_functions.py_typez&OpenAIFunctionsAgentOutputParser._type   s    '    messagec                 ,   t        | t              st        dt        |              | j                  j                  di       }|r|d   }	 t        |d   j                               dk(  ri }nt        j                  |d   d      }d
|v r|d
   }n|}| j                  rd| j                   dnd}d| d| d| d}t        |||| g      S t        d| j                  it        | j                              S # t        $ r t        d| d	      w xY w)zParse an AI message.zExpected an AI message got function_callname	argumentsr   F)strictzCould not parse tool input: z+ because the `arguments` is not valid JSON.__arg1zresponded: 
z
Invoking: `z` with `z`
)tool
tool_inputlogmessage_logoutput)return_valuesr"   )
isinstancer
   	TypeErrortypeadditional_kwargsgetlenstripjsonloadsr   r	   contentr   r   str)r   r   function_name_tool_inputr!   content_msgr"   s          r   _parse_ai_messagez2OpenAIFunctionsAgentOutputParser._parse_ai_message    sH    '9-9$w-IJJ1155orJ)&1M}[1779:a?"$K #'**];-GPU"VK ;&(2
(
?FK'8;TXK!-C}TVWC("%$I	  #W__53w;O
 	
5 # +2=/ B9 : s   <C: :DF)partialresultr5   c                ~    t        |d   t              st        d      |d   j                  }| j	                  |      S )Nr   z6This output parser only works on ChatGeneration output)r&   r   
ValueErrorr   r4   )r   r6   r5   r   s       r   parse_resultz-OpenAIFunctionsAgentOutputParser.parse_resultO   s>     &)^4UVV)##%%g..r   textc                     t        d      )NzCan only parse messages)r8   )r   r:   s     r   parsez&OpenAIFunctionsAgentOutputParser.parseW   s    233r   N)__name__
__module____qualname____doc__propertyr0   r   staticmethodr   r   r   r   r4   r   r   boolr9   r<   r   r   r   r   r      s    	 (s ( ( ,
; ,
5k9Q3R ,
 ,
^ <A/:&/48/	{K'	(/4# 4%[(@"A 4r   r   )r-   r   typingr   r   langchain_core.agentsr   r   r   langchain_core.exceptionsr	   langchain_core.messagesr
   r   langchain_core.outputsr   r   langchain.agents.agentr   r   r   r   r   <module>rJ      s3        Q Q ; > 4H4'8 H4r   