o
    Zh                     @  s   d dl mZ d dlZd dlZd dlZd dlmZ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 eeZG dd deZG dd deZdS )    )annotationsN)OptionalPatternUnion)AgentActionAgentFinish)OutputParserException)BaseLanguageModel)Field)AgentOutputParserFORMAT_INSTRUCTIONS)OutputFixingParserc                   @  sZ   e Zd ZU dZeZded< 	 edej	Z
ded< 	 ddd	ZdddZe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   e/var/www/html/lang_env/lib/python3.10/site-packages/langchain/agents/structured_chat/output_parser.pyget_format_instructions   s   z2StructuredChatOutputParser.get_format_instructionstextUnion[AgentAction, AgentFinish]c              
   C  s   zJ| j |}|d urCtj|d dd}t|tr&t	d| |d }|d dkr6t
d|d	 i|W S t|d |d	i |W S t
d|i|W S  ty^ } ztd
| |d }~ww )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er   r   r   parse!   s"   
z StructuredChatOutputParser.parsec                 C     dS )NZstructured_chatr   r   r   r   r   _type5      z StructuredChatOutputParser._typeNr   r   r   r   r   r   )__name__
__module____qualname____doc__r   r   __annotations__recompileDOTALLr   r   r-   propertyr/   r   r   r   r   r      s   
 

r   c                   @  sj   e Zd ZU dZeedZded< 	 dZded< 	 dd
dZ	dddZ
e		ddddZe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   B   s   z=StructuredChatOutputParserWithRetries.get_format_instructionsr   r   c              
   C  sX   z| j d ur| j |}|W S | j|}|W S  ty+ } ztd| |d }~ww )Nr   )r?   r-   r>   r*   r   )r   r   Z
parsed_objr,   r   r   r   r-   E   s   

z+StructuredChatOutputParserWithRetries.parsellmOptional[BaseLanguageModel]$Optional[StructuredChatOutputParser]c                 C  sB   |d ur|pt  }tj||d}| |dS |d ur| |dS |  S )N)r@   parser)r?   )r>   )r   r   from_llm)clsr@   r>   r?   r   r   r   rD   Q   s   


z.StructuredChatOutputParserWithRetries.from_llmc                 C  r.   )NZstructured_chat_with_retriesr   r   r   r   r   r/   b   r0   z+StructuredChatOutputParserWithRetries._typer1   r2   )NN)r@   rA   r>   rB   r   r<   )r3   r4   r5   r6   r
   r   r>   r7   r?   r   r-   classmethodrD   r;   r/   r   r   r   r   r<   :   s   
 

r<   )
__future__r   r!   loggingr8   typingr   r   r   Zlangchain_core.agentsr   r   Zlangchain_core.exceptionsr   Zlangchain_core.language_modelsr	   Zpydanticr
   Zlangchain.agents.agentr   Z'langchain.agents.structured_chat.promptr   Zlangchain.output_parsersr   	getLoggerr3   r'   r   r<   r   r   r   r   <module>   s    
&