
    3fi<                         d dl 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 d dlmZ  G d	 d
e      Zy)    N)JSONDecodeError)AgentActionAgentActionMessageLogAgentFinish)OutputParserException)	AIMessageBaseMessage)ChatGeneration
Generation)override)AgentOutputParserc            	           e Zd ZdZedefd       Zedede	e
z  fd       Zedddee   d	ede	e
z  fd
       Zedede	e
z  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    v/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_classic/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_messagez1OpenAIFunctionsAgentOutputParser.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/!)##$$W--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   r5   r   listr   boolr:   r=   r   r   r   r   r      s    	 (s ( ( /
+ /
+2K /
 /
b 
 	
.Z 
. 	
.
 
{	"
. 
. # +";  r   r   )r,   r   langchain_core.agentsr   r   r   langchain_core.exceptionsr   langchain_core.messagesr   r	   langchain_core.outputsr
   r   typing_extensionsr   langchain_classic.agents.agentr   r   r   r   r   <module>rL      s3       Q Q ; > & <R'8 Rr   