Microsoft 365 是由本笔记本将指导您如何将 LangChain 连接到Microsoft拥有的生产力软件、协作和基于云的服务的产品系列。 注意:Office 365已更名为Microsoft 365。
Office365 电子邮件和日历。
要使用此工具包,您需要按照 Microsoft Graph 身份验证和授权概述 中的说明设置您的凭据。收到 CLIENT_ID 和 CLIENT_SECRET 后,您可以将它们作为环境变量输入到下方。
您也可以使用此处的身份验证说明。
pip install -qU O365
pip install -qU beautifulsoup4 # 这是可选的,但对于解析 HTML 消息很有用
pip install -qU langchain-community
分配环境变量
工具包将读取CLIENT_ID 和 CLIENT_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。"
通过 MCP 将这些文档连接到 Claude、VSCode 等 以获取实时答案。

