a
    ag                     @   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                   @   sz   e Zd ZdZeedddZeee	e
ef dddZdd	ee ee	e
ef d
ddZee	e
ef d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   ~/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain/agents/output_parsers/openai_functions.py_type   s    z&OpenAIFunctionsAgentOutputParser._type)messager   c                 C   s   t | tstdt|  | jdi }|r|d }z0t|d  dkrRi }ntj	|d dd}W n" t
y   td| d	Y n0 d
|v r|d
 }n|}| jrd| 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    s6    



z2OpenAIFunctionsAgentOutputParser._parse_ai_messageF)partial)resultr(   r   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)textr   c                 C   s   t dd S )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   