Skip to main content
本文帮助您快速上手 Gmail 工具包。该工具包与 Gmail API 交互,支持读取邮件、起草和发送邮件等操作。关于 GmailToolkit 所有功能和配置的详细文档,请访问 API 参考

配置

使用此工具包需要按照 Gmail API 文档中的说明设置凭据。下载 credentials.json 文件后,即可开始使用 Gmail API。

安装

此工具包位于 langchain-google-community 包中,需要安装 gmail 扩展:
pip install -qU langchain-google-community\[gmail\]
要启用单个工具的自动追踪,请设置您的 LangSmith API 密钥:
os.environ["LANGSMITH_TRACING"] = "true"
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")

实例化

默认情况下,工具包会读取本地的 credentials.json 文件。您也可以手动提供 Credentials 对象。
from langchain_google_community import GmailToolkit

toolkit = GmailToolkit()

自定义身份验证

在底层,系统会使用以下方法创建 googleapi 资源。 您可以手动构建 googleapi 资源以获得更多身份验证控制。
from langchain_google_community.gmail.utils import (
    build_resource_service,
    get_gmail_credentials,
)

# 可在此处查看权限范围:https://developers.google.com/gmail/api/auth/scopes
# 例如,只读权限范围为 'https://www.googleapis.com/auth/gmail.readonly'
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()
tools
[GmailCreateDraft(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>),
 GmailSendMessage(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>),
 GmailSearch(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>),
 GmailGetMessage(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>),
 GmailGetThread(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>)]

在 Agent 中使用

以下演示如何将工具包集成到 Agent 中。 需要一个 LLM 或聊天模型:
# | output: false
# | echo: false

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4.1-mini", temperature=0)
from langchain.agents import create_agent


agent_executor = create_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()
================================ Human Message =================================

Draft an email to fake@fake.com thanking them for coffee.
================================== Ai Message ==================================
Tool Calls:
  create_gmail_draft (call_slGkYKZKA6h3Mf1CraUBzs6M)
 Call ID: call_slGkYKZKA6h3Mf1CraUBzs6M
  Args:
    message: Dear Fake,

I 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!

Best regards,
[Your Name]
    to: ['fake@fake.com']
    subject: Thank You for the Coffee
================================= Tool Message =================================
Name: create_gmail_draft

Draft created. Draft Id: r-7233782721440261513
================================== Ai Message ==================================

I have drafted an email to fake@fake.com thanking them for the coffee. You can review and send it from your email draft with the subject "Thank You for the Coffee".

API 参考

关于 GmailToolkit 所有功能和配置的详细文档,请访问 API 参考