a
    •˜ëj�	  ã                   @   st   d dl Z d dlZd dlmZ d dlmZ d dlmZmZ d dlm	Z	 d dl
mZ d dlmZ dZG d	d
„ d
eƒZdS )é    N)ÚPattern)ÚUnion)ÚAgentActionÚAgentFinish)ÚOutputParserException)ÚAgentOutputParser)ÚFORMAT_INSTRUCTIONSzFinal Answer:c                   @   sl   e Zd ZU dZeZeed< e 	dej
¡Zeed< edœdd„Zeeeef dœd	d
„Zeedœdd„ƒZdS )ÚChatOutputParserz!Output parser for the chat agent.Úformat_instructionsz ^.*?`{3}(?:json)?\n(.*?)`{3}.*?$Úpattern)Úreturnc                 C   s   | j S )z<Returns formatting instructions for the given output parser.)r
   ©Úself© r   úb/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/agents/chat/output_parser.pyÚget_format_instructions   s    z(ChatOutputParser.get_format_instructions)Útextr   c           
   
   C   sÚ   t |v }zp| j |¡}|s&d}t|ƒ‚| d¡}t | ¡ ¡}d|v }|r`|r`d|› �}t|ƒ‚t	|d | 
di ¡|ƒW S  tyÔ } zD|s d|› �}t|ƒ|‚| t ¡d  ¡ }	td|	i|ƒW  Y d	}~S d	}~0 0 d	S )
a\  Parse the output from the agent into an AgentAction or AgentFinish object.

        Args:
            text: The text to parse.

        Returns:
            An AgentAction or AgentFinish object.

        Raises:
            OutputParserException: If the output could not be parsed.
            ValueError: If the action could not be found.
        zaction not foundé   ÚactionzDParsing LLM output produced a final answer and a parse-able action: Zaction_inputzCould not parse LLM output: éÿÿÿÿÚoutputN)ÚFINAL_ANSWER_ACTIONr   ÚsearchÚ
ValueErrorÚgroupÚjsonÚloadsÚstripr   r   ÚgetÚ	ExceptionÚsplitr   )
r   r   Zincludes_answerÚfoundÚmsgr   ÚresponseZincludes_actionÚexcr   r   r   r   Úparse   s4    
ÿÿ
ý

zChatOutputParser.parsec                 C   s   dS )NZchatr   r   r   r   r   Ú_typeF   s    zChatOutputParser._typeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r
   ÚstrÚ__annotations__ÚreÚcompileÚDOTALLr   r   r   r   r   r   r%   Úpropertyr&   r   r   r   r   r	      s   
*r	   )r   r-   r   Útypingr   Zlangchain_core.agentsr   r   Zlangchain_core.exceptionsr   Zlangchain.agents.agentr   Zlangchain.agents.chat.promptr   r   r	   r   r   r   r   Ú<module>   s   