a
    bŠÝgt-  ã                   @   sx   d Z ddlZddlmZmZmZmZ ddlZddlZddl	m
Z
 ddlmZmZmZ ddlmZmZ G dd„ deƒZdS )	a  Util that can interact with Zapier NLA.

Full docs here: https://nla.zapier.com/start/

Note: this wrapper currently only implemented the `api_key` auth method for testing
and server-side production use cases (using the developer's connected accounts on
Zapier.com)

For use-cases where LangChain + Zapier NLA is powering a user-facing application, and
LangChain needs access to the end-user's connected accounts on Zapier.com, you'll need
to use oauth. Review the full docs above and reach out to nla@zapier.com for
developer support.
é    N)ÚAnyÚDictÚListÚOptional©Úget_from_dict_or_env)Ú	BaseModelÚ
ConfigDictÚmodel_validator)ÚRequestÚSessionc                   @   s¸  e Zd ZU dZeed< eed< dZeed< eddZe	eef dœd	d
„Z
edœdd„Zeeee	eef dœdd„Zd8eee	 e	dœdd„Zeedœdd„Zd9eeee	 edœdd„Zeddee	edœdd„ƒƒZee	 dœd d!„Zee	 dœd"d#„Zd:eeee	 e	dœd$d%„Zd;eeee	 e	dœd&d'„Zd<eeee	 e	dœd(d)„Zd=eeee	 e	dœd*d+„Zedœd,d-„Zedœd.d/„Zedœd0d1„Z edœd2d3„Z!edœd4d5„Z"edœd6d7„Z#dS )>ÚZapierNLAWrapperað  Wrapper for Zapier NLA.

    Full docs here: https://nla.zapier.com/start/

    This wrapper supports both API Key and OAuth Credential auth methods. API Key
    is the fastest way to get started using this wrapper.

    Call this wrapper with either `zapier_nla_api_key` or
    `zapier_nla_oauth_access_token` arguments, or set the `ZAPIER_NLA_API_KEY`
    environment variable. If both arguments are set, the Access Token will take
    precedence.

    For use-cases where LangChain + Zapier NLA is powering a user-facing application,
    and LangChain needs access to the end-user's connected accounts on Zapier.com,
    you'll need to use OAuth. Review the full docs above to learn how to create
    your own provider and generate credentials.
    Úzapier_nla_api_keyÚzapier_nla_oauth_access_tokenzhttps://nla.zapier.com/api/v1/Úzapier_nla_api_baseZforbid)Úextra)Úreturnc                 C   s<   dddœ}| j r(| dd| j › i¡ n| d| ji¡ |S )zFormat headers for requests.zapplication/json)ÚAcceptzContent-TypeÚAuthorizationzBearer z	X-API-Key)r   Úupdater   )ÚselfÚheaders© r   úr/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/utilities/zapier.pyÚ_format_headers4   s    þÿz ZapierNLAWrapper._format_headersc                 C   s   t  ¡ }|j |  ¡ ¡ |S )N)Úrequestsr   r   r   r   )r   Úsessionr   r   r   Ú_get_sessionD   s    zZapierNLAWrapper._get_session)ÚmethodÚurlÚkwargsr   c              
   Ë   sº   t j|  ¡ d4 I dH šˆ}|j||fi |¤Ž4 I dH š@}| ¡  | ¡ I dH W  d  ƒI dH  W  d  ƒI dH  S 1 I dH s‚0    Y  W d  ƒI dH  q¶1 I dH s¬0    Y  dS )zMake an async request.)r   N)ÚaiohttpZClientSessionr   ÚrequestÚraise_for_statusÚjson)r   r   r   r    r   Úresponser   r   r   Ú	_arequestI   s    zZapierNLAWrapper._arequestNF)ÚinstructionsÚparamsr   c                 C   s0   |r|ni }|  d|i¡ |r,|  ddi¡ |S )zCreate a payload for an action.r'   Úpreview_onlyT)r   )r   r'   r(   r)   Údatar   r   r   Ú_create_action_payloadP   s    ÿÿz'ZapierNLAWrapper._create_action_payload)Ú	action_idr   c                 C   s   | j d|› d S )zCreate a url for an action.úexposed/z	/execute/)r   )r   r,   r   r   r   Ú_create_action_url^   s    z#ZapierNLAWrapper._create_action_url)r,   r'   r(   r   c                 C   s"   |   |||¡}td|  |¡|dS )NÚPOST©r$   )r+   r   r.   )r   r,   r'   r(   r)   r*   r   r   r   Ú_create_action_requestb   s    ýz'ZapierNLAWrapper._create_action_requestÚbefore)Úmode)Úvaluesr   c                 C   s4   d}d|v rd}nd|d< t |dd|ƒ}||d< |S )z,Validate that api key exists in environment.Nr   Ú r   ZZAPIER_NLA_API_KEYr   )Úclsr4   Zzapier_nla_api_key_defaultr   r   r   r   Úvalidate_environmentp   s    üz%ZapierNLAWrapper.validate_environmentc                 Ã   s    |   d| jd ¡I dH }|d S )a´  Returns a list of all exposed (enabled) actions associated with
        current user (associated with the set api_key). Change your exposed
        actions here: https://nla.zapier.com/demo/start/

        The return list can be empty if no actions exposed. Else will contain
        a list of action objects:

        [{
            "id": str,
            "description": str,
            "params": Dict[str, str]
        }]

        `params` will always contain an `instructions` key, the only required
        param. All others optional and if provided will override any AI guesses
        (see "understanding the AI guessing flow" here:
        https://nla.zapier.com/api/v1/docs)
        ÚGETr-   NÚresults)r&   r   )r   r%   r   r   r   ÚalistŠ   s    zZapierNLAWrapper.alistc              
   C   s”   |   ¡ }z| | jd ¡}| ¡  W nb tjy† } zH|jdkrn| jrZtjd|› |d‚tjd|› |d‚|‚W Y d}~n
d}~0 0 | ¡ d S )aÇ  Returns a list of all exposed (enabled) actions associated with
        current user (associated with the set api_key). Change your exposed
        actions here: https://nla.zapier.com/demo/start/

        The return list can be empty if no actions exposed. Else will contain
        a list of action objects:

        [{
            "id": str,
            "description": str,
            "params": Dict[str, str]
        }]

        `params` will always contain an `instructions` key, the only required
        param. All others optional and if provided will override any AI guesses
        (see "understanding the AI guessing flow" here:
        https://nla.zapier.com/docs/using-the-api#ai-guessing)
        r-   i‘  zrAn unauthorized response occurred. Check that your access token is correct and doesn't need to be refreshed. Err: )r%   zLAn unauthorized response occurred. Check that your api key is correct. Err: Nr9   )	r   Úgetr   r#   r   Ú	HTTPErrorÚstatus_coder   r$   )r   r   r%   Úhttp_errr   r   r   Úlist    s*    
þüÿýzZapierNLAWrapper.listc                 C   s:   |   ¡ }|  |||¡}| | |¡¡}| ¡  | ¡ d S )á  Executes an action that is identified by action_id, must be exposed
        (enabled) by the current user (associated with the set api_key). Change
        your exposed actions here: https://nla.zapier.com/demo/start/

        The return JSON is guaranteed to be less than ~500 words (350
        tokens) making it safe to inject into the prompt of another LLM
        call.
        Úresult)r   r1   ÚsendÚprepare_requestr#   r$   ©r   r,   r'   r(   r   r"   r%   r   r   r   ÚrunÈ   s
    zZapierNLAWrapper.runc                 Ã   s,   | j d|  |¡|  ||¡dI dH }|d S )r@   r/   r0   NrA   ©r&   r.   r+   ©r   r,   r'   r(   r%   r   r   r   ÚarunÙ   s    
ýzZapierNLAWrapper.arunc                 C   sV   |   ¡ }|r|ni }| ddi¡ |  |||d¡}| | |¡¡}| ¡  | ¡ d S )úËSame as run, but instead of actually executing the action, will
        instead return a preview of params that have been guessed by the AI in
        case you need to explicitly review before executing.r)   TZinput_params)r   r   r1   rB   rC   r#   r$   rD   r   r   r   Úpreviewë   s    zZapierNLAWrapper.previewc                 Ã   s0   | j d|  |¡| j||dddI dH }|d S )rI   r/   T)r)   r0   NrA   rF   rG   r   r   r   Úapreviewù   s    ýzZapierNLAWrapper.apreviewc                 O   s   | j |i |¤Ž}t |¡S )úcSame as run, but returns a stringified version of the JSON for
        insertting back into an LLM.)rE   r$   Údumps©r   Úargsr    r*   r   r   r   Ú
run_as_str  s    zZapierNLAWrapper.run_as_strc                 Ï   s    | j |i |¤ŽI dH }t |¡S )rL   N)rH   r$   rM   rN   r   r   r   Úarun_as_str  s    zZapierNLAWrapper.arun_as_strc                 O   s   | j |i |¤Ž}t |¡S )úgSame as preview, but returns a stringified version of the JSON for
        insertting back into an LLM.)rJ   r$   rM   rN   r   r   r   Úpreview_as_str  s    zZapierNLAWrapper.preview_as_strc                 Ï   s    | j |i |¤ŽI dH }t |¡S )rR   N)rK   r$   rM   rN   r   r   r   Úapreview_as_str  s    z ZapierNLAWrapper.apreview_as_strc                 C   s   |   ¡ }t |¡S )údSame as list, but returns a stringified version of the JSON for
        insertting back into an LLM.)r?   r$   rM   ©r   Úactionsr   r   r   Úlist_as_str   s    zZapierNLAWrapper.list_as_strc                 Ã   s   |   ¡ I dH }t |¡S )rU   N)r:   r$   rM   rV   r   r   r   Úalist_as_str&  s    zZapierNLAWrapper.alist_as_str)NF)NF)N)N)N)N)$Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚ__annotations__r   r	   Zmodel_configr   r   r   r   r   r&   r   r+   r.   r   r1   r
   Úclassmethodr7   r   r:   r?   rE   rH   rJ   rK   rP   rQ   rS   rT   rX   rY   r   r   r   r   r      sp   
ÿ ÿþ  ûú) ÿ
þ ÿ
þ ÿ
þ ÿ
þþr   )r]   r$   Útypingr   r   r   r   r!   r   Zlangchain_core.utilsr   Zpydanticr   r	   r
   r   r   r   r   r   r   r   Ú<module>   s   