Skip to main content
Microsoft 365 是由 Microsoft 拥有的生产力软件、协作和基于云的服务的产品系列。 注意:Office 365 已更名为 Microsoft 365
本笔记本将引导你将 LangChain 连接到 Office365 电子邮件和日历。 要使用此工具包,你需要按照 Microsoft Graph 身份验证和授权概述 中的说明设置你的凭据。一旦你获得了 CLIENT_ID 和 CLIENT_SECRET,你可以在下面将它们作为环境变量输入。 你也可以使用此处的身份验证说明
pip install -qU  O365
pip install -qU  beautifulsoup4  # 这是可选的,但对于解析 HTML 消息很有用
pip install -qU langchain-community

分配环境变量

工具包将读取 CLIENT_IDCLIENT_SECRET 环境变量来对用户进行身份验证,因此你需要在此处设置它们。你还需要设置你的 OPENAI_API_KEY,以便稍后使用代理。
# 在此处设置环境变量

创建工具包并获取工具

首先,你需要创建工具包,以便稍后访问其工具。
from langchain_community.agent_toolkits import O365Toolkit

toolkit = O365Toolkit()
tools = toolkit.get_tools()
tools
[O365SearchEvents(name='events_search', description=" 使用此工具搜索用户的日历事件。输入必须是搜索查询的开始和结束日期时间。输出是用户日历中开始和结束时间之间所有事件的 JSON 列表。你可以假设用户无法在现有会议之上安排任何会议,并且用户在会议期间很忙。任何没有事件的时间对用户来说都是空闲的。 ", args_schema=<class 'langchain_community.tools.office365.events_search.SearchEventsInput'>, return_direct=False, verbose=False, callbacks=None, callback_manager=None, handle_tool_error=False, account=Account Client Id: f32a022c-3c4c-4d10-a9d8-f6a9a9055302),
 O365CreateDraftMessage(name='create_email_draft', description='使用此工具使用提供的消息字段创建草稿电子邮件。', args_schema=<class 'langchain_community.tools.office365.create_draft_message.CreateDraftMessageSchema'>, return_direct=False, verbose=False, callbacks=None, callback_manager=None, handle_tool_error=False, account=Account Client Id: f32a022c-3c4c-4d10-a9d8-f6a9a9055302),
 O365SearchEmails(name='messages_search', description='使用此工具搜索电子邮件消息。输入必须是有效的 Microsoft Graph v1.0 $search 查询。输出是请求资源的 JSON 列表。', args_schema=<class 'langchain_community.tools.office365.messages_search.SearchEmailsInput'>, return_direct=False, verbose=False, callbacks=None, callback_manager=None, handle_tool_error=False, account=Account Client Id: f32a022c-3c4c-4d10-a9d8-f6a9a9055302),
 O365SendEvent(name='send_event', description='使用此工具使用提供的事件字段创建并发送事件。', args_schema=<class 'langchain_community.tools.office365.send_event.SendEventSchema'>, return_direct=False, verbose=False, callbacks=None, callback_manager=None, handle_tool_error=False, account=Account Client Id: f32a022c-3c4c-4d10-a9d8-f6a9a9055302),
 O365SendMessage(name='send_email', description='使用此工具使用提供的消息字段发送电子邮件。', args_schema=<class 'langchain_community.tools.office365.send_message.SendMessageSchema'>, return_direct=False, verbose=False, callbacks=None, callback_manager=None, handle_tool_error=False, account=Account Client Id: f32a022c-3c4c-4d10-a9d8-f6a9a9055302)]

在代理中使用

from langchain.agents import create_agent
from langchain_openai import OpenAI
llm = OpenAI(temperature=0)
agent = create_agent(
    model=llm,
    tools=toolkit.get_tools(),
)
agent.invoke(
    {"input": "为我创建一封电子邮件草稿,以便我编辑一封来自一只希望与她疏远的朋友(一只猫)合作进行一些研究的、有感知能力的鹦鹉的信。在任何情况下都不得发送该消息。"}
)
'草稿电子邮件已正确创建。'
agent.invoke(
    {"input": "你能在我的草稿文件夹中搜索一下,并告诉我其中是否有任何关于合作的吗?"}
)
"我在你的草稿文件夹中找到了一份关于合作的草稿。它发送于 2023-06-16T18:22:17+0000,主题是 '合作请求'。"
agent.invoke(
    {"input": "你能在 2023 年 10 月 3 日美国东部时间下午 2 点安排一次与一只鹦鹉的 30 分钟会议,讨论研究合作吗?"}
)
/home/vscode/langchain-py-env/lib/python3.11/site-packages/O365/utils/windows_tz.py:639: PytzUsageWarning: The zone attribute is specific to pytz's interface; please migrate to a new time zone provider. For more details on how to do so, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
  iana_tz.zone if isinstance(iana_tz, tzinfo) else iana_tz)
/home/vscode/langchain-py-env/lib/python3.11/site-packages/O365/utils/utils.py:463: PytzUsageWarning: The zone attribute is specific to pytz's interface; please migrate to a new time zone provider. For more details on how to do so, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
  timezone = date_time.tzinfo.zone if date_time.tzinfo is not None else None
'我已安排在 2023 年 10 月 3 日美国东部时间下午 2 点与一只鹦鹉举行会议,讨论研究合作。如果你需要进行任何更改,请告诉我。'
agent.invoke(
    {"input": "你能告诉我我在 2023 年 10 月 3 日美国东部时间是否有任何事件吗?如果有,告诉我其中是否有任何是与一只鹦鹉的?"}
)
"是的,你在 2023 年 10 月 3 日有一个与一只鹦鹉的事件。该事件标题为 '与鹦鹉的会议',计划从下午 6:00 到 6:30。"