
    Ng                        d Z ddlmZ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 ddlmZ dd	lmZ  G d
 de          Zej        j         ej         z   e_          G d de          Zej        j         ej         z   e_         dS )a
  [DEPRECATED]

## Zapier Natural Language Actions API
Full docs here: https://nla.zapier.com/start/

**Zapier Natural Language Actions** gives you access to the 5k+ apps, 20k+ actions
on Zapier's platform through a natural language API interface.

NLA supports apps like Gmail, Salesforce, Trello, Slack, Asana, HubSpot, Google Sheets,
Microsoft Teams, and thousands more apps: https://zapier.com/apps

Zapier NLA handles ALL the underlying API auth and translation from
natural language --> underlying API call --> return simplified output for LLMs
The key idea is you, or your users, expose a set of actions via an oauth-like setup
window, which you can then query and execute via a REST API.

NLA offers both API Key and OAuth for signing NLA API requests.

1. Server-side (API Key): for quickly getting started, testing, and production scenarios
    where LangChain will only use actions exposed in the developer's Zapier account
    (and will use the developer's connected accounts on Zapier.com)

2. User-facing (Oauth): for production scenarios where you are deploying an end-user
    facing application and LangChain needs access to end-user's exposed actions and
    connected accounts on Zapier.com

This quick start will focus on the server-side use case for brevity.
Review [full docs](https://nla.zapier.com/start/) for user-facing oauth developer
support.

Typically, you'd use SequentialChain, here's a basic example:

    1. Use NLA to find an email in Gmail
    2. Use LLMChain to generate a draft reply to (1)
    3. Use NLA to send the draft reply (2) to someone in Slack via direct message

In code, below:

```python

import os

# get from https://platform.openai.com/
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY", "")

# get from https://nla.zapier.com/docs/authentication/
os.environ["ZAPIER_NLA_API_KEY"] = os.environ.get("ZAPIER_NLA_API_KEY", "")

from langchain_community.agent_toolkits import ZapierToolkit
from langchain_community.utilities.zapier import ZapierNLAWrapper

## step 0. expose gmail 'find email' and slack 'send channel message' actions

# first go here, log in, expose (enable) the two actions:
#    https://nla.zapier.com/demo/start
#    -- for this example, can leave all fields "Have AI guess"
# in an oauth scenario, you'd get your own <provider> id (instead of 'demo')
# which you route your users through first

zapier = ZapierNLAWrapper()
## To leverage OAuth you may pass the value `nla_oauth_access_token` to
## the ZapierNLAWrapper. If you do this there is no need to initialize
## the ZAPIER_NLA_API_KEY env variable
# zapier = ZapierNLAWrapper(zapier_nla_oauth_access_token="TOKEN_HERE")
toolkit = ZapierToolkit.from_zapier_nla_wrapper(zapier)
```

    )AnyDictOptional)warn_deprecated)AsyncCallbackManagerForToolRunCallbackManagerForToolRun)BaseTool)pre_init)Field)BASE_ZAPIER_TOOL_PROMPT)ZapierNLAWrapperc                   H   e Zd ZU dZ ee          Zeed<   eed<   dZ	e
e         ed<   eZeed<   eed<    ee          Zeeef         ed	<   d
Zeed<   d
Zeed<   edeeef         deeef         fd            Z	 ddede
e         defdZ	 ddede
e         defdZdS )ZapierNLARunActionaz  Tool to run a specific action from the user's exposed actions.

    Params:
        action_id: a specific action ID (from list actions) of the action to execute
            (the set api_key must be associated with the action owner)
        instructions: a natural language instruction string for using the action
            (eg. "get the latest email from Mike Knoop" for "Gmail: find email" action)
        params: a dict, optional. Any params provided will *override* AI guesses
            from `instructions` (see "understanding the AI guessing flow" here:
            https://nla.zapier.com/docs/using-the-api#ai-guessing)

    default_factoryapi_wrapper	action_idNparamsbase_promptzapier_descriptionparams_schema namedescriptionvaluesreturnc           	      4   d         }d         }d|v r|d= ddh}t          fd|D                       st          d          |d<   d	                             |t          t	          |                                                    
          d<   S )Nr   r   instructionsz{zapier_description}z{params}c              3   ,   K   | ]}|d          v V  dS )r   N ).0fieldr   s     a/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/tools/zapier/tool.py	<genexpr>z:ZapierNLARunAction.set_name_description.<locals>.<genexpr>v   s-      PPe5F=11PPPPPP    z_Your custom base Zapier prompt must contain input fields for {zapier_description} and {params}.r   r   )r   r   r   )all
ValueErrorformatstrlistkeys)clsr   r   r   necessary_fieldss    `   r#   set_name_descriptionz'ZapierNLARunAction.set_name_descriptionm   s    #$89/]**n- 3J?PPPP?OPPPPP 	5  
 ,v &} 5 < <1tM..001122 != !
 !
} r%   r   run_managerc                 p    t          dd           | j                            | j        || j                  S EUse the Zapier NLA tool to return a list of all exposed user actions.0.0.319\This tool will be deprecated on 2023-11-17. See <https://nla.zapier.com/sunset/> for detailssincemessage)r   r   
run_as_strr   r   selfr   r/   s      r#   _runzZapierNLARunAction._run   sD     	?	
 	
 	
 	
 **4><UUUr%   c                    K   t          dd           | j                            | j        || j                   d{V S r2   r3   r4   r5   N)r   r   arun_as_strr   r   r9   s      r#   _arunzZapierNLARunAction._arun   so       	?	
 	
 	
 	
 %11NK
 
 
 
 
 
 
 
 	
r%   )N)__name__
__module____qualname____doc__r   r   r   __annotations__r)   r   r   dictr   r   r   r   r   r   r
   r   r.   r   r;   r   r?   r    r%   r#   r   r   V   s          %*E:J$K$K$KK!KKKNNN!FHTN!!!.K...$)E$$?$?$?M4S>???D#NNNK$sCx. T#s(^    X, UYV VV.67P.QV	V V V V  AE
 

 <=
 
	
 
 
 
 
 
r%   r   c                       e Zd ZU dZdZeed<   edz   Zeed<    e	e
          Ze
ed<   	 	 dd
edee         defdZ	 	 dd
edee         defdZd	S )ZapierNLAListActionsz.Tool to list all exposed actions for the user.ZapierNLA_list_actionsr   z7This tool returns a list of the user's exposed actions.r   r   r   r   N_r/   r   c                 V    t          dd           | j                                        S r1   )r   r   list_as_strr:   rI   r/   s      r#   r;   zZapierNLAListActions._run   s:     	?	
 	
 	
 	
 ++---r%   c                 f   K   t          dd           | j                                         d{V S r=   )r   r   alist_as_strrL   s      r#   r?   zZapierNLAListActions._arun   sP       	?	
 	
 	
 	
 %22444444444r%   )r   N)r@   rA   rB   rC   r   r)   rD   r   r   r   r   r   r   r   r;   r   r?   r    r%   r#   rG   rG      s         88(D#(((.AK    %*E:J$K$K$KK!KKK ;?. .. 78. 
	. . . ." @D5 55 <=5 
	5 5 5 5 5 5r%   rG   N)rC   typingr   r   r   langchain_core._apir   langchain_core.callbacksr   r   langchain_core.toolsr	   langchain_core.utilsr
   pydanticr   'langchain_community.tools.zapier.promptr   $langchain_community.utilities.zapierr   r   runrG   r*   r    r%   r#   <module>rX      sd  D DL ' & & & & & & & & & / / / / / /        * ) ) ) ) ) ) ) ) ) ) )       K K K K K K A A A A A AK
 K
 K
 K
 K
 K
 K
 K
^  #5#==  %5 %5 %5 %5 %58 %5 %5 %5R !$8$@@    r%   