a
    ~g                     @  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                   @  sd   e Zd ZU dZeZded< edej	Z
ded< dddd	Z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   s/var/www/html/emsaiapi.evdpl.com/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rtj|d dd}t|trLt	d| |d }|d dkrlt
d|d	 i|W S t|d |d	i |W S nt
d|i|W S W n4 ty } ztd
| |W Y d }~n
d }~0 0 d S )N   F)strictz!Got multiple action responses: %sr   actionzFinal Answeroutputaction_inputCould not parse LLM output: )r   searchjsonloadsgroupstrip
isinstancelistloggerwarningr   r   get	Exceptionr   )r   r   action_matchresponseer   r   r   parse!   s    
z StructuredChatOutputParser.parsec                 C  s   dS )Nstructured_chatr   r   r   r   r   _type5   s    z StructuredChatOutputParser._typeN)__name__
__module____qualname____doc__r   r   __annotations__recompileDOTALLr   r   r1   propertyr3   r   r   r   r   r      s   
r   c                   @  sx   e Zd ZU dZeedZded< dZded< dd	d
dZ	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   B   s    z=StructuredChatOutputParserWithRetries.get_format_instructionsr   r   c              
   C  sd   z*| j d ur| j |}n| j|}|W S  ty^ } ztd| |W Y d }~n
d }~0 0 d S )Nr"   )r@   r1   r?   r-   r   )r   r   Z
parsed_objr0   r   r   r   r1   E   s    

z+StructuredChatOutputParserWithRetries.parsezOptional[BaseLanguageModel]z$Optional[StructuredChatOutputParser])llmr?   r   c                 C  sF   |d ur*|pt  }tj||d}| |dS |d ur<| |dS |  S d S )N)rA   parser)r@   )r?   )r   r   from_llm)clsrA   r?   r@   r   r   r   rC   Q   s    


z.StructuredChatOutputParserWithRetries.from_llmc                 C  s   dS )NZstructured_chat_with_retriesr   r   r   r   r   r3   b   s    z+StructuredChatOutputParserWithRetries._type)NN)r4   r5   r6   r7   r
   r   r?   r8   r@   r   r1   classmethodrC   r<   r3   r   r   r   r   r=   :   s   
  r=   )
__future__r   r$   loggingr9   typingr   r   r   langchain_core.agentsr   r   langchain_core.exceptionsr   langchain_core.language_modelsr	   pydanticr
   langchain.agents.agentr   'langchain.agents.structured_chat.promptr   Zlangchain.output_parsersr   	getLoggerr4   r*   r   r=   r   r   r   r   <module>   s   
&