a
    bgH"                     @  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   v/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/chains/openapi/chain.pyr      s   
r   c                   @  sZ  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ddddZeddd d!Zd"dd#d$d%Zd"d"d#d&d'Zd"d(d#d)d*Zdd+d,d-d.Zdd+d+d/d0d1ZdBd2d3d"d4d5d6ZedCdddd7d8dd9d:d;d<d=ZedDdd7d8dddd>d9d:d?	d@dAZdS )E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_lengthr   )returnc                 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 dS )z3Expect output key.

        :meta private:
        intermediate_stepsNr+   r0   r4   r   r   r    output_keys2   s    z OpenAPIEndpointChain.output_keyszDict[str, str])argsr3   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   rA   )r5   r:   r   rB   r   r   r    _extract_query_paramsD   s
    z*OpenAPIEndpointChain._extract_query_paramszOptional[Dict[str, str]]c                 C  s8   d}| j jr4i }| j jD ]}||v r||||< q|S )z<Extract the request body params from the deserialized input.N)r(   r   rA   )r5   r:   r   rB   r   r   r    _extract_body_paramsL   s    z)OpenAPIEndpointChain._extract_body_paramsdict)serialized_argsr3   c                 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loadsrC   rE   rD   )r5   rG   r:   r?   r   r   r   r   r    deserialize_json_inputV   s    



z+OpenAPIEndpointChain.deserialize_json_input)r/   r7   r3   c                 C  s"   | j r| j|d|iS | j|iS dS )z$Return the output from the API call.r7   Nr8   )r5   r/   r7   r   r   r    _get_outpute   s
    z OpenAPIEndpointChain._get_outputzDict[str, Any]z$Optional[CallbackManagerForChainRun])inputsrun_managerr3   c              
   C  s  |p
t  }i }|| j }|d | j }| jj|| d}tt|}||d< |j	|dd| j
d |drx| ||S |dr| |tdd  |S z| |}t| j| jjj}	|	f i |}
|
jdkrt| jjj}|
j d	|
j d
|  d|d  d d|d   }n|
j}W n4 tyX } zdt| }W Y d }~n
d }~0 0 |d | j }||d< |j	|dd| j
d | jd ur| jj||| d}tt|}|j	|dd| j
d | ||S | ||S d S )N)r,   	callbacksrequest_argsgreen
)colorendverboseERRORzMESSAGE:   z: z
For z  rH   zCalled with args: rJ   zError with message response_textblue)responser,   rQ   yellow)r   Zget_noop_managerr.   r2   r"   Zpredict_and_parseZ	get_childr   r-   Zon_textrW   
startswithrN   lenrM   getattrr'   r$   methodvaluestatus_codereasonuppertext	Exceptionr#   )r5   rO   rP   Z_run_managerr7   r,   Z_api_argumentsZapi_argumentsrR   ra   Zapi_responseZ
method_strrZ   eZ_answerZanswerr   r   r    _callo   s`    






$

zOpenAPIEndpointChain._callr   zOptional[Requests]r   z'OpenAPIEndpointChain')spec_urlr?   ra   llmr'   r+   kwargsr3   c           	      K  s(   t |||}| j|f|||d|S )z;Create an OpenAPIEndpoint from a spec at the specified url.)r'   rk   r+   )r   Zfrom_openapi_urlfrom_api_operation)	clsrj   r?   ra   rk   r'   r+   rl   	operationr   r   r    from_url_and_method   s    z(OpenAPIEndpointChain.from_url_and_methodr   )	ro   rk   r'   rW   r+   raw_responserQ   rl   r3   c                 K  sn   t |j|j|jd}	tj|| ||d}
|r4d}ntj|||d}|pLt	 }| f |
||||	|||d|S )z<Create an OpenAPIEndpointChain from an operation and a spec.)r   r   r   )Ztypescript_definitionrW   rQ   N)rW   rQ   )r"   r#   r$   r'   r(   rW   r+   rQ   )
r   r   r   r   r	   Zfrom_llm_and_typescriptZto_typescriptr
   Zfrom_llmr   )rn   ro   rk   r'   rW   r+   rq   rQ   rl   r(   Zrequests_chainZresponse_chainZ	_requestsr   r   r    rm      s:    
	z'OpenAPIEndpointChain.from_api_operation)N)NF)NFFFN)r   r   r   r   r   r#   r   r   r'   r(   r+   r.   r0   r2   propertyr6   r9   rC   rD   rE   rM   rN   ri   classmethodrp   rm   r   r   r   r    r!      s@   


 7        r!   )#r   
__future__r   rK   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    