本文档提供了一个集成 Azure Functions 和 Microsoft Graph API 以访问和搜索 SharePoint 及 Office 365 内容的全面指南。它侧重于实现 OAuth 身份验证、执行 Graph API 查询以及处理响应,从而使 SharePoint 内容可以通过 ChatGPT 动作进行访问。
有关 Azure OpenAI 服务集成的常规信息,请参阅 Azure OpenAI 服务示例。
Azure Functions 可以作为 ChatGPT 和 Microsoft Graph API 之间的中间件,使用户能够通过自然语言查询搜索和检索 SharePoint 和 Office 365 中的文档。此集成利用 Azure 的身份验证机制和无服务器计算能力来安全地访问企业内容。
涵盖两种主要的解决方案模式
来源:examples/chatgpt/sharepoint_azure_function/Using_Azure_Functions_and_Microsoft_Graph_to_Query_SharePoint.md5-59 examples/chatgpt/sharepoint_azure_function/Using_Azure_Functions_and_Microsoft_Graph_to_Query_SharePoint.md60-83
此解决方案通过文件检索模式将文件返回给 ChatGPT。
此解决方案在将文件内容返回给 ChatGPT 之前对其进行预处理。
身份验证流程使用 Azure 的 Easy Auth 和 On-Behalf-Of (OBO) 令牌交换来维护用户上下文。
来源:examples/chatgpt/sharepoint_azure_function/Using_Azure_Functions_and_Microsoft_Graph_to_Query_SharePoint.md84-108 examples/chatgpt/sharepoint_azure_function/solution_two_preprocessing.js9-46
实现需要
身份验证过程涉及初始化 Graph 客户端并获取 On-Behalf-Of 令牌
getOboToken 函数将用户令牌交换为 Graph API 令牌
来源:examples/chatgpt/sharepoint_azure_function/solution_two_preprocessing.js19-46
对于解决方案 1,getDriveItemContent 函数检索文件内容并将其转换为 base64。
对于解决方案 2,实现包括
来源:examples/chatgpt/sharepoint_azure_function/solution_two_preprocessing.js48-116
此解决方案将文件作为 base64 编码内容直接返回给 ChatGPT。
| 功能 | 目的 |
|---|---|
initGraphClient | 使用访问令牌初始化 Microsoft Graph 客户端 |
getOboToken | 交换用户令牌以获取 Graph API 令牌 |
getDriveItemContent | 检索文件内容并转换为 base64 |
| 主函数 | 协调搜索和文件检索过程 |
该函数将响应格式化为符合 ChatGPT 对文件响应的预期结构。
此解决方案在返回给 ChatGPT 之前提取和摘要文本内容。
| 功能 | 目的 |
|---|---|
initGraphClient | 使用访问令牌初始化 Microsoft Graph 客户端 |
getOboToken | 交换用户令牌以获取 Graph API 令牌 |
getDriveItemContent | 检索并提取文件中的文本 |
getRelevantParts | 使用 GPT-4o-mini 提取相关内容 |
| 主函数 | 协调搜索和内容处理 |
来源:examples/chatgpt/sharepoint_azure_function/solution_two_preprocessing.js118-238
该解决方案支持多种文件类型
| 文件类型 | 处理方法 |
|---|---|
| 直接文本提取 | |
| Office 文档 | 转换为 PDF 再提取文本 |
| 文本文件 | 直接内容检索 |
| CSV 文件 | 直接内容检索 |
| 不支持的类型 | 返回“不支持的文件类型”消息 |
来源:examples/chatgpt/sharepoint_azure_function/solution_two_preprocessing.js48-89
要与 ChatGPT 集成,需要 OpenAPI 规范
对于解决方案 1(文件检索)
对于解决方案 2(文本预处理)
GPT 的自定义指令应包括
示例指令模式
You are a Q&A helper that helps answer users questions. You have access to a documents repository through your API action. When a user asks a question, you pass in the "searchTerm" a single keyword or term you think you should use for the search.
If your action returns results, then you take the results from the action and try to answer the users question.
If the response you get from the action is "No results found", let the user know and try a different search term, explaining why.
实施方式可以进行多种定制
| 定制 | 描述 |
|---|---|
| 搜索范围 | 将搜索范围限制在特定的 SharePoint 站点或 O365 驱动器 |
| 文件类型筛选 | 限制为特定文件类型 |
| 结果限制 | 调整处理的搜索结果数量 |
| GPT 模型 | 使用不同的模型进行文本摘要 |
| 提示工程 | 为文本提取自定义提示 |
来源:examples/chatgpt/sharepoint_azure_function/Using_Azure_Functions_and_Microsoft_Graph_to_Query_SharePoint.md471-485 examples/chatgpt/sharepoint_azure_function/Using_Azure_Functions_and_Microsoft_Graph_to_Query_SharePoint.md512-533
在实施此集成时,请考虑
来源:examples/chatgpt/sharepoint_azure_function/Using_Azure_Functions_and_Microsoft_Graph_to_Query_SharePoint.md484-488 examples/chatgpt/gpt_actions_library/gpt_action_sharepoint_text.ipynb527-533
Azure Functions 为连接 ChatGPT 到 Microsoft Graph API 和 SharePoint 内容提供了一个强大的中间件层。通过实施文件检索或文本预处理解决方案中的任何一个,组织可以在保持通过适当身份验证进行安全访问的同时,实现对其文档存储库的自然语言访问。
解决方案之间的选择取决于具体要求
两种解决方案都利用 Azure 的无服务器功能和 Microsoft 的身份验证机制,提供安全、可扩展的企业内容访问。