
    Ng                         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dS )    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dS ) 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                     dS )Nzopenai-functions-agent )selfs    l/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain/agents/output_parsers/openai_functions.py_typez&OpenAIFunctionsAgentOutputParser._type   s    ''    messagec                 h   t          | t                    st          dt          |                      | j                            di           }|r|d         }	 t          |d                                                   dk    ri }nt          j	        |d         d          }n!# t          $ r t          d| d	          w xY w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 )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    s    '9-- 	KI$w--IIJJJ155orJJ !	)&1M}[17799::a??"$KK #'*];-GPU"V"V"VK"   +:= : : :   ;&&(2

(
?FX;;;;;TXKW-WWWWWWWC("%$I	    #W_53w;O;O
 
 
 	
s   A
B& &CF)partialresultr5   c                    t          |d         t                    st          d          |d         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   sJ     &)^44 	WUVVV)#%%g...r   textc                      t          d          )NzCan only parse messages)r8   )r   r:   s     r   parsez&OpenAIFunctionsAgentOutputParser.parseW   s    23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 ( ( ( X( ,
; ,
5k9Q3R ,
 ,
 ,
 \,
^ <A/ / /:&/48/	{K'	(/ / / /4# 4%[(@"A 4 4 4 4 4 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      s                         Q Q Q Q Q Q Q Q Q Q ; ; ; ; ; ;        > = = = = = = = 4 4 4 4 4 4H4 H4 H4 H4 H4'8 H4 H4 H4 H4 H4r   