Gmail 工具包
这将帮助你开始使用GMail 工具包。此工具包与GMail API交互,用于读取消息、草拟和发送消息等操作。如需详细了解所有GmailToolkit功能和配置,请参阅API参考文档。
设置
要使用此工具包,您需要设置在Gmail API 文档中解释的凭据。一旦您下载了credentials.json文件,就可以开始使用 Gmail API。
安装
这个工具包位于langchain-google-community包中。我们需要gmail额外依赖项:
%pip install -qU langchain-google-community\[gmail\]
要启用单个工具的自动跟踪,请设置您的LangSmithAPI密钥:
# os.environ["LANGSMITH_TRACING"] = "true"
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
Instantiation
默认情况下,工具包会读取本地的 credentials.json 文件。您也可以手动提供一个 Credentials 对象。
from langchain_google_community import GmailToolkit
toolkit = GmailToolkit()
API 参考:Gmail 工具包
自定义身份验证
在幕后,会创建一个 `0` 资源,并使用以下方法。您可以手动构建一个 `1` 资源以获得更多的认证控制。
from langchain_google_community.gmail.utils import (
build_resource_service,
get_gmail_credentials,
)
# Can review scopes here https://developers.google.com/gmail/api/auth/scopes
# For instance, readonly scope is '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)
API 参考:构建资源服务 |get_gmail_credentials
工具
查看可用工具:
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>)]
使用于代理
下方我们将展示如何将工具包集成到一个代理中。
我们需要一个大语言模型或聊天模型:
选择 聊天模型:
pip install -qU "langchain[openai]"
import getpass
import os
if not os.environ.get("OPENAI_API_KEY"):
os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter API key for OpenAI: ")
from langchain.chat_models import init_chat_model
llm = init_chat_model("gpt-4o-mini", model_provider="openai")
from langgraph.prebuilt import create_react_agent
agent_executor = create_react_agent(llm, tools)
API 参考:create_react_agent
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()
================================[1m Human Message [0m=================================
Draft an email to fake@fake.com thanking them for coffee.
==================================[1m Ai Message [0m==================================
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
=================================[1m Tool Message [0m=================================
Name: create_gmail_draft
Draft created. Draft Id: r-7233782721440261513
==================================[1m Ai Message [0m==================================
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参考。