Skip to main content

Gmail工具包 (Gmail Toolkit)

这个笔记本介绍了如何将LangChain邮件连接到Gmail API。

要使用这个工具包,您需要按照Gmail API文档中的说明设置您的凭据。一旦您下载了credentials.json文件,就可以开始使用Gmail API。完成这一步后,我们将安装所需的库。

pip install --upgrade google-api-python-client > /dev/null
pip install --upgrade google-auth-oauthlib > /dev/null
pip install --upgrade google-auth-httplib2 > /dev/null
pip install beautifulsoup4 > /dev/null # 这是可选的,但对于解析HTML消息很有用

创建工具包 (Create the Toolkit)

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

from langchain.agents.agent_toolkits import GmailToolkit

toolkit = GmailToolkit()

自定义身份验证 (Customizing Authentication)

在幕后,使用以下方法创建了一个googleapi资源。您可以手动构建一个googleapi资源以获得更多的身份验证控制。

from langchain.tools.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(name='create_gmail_draft', description='Use this tool to create a draft email with the provided message fields.', args_schema=<class 'langchain.tools.gmail.create_draft.CreateDraftSchema'>, return_direct=False, verbose=False, callbacks=None, callback_manager=None, api_resource=<googleapiclient.discovery.Resource object at 0x10e5c6d10>),
GmailSendMessage(name='send_gmail_message', description='Use this tool to send email messages. The input is the message, recipents', args_schema=None, return_direct=False, verbose=False, callbacks=None, callback_manager=None, api_resource=<googleapiclient.discovery.Resource object at 0x10e5c6d10>),
GmailSearch(name='search_gmail', description=('Use this tool to search for email messages or threads. The input must be a valid Gmail query. The output is a JSON list of the requested resource.',), args_schema=<class 'langchain.tools.gmail.search.SearchArgsSchema'>, return_direct=False, verbose=False, callbacks=None, callback_manager=None, api_resource=<googleapiclient.discovery.Resource object at 0x10e5c6d10>),
GmailGetMessage(name='get_gmail_message', description='Use this tool to fetch an email by message ID. Returns the thread ID, snipet, body, subject, and sender.', args_schema=<class 'langchain.tools.gmail.get_message.SearchArgsSchema'>, return_direct=False, verbose=False, callbacks=None, callback_manager=None, api_resource=<googleapiclient.discovery.Resource object at 0x10e5c6d10>),
GmailGetThread(name='get_gmail_thread', description=('Use this tool to search for email messages. The input must be a valid Gmail query. The output is a JSON list of messages.',), args_schema=<class 'langchain.tools.gmail.get_thread.GetThreadSchema'>, return_direct=False, verbose=False, callbacks=None, callback_manager=None, api_resource=<googleapiclient.discovery.Resource object at 0x10e5c6d10>)]

在代理中使用 (Use within an Agent)

from langchain import OpenAI
from langchain.agents import initialize_agent, AgentType
llm = OpenAI(temperature=0)
agent = initialize_agent(
tools=toolkit.get_tools(),
llm=llm,
agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
)
agent.run(
"请为我创建一封草稿邮件,内容是一封来自一只有思维能力的鹦鹉的信,她希望与她失散已久的朋友,一只猫,合作进行一些研究。但是在任何情况下都不要发送这封邮件。"
)
    WARNING:root:Failed to load default session, using empty session: 0
WARNING:root:Failed to persist run: {"detail":"Not Found"}





'我已经为您创建了一封草稿邮件,您可以进行编辑。草稿的ID是r5681294731961864018。'
agent.run("请在我的草稿中搜索最新的邮件。")
    WARNING:root:Failed to load default session, using empty session: 0
WARNING:root:Failed to persist run: {"detail":"Not Found"}





"您的草稿中最新的邮件是来自hopefulparrot@gmail.com,主题是'合作机会'。邮件的正文内容如下:'亲爱的[朋友],我希望这封信能找到你。我写信给你是希望重新点燃我们的友谊,并讨论一起进行一些研究的可能性。我知道我们过去有过分歧,但我相信我们可以把它们放在一边,为了更大的利益而共同努力。期待您的回音。诚挚地,[鹦鹉]'"