a
    bg                     @   s   d Z ddlZddl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 G d	d
 d
eZG dd deZdS )zrequest parser.    N)Any)REQUEST_TEMPLATE)LLMChain)BaseLanguageModel)BaseOutputParser)PromptTemplatec                   @   sB   e Zd ZdZeedddZeedddZeedd	d
ZdS )APIRequesterOutputParser!Parse the request and error tags.)serialized_blockreturnc                 C   s2   zt t j|ddW S  t jy,   Y dS 0 d S )NF)strictzERROR serializing request.)jsondumpsloadsJSONDecodeError)selfr
    r   /var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/chains/openapi/requests_chain.py_load_json_block   s    z)APIRequesterOutputParser._load_json_block)
llm_outputr   c                 C   sT   t d|t j}|r(| |d S t d|t j}|rPd|d  S dS )r	   z```json(.*?)```   z```text(.*?)```z	MESSAGE: zERROR making request)researchDOTALLr   groupstrip)r   r   Z
json_matchZmessage_matchr   r   r   parse   s    zAPIRequesterOutputParser.parser   c                 C   s   dS )NZapi_requesterr   )r   r   r   r   _type"   s    zAPIRequesterOutputParser._typeN)	__name__
__module____qualname____doc__strr   r   propertyr   r   r   r   r   r      s
   r   c                   @   s>   e Zd ZdZeedddZed
eeee	e
dddZd	S )APIRequesterChainGet the request parser.r   c                 C   s   dS )NFr   )clsr   r   r   is_lc_serializable*   s    z$APIRequesterChain.is_lc_serializableT)llmtypescript_definitionverbosekwargsr   c                 K   s2   t  }tt|d|idgd}| f |||d|S )r&   ZschemaZinstructions)templateoutput_parserZpartial_variablesZinput_variables)promptr)   r+   )r   r   r   )r'   r)   r*   r+   r,   r.   r/   r   r   r   from_llm_and_typescript.   s    	z)APIRequesterChain.from_llm_and_typescriptN)T)r   r    r!   r"   classmethodboolr(   r   r#   r   r   r0   r   r   r   r   r%   '   s    r%   )r"   r   r   typingr   Z$langchain.chains.api.openapi.promptsr   Zlangchain.chains.llmr   Zlangchain_core.language_modelsr   Zlangchain_core.output_parsersr   Zlangchain_core.prompts.promptr   r   r%   r   r   r   r   <module>   s   