o
    ZhH"                     @  s   d Z ddlmZ ddlZddlmZmZmZmZ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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  G dd deZ!G dd deeZ"dS )zMChain that makes API calls and summarizes the responses to answer a question.    )annotationsN)AnyDictList
NamedTupleOptionalcast)APIRequesterChain)APIResponderChain)Chain)LLMChain)CallbackManagerForChainRun	Callbacks)BaseLanguageModel)	BaseModelField)Response)APIOperation)Requestsc                   @  s*   e Zd ZU dZded< ded< ded< dS )_ParamMappingz/Mapping from parameter name to parameter value.	List[str]query_paramsbody_paramspath_paramsN)__name__
__module____qualname____doc____annotations__ r   r   _/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/chains/openapi/chain.pyr      s
   
 r   c                   @  s  e Zd ZU dZded< dZded< ded< ed	ed
Zded< eddZ	ded< dZ
ded< dZded< dZded< eddZded< edJddZedJd d!ZdKd$d%ZdLd&d'ZdMd)d*ZdNd-d.ZdOd0d1Z	dPdQd6d7Ze		dRdSdAdBZe					dTdUdHdIZdS )VOpenAPIEndpointChainz@Chain interacts with an OpenAPI endpoint using natural language.r   api_request_chainNzOptional[LLMChain]api_response_chainr   api_operationT)excludedefault_factoryr   requestsparam_mapping)aliasr   Fboolreturn_intermediate_stepsinstructionsstrinstructions_keyoutput
output_keyr   )gezOptional[int]max_text_lengthreturnr   c                 C  s   | j gS )z2Expect input key.

        :meta private:
        )r.   selfr   r   r    
input_keys*   s   zOpenAPIEndpointChain.input_keysc                 C  s   | j s| jgS | jdgS )z3Expect output key.

        :meta private:
        intermediate_stepsr+   r0   r4   r   r   r    output_keys2   s   
z OpenAPIEndpointChain.output_keysargsDict[str, str]c              	   C  sB   | j j| j j }| jjD ]}|d| dt||d}q|S )z/Construct the path from the deserialized input.{} )r$   base_urlpathr(   r   replacer-   pop)r5   r:   r@   paramr   r   r    _construct_path=   s   "z$OpenAPIEndpointChain._construct_pathc                 C  s,   i }| j jD ]}||v r||||< q|S )z5Extract the query params from the deserialized input.)r(   r   rB   )r5   r:   r   rC   r   r   r    _extract_query_paramsD   s   z*OpenAPIEndpointChain._extract_query_paramsOptional[Dict[str, str]]c                 C  s8   d}| j jri }| j jD ]}||v r||||< q|S )z<Extract the request body params from the deserialized input.N)r(   r   rB   )r5   r:   r   rC   r   r   r    _extract_body_paramsL   s   z)OpenAPIEndpointChain._extract_body_paramsserialized_argsdictc                 C  s4   t |}| |}| |}| |}|||dS )zUse the serialized typescript dictionary.

        Resolve the path, query params dict, and optional requestBody dict.
        )urldataparams)jsonloadsrD   rG   rE   )r5   rH   r:   r@   r   r   r   r   r    deserialize_json_inputV   s   



z+OpenAPIEndpointChain.deserialize_json_inputr7   c                 C  s   | j r
| j|d|iS | j|iS )z$Return the output from the API call.r7   r8   )r5   r/   r7   r   r   r    _get_outpute   s
   
z OpenAPIEndpointChain._get_outputinputsDict[str, Any]run_manager$Optional[CallbackManagerForChainRun]c              
   C  s  |pt  }i }|| j }|d | j }| jj|| d}tt|}||d< |j	|dd| j
d |dr<| ||S |drM| |tdd  |S zD| |}t| j| jjj}	|	di |}
|
jdkrt| jjj}|
j d	|
j d
|  d|d  d d|d   }n|
j}W n ty } zdt| }W Y d }~nd }~ww |d | j }||d< |j	|dd| j
d | jd ur| jj||| d}tt|}|j	|dd| j
d | ||S | ||S )N)r,   	callbacksrequest_argsgreen
)colorendverboseERRORzMESSAGE:   z: z
For z  rJ   zCalled with args: rL   zError with message response_textblue)responser,   rU   yellowr   )r   Zget_noop_managerr.   r2   r"   Zpredict_and_parseZ	get_childr   r-   Zon_textr[   
startswithrP   lenrO   getattrr'   r$   methodvaluestatus_codereasonuppertext	Exceptionr#   )r5   rQ   rS   Z_run_managerr7   r,   Z_api_argumentsZapi_argumentsrV   re   Zapi_responseZ
method_strr^   eZ_answerZanswerr   r   r    _callo   sf   









zOpenAPIEndpointChain._callspec_urlr@   re   llmr   Optional[Requests]kwargsr   'OpenAPIEndpointChain'c           	      K  s(   t |||}| j|f|||d|S )z;Create an OpenAPIEndpoint from a spec at the specified url.)r'   ro   r+   )r   Zfrom_openapi_urlfrom_api_operation)	clsrn   r@   re   ro   r'   r+   rq   	operationr   r   r    from_url_and_method   s   z(OpenAPIEndpointChain.from_url_and_methodru   r[   raw_responserU   r   c                 K  sn   t |j|j|jd}	tj|| ||d}
|rd}ntj|||d}|p&t	 }| d|
||||	|||d|S )z<Create an OpenAPIEndpointChain from an operation and a spec.)r   r   r   )Ztypescript_definitionr[   rU   N)r[   rU   )r"   r#   r$   r'   r(   r[   r+   rU   r   )
r   r   r   r   r	   Zfrom_llm_and_typescriptZto_typescriptr
   Zfrom_llmr   )rt   ru   ro   r'   r[   r+   rw   rU   rq   r(   Zrequests_chainZresponse_chainZ	_requestsr   r   r    rs      s:   
	z'OpenAPIEndpointChain.from_api_operation)r3   r   )r:   r;   r3   r-   )r:   r;   r3   r;   )r:   r;   r3   rF   )rH   r-   r3   rI   )r/   r-   r7   rI   r3   rI   )N)rQ   rR   rS   rT   r3   r;   )NF)rn   r-   r@   r-   re   r-   ro   r   r'   rp   r+   r*   rq   r   r3   rr   )NFFFN)ru   r   ro   r   r'   rp   r[   r*   r+   r*   rw   r*   rU   r   rq   r   r3   rr   )r   r   r   r   r   r#   r   r   r'   r(   r+   r.   r0   r2   propertyr6   r9   rD   rE   rG   rO   rP   rm   classmethodrv   rs   r   r   r   r    r!      sB   
 






7r!   )#r   
__future__r   rM   typingr   r   r   r   r   r   Z+langchain.chains.api.openapi.requests_chainr	   Z+langchain.chains.api.openapi.response_chainr
   Zlangchain.chains.baser   Zlangchain.chains.llmr   Zlangchain_core.callbacksr   r   Zlangchain_core.language_modelsr   Zpydanticr   r   r'   r   Z2langchain_community.tools.openapi.utils.api_modelsr   Z&langchain_community.utilities.requestsr   r   r!   r   r   r   r    <module>   s      