
    @h!                         S SK r S SK Jr  S SKJr  S SKJrJrJr  S SKJ	r	  S SK
JrJr  S SKJrJr  S SKJr   " S	 S
\5      rg)    N)JSONDecodeError)Union)AgentActionAgentActionMessageLogAgentFinish)OutputParserException)	AIMessageBaseMessage)ChatGeneration
Generation)AgentOutputParserc            	           \ rS rSrSr\S\4S j5       r\S\	S\
\\4   4S j5       rSS.S	\\   S
\S\
\\4   4S jjrS\S\
\\4   4S jrSrg) OpenAIFunctionsAgentOutputParser   aZ  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                     g)Nzopenai-functions-agent )selfs    h/var/www/html/shao/venv/lib/python3.13/site-packages/langchain/agents/output_parsers/openai_functions.py_type&OpenAIFunctionsAgentOutputParser._type   s    '    messagec                 V   [        U [        5      (       d  S[        U 5       3n[        U5      eU R                  R                  S0 5      nU(       a  US   n [        US   R                  5       5      S:X  a  0 nO[        R                  " US   SS9n SU;   a  US   nOUnU R                  (       a  SU R                   S3OSnSU SU SU S3n[        UUUU /S9$ [        SU R                  0[        U R                  5      S9$ ! [         a  nSU S	3n[        U5      UeS
nAff = f)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.N__arg1zresponded: 
z
Invoking: `z` with `z`
)tool
tool_inputlogmessage_logoutput)return_valuesr#   )
isinstancer	   type	TypeErroradditional_kwargsgetlenstripjsonloadsr   r   contentr   r   str)	r   msgr   function_name_tool_inputer"   content_msgr#   s	            r   _parse_ai_message2OpenAIFunctionsAgentOutputParser._parse_ai_message    sW    '9--/W?CC. 1155orJ)&1M8}[1779:a?"$K #'**];-GPU"VK ;&(2
(
?FK'8;TXK!-C}TVWC("%$I	  #W__5GOO$
 	
7 # 82=/ B9 :  ,C0a78s   "D ;D 
D(D##D(F)partialresultr9   c                    [        US   [        5      (       d  Sn[        U5      eUS   R                  nU R	                  U5      $ )Nr   z6This output parser only works on ChatGeneration output)r'   r   
ValueErrorr   r7   )r   r:   r9   r2   r   s        r   parse_result-OpenAIFunctionsAgentOutputParser.parse_resultR   sD     &)^44JCS/!)##%%g..r   textc                     Sn[        U5      e)NzCan only parse messages)r<   )r   r?   r2   s      r   parse&OpenAIFunctionsAgentOutputParser.parse^   s    'or   r   N)__name__
__module____qualname____firstlineno____doc__propertyr1   r   staticmethodr
   r   r   r   r7   listr   boolr=   rA   __static_attributes__r   r   r   r   r      s    	 (s ( ( /
; /
5k9Q3R /
 /
j 	
/Z 
/ 	
/
 
{K'	(
/# %[(@"A r   r   )r.   r   typingr   langchain_core.agentsr   r   r   langchain_core.exceptionsr   langchain_core.messagesr	   r
   langchain_core.outputsr   r   langchain.agents.agentr   r   r   r   r   <module>rS      s3        Q Q ; > 4P'8 Pr   