a
    •˜ëj  ã                   @  sÒ   d dl mZ d dlZd dlZd dl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 d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ e e¡ZG dd„ deƒZG dd„ deƒZdS )é    )ÚannotationsN)ÚPattern)ÚOptionalÚUnion)ÚAgentActionÚAgentFinish)ÚOutputParserException)ÚBaseLanguageModel)ÚField)Úoverride)ÚAgentOutputParser©ÚFORMAT_INSTRUCTIONS)ÚOutputFixingParserc                   @  sl   e Zd ZU dZeZded< e dej	¡Z
ded< eddœdd	„ƒZedd
dœdd„ƒZeddœdd„ƒZdS )ÚStructuredChatOutputParserz,Output parser for the structured chat agent.ÚstrÚformat_instructionsz```(?:json\s+)?(\W.*?)```r   Úpattern©Úreturnc                 C  s   | j S )z<Returns formatting instructions for the given output parser.)r   ©Úself© r   úm/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/agents/structured_chat/output_parser.pyÚget_format_instructions   s    z2StructuredChatOutputParser.get_format_instructionsúUnion[AgentAction, AgentFinish]©Útextr   c              
   C  sÒ   z”| j  |¡}|d ur†tj| d¡ ¡ dd�}t|tƒrLt 	d|¡ |d }|d dkrlt
d|d	 i|ƒW S t|d | d	i ¡|ƒW S t
d|i|ƒW S  tyÌ } z d
|› �}t|ƒ|‚W Y d }~n
d }~0 0 d S )Né   F)Ústrictz!Got multiple action responses: %sr   ÚactionzFinal AnswerÚoutputZaction_inputúCould not parse LLM output: )r   ÚsearchÚjsonÚloadsÚgroupÚstripÚ
isinstanceÚlistÚloggerÚwarningr   r   ÚgetÚ	Exceptionr   )r   r   Zaction_matchÚresponseÚeÚmsgr   r   r   Úparse$   s$    

ý
z StructuredChatOutputParser.parsec                 C  s   dS )NZstructured_chatr   r   r   r   r   Ú_type:   s    z StructuredChatOutputParser._typeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__annotations__ÚreÚcompileÚDOTALLr   r   r   r1   Úpropertyr2   r   r   r   r   r      s   
r   c                   @  s€   e Zd ZU dZeed�Zded< dZded< e	dd	œd
d„ƒZ
e	dddœdd„ƒZedddd dœdd„ƒZedd	œdd„ƒZdS )Ú%StructuredChatOutputParserWithRetriesz9Output parser with retries for the structured chat agent.)Údefault_factoryr   Úbase_parserNzOptional[OutputFixingParser]Úoutput_fixing_parserr   r   c                 C  s   t S )Nr   r   r   r   r   r   G   s    z=StructuredChatOutputParserWithRetries.get_format_instructionsr   r   c              
   C  sd   z&| j d ur| j  |¡W S | j |¡W S  ty^ } z d|› �}t|ƒ|‚W Y d }~n
d }~0 0 d S )Nr"   )r?   r1   r>   r-   r   )r   r   r/   r0   r   r   r   r1   K   s    

z+StructuredChatOutputParserWithRetries.parsezOptional[BaseLanguageModel]z$Optional[StructuredChatOutputParser])Úllmr>   r   c                 C  sB   |dur*|pt ƒ }tj||d�}| |d�S |dur<| |d�S | ƒ S )a%  Create a StructuredChatOutputParserWithRetries from a language model.

        Args:
            llm: The language model to use.
            base_parser: An optional StructuredChatOutputParser to use.

        Returns:
            An instance of StructuredChatOutputParserWithRetries.
        N)r@   Úparser)r?   )r>   )r   r   Úfrom_llm)Úclsr@   r>   r?   r   r   r   rB   U   s    
þ

z.StructuredChatOutputParserWithRetries.from_llmc                 C  s   dS )NZstructured_chat_with_retriesr   r   r   r   r   r2   o   s    z+StructuredChatOutputParserWithRetries._type)NN)r3   r4   r5   r6   r
   r   r>   r7   r?   r   r   r1   ÚclassmethodrB   r;   r2   r   r   r   r   r<   ?   s   
	  ýr<   )Ú
__future__r   r$   Úloggingr8   r   Útypingr   r   Zlangchain_core.agentsr   r   Zlangchain_core.exceptionsr   Zlangchain_core.language_modelsr	   Zpydanticr
   Ztyping_extensionsr   Zlangchain.agents.agentr   Z'langchain.agents.structured_chat.promptr   Zlangchain.output_parsersr   Ú	getLoggerr3   r*   r   r<   r   r   r   r   Ú<module>   s    
)