
    h!                         d dl Z d dl mZ d dl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)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    n/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain/agents/output_parsers/openai_functions.py_typez&OpenAIFunctionsAgentOutputParser._type   s    '    messagec                 >   t        | t              sdt        |        }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}d| d	}t        |      |d
}~w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.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_messagez2OpenAIFunctionsAgentOutputParser._parse_ai_message    sT    '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   <C< <	DDDF)partialresultr6   c                    t        |d   t              sd}t        |      |d   j                  }| j	                  |      S )Nr   z6This output parser only works on ChatGeneration output)r%   r   
ValueErrorr   r5   )r   r7   r6   r0   r   s        r   parse_resultz-OpenAIFunctionsAgentOutputParser.parse_resultR   sA     &)^4JCS/!)##%%g..r   textc                     d}t        |      )NzCan only parse messages)r9   )r   r;   r0   s      r   parsez&OpenAIFunctionsAgentOutputParser.parse^   s    'or   N)__name__
__module____qualname____doc__propertyr/   r   staticmethodr
   r   r   r   r5   listr   boolr:   r=   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>rL      s3        Q Q ; > 4P'8 Pr   