Skip to main content

Zapier自然语言操作API (Zapier Natural Language Actions API)

完整文档请参阅:https://nla.zapier.com/start/

Zapier自然语言操作通过自然语言API接口,让您可以访问Zapier平台上的5000多个应用和20000多个操作。

NLA支持的应用包括Gmail、Salesforce、Trello、Slack、Asana、HubSpot、Google Sheets、Microsoft Teams等成千上万个应用:https://zapier.com/apps

Zapier NLA负责处理所有底层API授权和自然语言翻译,将自然语言转换为底层API调用,并返回简化的LLMs输出。关键思想是您或您的用户通过类似OAuth的设置窗口公开一组操作,然后可以通过REST API查询和执行这些操作。

NLA提供API密钥和OAuth两种方式来签署NLA API请求。

  1. 服务器端(API密钥):用于快速入门、测试和生产场景,其中LangChain仅使用开发者在Zapier帐户中公开的操作(并且将使用开发者在Zapier.com上连接的帐户)。

  2. 用户界面(OAuth):用于生产场景,您正在部署面向最终用户的应用程序,LangChain需要访问最终用户在Zapier.com上公开的操作和连接的帐户。

为了简洁起见,本快速入门主要关注服务器端用例。请跳转到使用OAuth访问令牌的示例以查看如何设置面向用户的Zapier的简短示例。请查阅完整文档以获取完整的面向用户的OAuth开发者支持。

本示例介绍了如何使用Zapier集成的SimpleSequentialChainAgent。在下面的代码中:

import os

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

# 从https://nla.zapier.com/docs/authentication/登录后获取
os.environ["ZAPIER_NLA_API_KEY"] = os.environ.get("ZAPIER_NLA_API_KEY", "")

使用代理的示例 (Example with Agent)

Zapier工具可以与代理一起使用。请参考下面的示例。

from langchain.llms import OpenAI
from langchain.agents import initialize_agent
from langchain.agents.agent_toolkits import ZapierToolkit
from langchain.agents import AgentType
from langchain.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
llm = OpenAI(temperature=0)
zapier = ZapierNLAWrapper()
toolkit = ZapierToolkit.from_zapier_nla_wrapper(zapier)
agent = initialize_agent(
toolkit.get_tools(), llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)
agent.run(
"Summarize the last email I received regarding Silicon Valley Bank. Send the summary to the #test-zapier channel in slack."
)
    

> 进入新的 AgentExecutor 链...
我需要找到邮件并对其进行摘要。
操作:Gmail:查找邮件
操作输入:查找来自 Silicon Valley Bank 的最新邮件
观察结果:{"from__name": "Silicon Valley Bridge Bank, N.A.", "from__email": "sreply@svb.com", "body_plain": "Dear Clients, After chaotic, tumultuous & stressful days, we have clarity on path for SVB, FDIC is fully insuring all deposits & have an ask for clients & partners as we rebuild. Tim Mayopoulos <https://eml.svb.com/NjEwLUtBSy0yNjYAAAGKgoxUeBCLAyF_NxON97X4rKEaNBLG", "reply_to__email": "sreply@svb.com", "subject": "Meet the new CEO Tim Mayopoulos", "date": "Tue, 14 Mar 2023 23:42:29 -0500 (CDT)", "message_url": "https://mail.google.com/mail/u/0/#inbox/186e393b13cfdf0a", "attachment_count": "0", "to__emails": "ankush@langchain.dev", "message_id": "186e393b13cfdf0a", "labels": "IMPORTANT, CATEGORY_UPDATES, INBOX"}
思考:我需要对邮件进行摘要,并将其发送到 Slack 的 #test-zapier 频道。
操作:Slack:发送频道消息
操作输入:在 #test-zapier 频道中发送一条 Slack 消息,内容为“Silicon Valley Bank has announced that Tim Mayopoulos is the new CEO. FDIC is fully insuring all deposits and they have an ask for clients and partners as they rebuild.”
观察结果:{"message__text": "Silicon Valley Bank has announced that Tim Mayopoulos is the new CEO. FDIC is fully insuring all deposits and they have an ask for clients and partners as they rebuild.", "message__permalink": "https://langchain.slack.com/archives/C04TSGU0RA7/p1678859932375259", "channel": "C04TSGU0RA7", "message__bot_profile__name": "Zapier", "message__team": "T04F8K3FZB5", "message__bot_id": "B04TRV4R74K", "message__bot_profile__deleted": "false", "message__bot_profile__app_id": "A024R9PQM", "ts_time": "2023-03-15T05:58:52Z", "message__bot_profile__icons__image_36": "https://avatars.slack-edge.com/2022-08-02/3888649620612_f864dc1bb794cf7d82b0_36.png", "message__blocks[]block_id": "kdZZ", "message__blocks[]elements[]type": "['rich_text_section']"}
思考:我现在知道最终答案了。
最终答案:我已将最新一封来自 Silicon Valley Bank 的邮件摘要发送到 Slack 的 #test-zapier 频道。

> 链结束。

'我已将最新一封来自 Silicon Valley Bank 的邮件摘要发送到 Slack 的 #test-zapier 频道。'

使用SimpleSequentialChain的示例

如果您需要更明确的控制,请使用以下链式结构。

from langchain.llms import OpenAI
from langchain.chains import LLMChain, TransformChain, SimpleSequentialChain
from langchain.prompts import PromptTemplate
from langchain.tools.zapier.tool import ZapierNLARunAction
from langchain.utilities.zapier import ZapierNLAWrapper
## 步骤 0. 公开 Gmail 的“查找邮件”和 Slack 的“发送直接消息”操作

# 首先,转到此处,登录,公开(启用)这两个操作:https://nla.zapier.com/demo/start -- 对于此示例,可以将所有字段都设置为“让 AI 猜测”
# 在 OAuth 场景中,您将获得自己的 <provider> id(而不是 'demo'),您需要首先将用户引导到该 id

actions = ZapierNLAWrapper().list()
## 步骤 1. Gmail 查找邮件 (Step 1. Gmail find email)

GMAIL_SEARCH_INSTRUCTIONS = "从硅谷银行获取最新的电子邮件"


def nla_gmail(inputs):
action = next(
(a for a in actions if a["description"].startswith("Gmail: Find Email")), None
)
return {
"email_data": ZapierNLARunAction(
action_id=action["id"],
zapier_description=action["description"],
params_schema=action["params"],
).run(inputs["instructions"])
}


gmail_chain = TransformChain(
input_variables=["instructions"],
output_variables=["email_data"],
transform=nla_gmail,
)
## 步骤2. 生成草稿回复

```python
template = """您是一个助手,负责起草回复收到的电子邮件。以纯文本形式输出草稿回复(不是JSON格式)。

收到的电子邮件:
{email_data}

草稿回复:"""

prompt_template = PromptTemplate(input_variables=["email_data"], template=template)
reply_chain = LLMChain(llm=OpenAI(temperature=0.7), prompt=prompt_template)
## 步骤3. 通过Slack直接消息发送草稿回复

SLACK_HANDLE = "@Ankush Gola"


def nla_slack(inputs):
action = next(
(
a
for a in actions
if a["description"].startswith("Slack: Send Direct Message")
),
None,
)
instructions = f'在Slack中向{SLACK_HANDLE}发送以下内容:{inputs["draft_reply"]}'
return {
"slack_data": ZapierNLARunAction(
action_id=action["id"],
zapier_description=action["description"],
params_schema=action["params"],
).run(instructions)
}


slack_chain = TransformChain(
input_variables=["draft_reply"],
output_variables=["slack_data"],
transform=nla_slack,
)
## 最后,执行

```python
overall_chain = SimpleSequentialChain(
chains=[gmail_chain, reply_chain, slack_chain], verbose=True
)
overall_chain.run(GMAIL_SEARCH_INSTRUCTIONS)
    

> 进入新的SimpleSequentialChain链...
{"from__name": "Silicon Valley Bridge Bank, N.A.", "from__email": "sreply@svb.com", "body_plain": "亲爱的客户,经过混乱、动荡和紧张的日子,我们对SVB的道路有了明确的认识,FDIC完全保险所有存款,并且在我们重建时向客户和合作伙伴提出了要求。Tim Mayopoulos <https://eml.svb.com/NjEwLUtBSy0yNjYAAAGKgoxUeBCLAyF_NxON97X4rKEaNBLG", "reply_to__email": "sreply@svb.com", "subject": "Meet the new CEO Tim Mayopoulos", "date": "Tue, 14 Mar 2023 23:42:29 -0500 (CDT)", "message_url": "https://mail.google.com/mail/u/0/#inbox/186e393b13cfdf0a", "attachment_count": "0", "to__emails": "ankush@langchain.dev", "message_id": "186e393b13cfdf0a", "labels": "IMPORTANT, CATEGORY_UPDATES, INBOX"}

亲爱的Silicon Valley Bridge Bank,

感谢您的电子邮件和关于您的新CEO Tim Mayopoulos的更新。我们感谢您致力于让您的客户和合作伙伴保持了解,并期待继续与您保持合作关系。

最好的祝福,
[您的名字]
{"message__text": "亲爱的Silicon Valley Bridge Bank,\n\n感谢您的电子邮件和关于您的新CEO Tim Mayopoulos的更新。我们感谢您致力于让您的客户和合作伙伴保持了解,并期待继续与您保持合作关系。\n\n最好的祝福,\n[您的名字]", "message__permalink": "https://langchain.slack.com/archives/D04TKF5BBHU/p1678859968241629", "channel": "D04TKF5BBHU", "message__bot_profile__name": "Zapier", "message__team": "T04F8K3FZB5", "message__bot_id": "B04TRV4R74K", "message__bot_profile__deleted": "false", "message__bot_profile__app_id": "A024R9PQM", "ts_time": "2023-03-15T05:59:28Z", "message__blocks[]block_id": "p7i", "message__blocks[]elements[]elements[]type": "[['text']]", "message__blocks[]elements[]type": "['rich_text_section']"}

> 完成链的执行。


'{"message__text": "亲爱的Silicon Valley Bridge Bank,\\n\\n感谢您的电子邮件和关于您的新CEO Tim Mayopoulos的更新。我们感谢您致力于让您的客户和合作伙伴保持了解,并期待继续与您保持合作关系。\\n\\n最好的祝福,\\n[您的名字]", "message__permalink": "https://langchain.slack.com/archives/D04TKF5BBHU/p1678859968241629", "channel": "D04TKF5BBHU", "message__bot_profile__name": "Zapier", "message__team": "T04F8K3FZB5", "message__bot_id": "B04TRV4R74K", "message__bot_profile__deleted": "false", "message__bot_profile__app_id": "A024R9PQM", "ts_time": "2023-03-15T05:59:28Z", "message__blocks[]block_id": "p7i", "message__blocks[]elements[]elements[]type": "[[\'text\']]", "message__blocks[]elements[]type": "[\'rich_text_section\']"}'

使用OAuth访问令牌的示例

以下代码片段展示了如何使用已获取的OAuth访问令牌初始化包装器。请注意,参数是作为参数传递的,而不是设置环境变量。请查看身份验证文档以获取完整的面向用户的OAuth开发者支持。

开发人员负责处理OAuth握手以获取和刷新访问令牌。

llm = OpenAI(temperature=0)
zapier = ZapierNLAWrapper(zapier_nla_oauth_access_token="<填入访问令牌>")
toolkit = ZapierToolkit.from_zapier_nla_wrapper(zapier)
agent = initialize_agent(
toolkit.get_tools(), llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)

agent.run(
"总结我收到的关于Silicon Valley Bank的最后一封电子邮件。将摘要发送到slack的#test-zapier频道。"
)