o
    Zh                     @   s   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
 d
eZdS )    N)JSONDecodeError)ListUnion)AgentActionAgentActionMessageLogAgentFinish)OutputParserException)	AIMessageBaseMessage)ChatGeneration
Generation)AgentOutputParserc                	   @   s   e Zd ZdZedefddZedede	e
ef fddZdd	d
ee dede	e
ef fddZdede	e
ef fd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                 C   s   dS )Nzopenai-functions-agent )selfr   r   g/var/www/html/lang_env/lib/python3.10/site-packages/langchain/agents/output_parsers/openai_functions.py_type   s   z&OpenAIFunctionsAgentOutputParser._typemessagec                 C   s   t | tstdt|  | jdi }|ro|d }zt|d  dkr)i }n	tj	|d dd}W n t
yB   td| d	w d
|v rL|d
 }n|}| jrXd| j dnd}d| d| d| d}t|||| gdS td| jit| jd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.Z__arg1zresponded: 
z
Invoking: `z` with `z`
)Ztool
tool_inputlogZmessage_logoutput)Zreturn_valuesr   )
isinstancer	   	TypeErrortypeZadditional_kwargsgetlenstripjsonloadsr   r   contentr   r   str)r   r   Zfunction_nameZ_tool_inputr   Zcontent_msgr   r   r   r   _parse_ai_message    s:   


z2OpenAIFunctionsAgentOutputParser._parse_ai_messageF)partialresultr(   c                C   s*   t |d tstd|d j}| |S )Nr   z6This output parser only works on ChatGeneration output)r   r   
ValueErrorr   r'   )r   r)   r(   r   r   r   r   parse_resultO   s   

z-OpenAIFunctionsAgentOutputParser.parse_resulttextc                 C   s   t d)NzCan only parse messages)r*   )r   r,   r   r   r   parseW   s   z&OpenAIFunctionsAgentOutputParser.parseN)__name__
__module____qualname____doc__propertyr&   r   staticmethodr
   r   r   r   r'   r   r   boolr+   r-   r   r   r   r   r      s    /

r   )r#   r   typingr   r   Zlangchain_core.agentsr   r   r   Zlangchain_core.exceptionsr   Zlangchain_core.messagesr	   r
   Zlangchain_core.outputsr   r   Zlangchain.agents.agentr   r   r   r   r   r   <module>   s    