Microsoft Purview 为 AI 应用程序提供企业级数据安全性、合规性和治理功能。 通过在 Agent Framework SDK 中集成 Purview API,开发人员可以构建设计安全的智能代理,同时确保提示和响应中的敏感数据受到保护并符合组织策略。
为什么将 Purview 与 Agent Framework 集成?
- 防止敏感数据泄漏:根据数据丢失防护(DLP)策略阻止敏感内容泄漏。
- 启用治理:在 Purview 中记录 AI 交互,用于审核、通信合规性、内部风险管理、电子数据展示和数据生命周期管理。
- 加速采用:企业客户需要 AI 应用的合规性。 Purview 集成促进了部署的进行。
先决条件
在开始之前,请确保具备:
- 配置了 Microsoft Purview 的 Microsoft Azure 订阅。
- 使用 E5 许可证和即用即付计费设置 Microsoft 365 订阅。
- 若要进行测试,可以使用 Microsoft 365 开发人员计划租户。 有关详细信息,请参阅 加入 Microsoft 365 开发人员计划。
- 代理框架 SDK:要安装代理框架 SDK,按照以下步骤进行:
- Python:运行
pip install agent-framework --pre。 - .NET:从 NuGet 安装。
- Python:运行
如何将 Microsoft Purview 集成到代理中
在代理的工作流中间件管道中,可以添加 Microsoft Purview 策略中间件来截获提示和响应,以确定它们是否满足在 Microsoft Purview 中设置的策略。 Agent Framework SDK 能够拦截代理到代理或终端用户聊天客户端的提示和响应。
下面的代码示例演示如何将 Microsoft Purview 策略中间件添加到代理代码。 如果不熟悉 Agent Framework,请参阅 使用 Agent Framework 创建并运行代理。
using Azure.AI.OpenAI;
using Azure.Core;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Purview;
using Microsoft.Extensions.AI;
using OpenAI;
string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
string deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
string purviewClientAppId = Environment.GetEnvironmentVariable("PURVIEW_CLIENT_APP_ID") ?? throw new InvalidOperationException("PURVIEW_CLIENT_APP_ID is not set.");
TokenCredential browserCredential = new InteractiveBrowserCredential(
new InteractiveBrowserCredentialOptions
{
ClientId = purviewClientAppId
});
AIAgent agent = new AzureOpenAIClient(
new Uri(endpoint),
new DefaultAzureCredential())
.GetChatClient(deploymentName)
.AsAIAgent("You are a secure assistant.")
.AsBuilder()
.WithPurview(browserCredential, new PurviewSettings("My Secure Agent"))
.Build();
AgentResponse response = await agent.RunAsync("Summarize zero trust in one sentence.").ConfigureAwait(false);
Console.WriteLine(response);
警告
DefaultAzureCredential 对于开发来说很方便,但在生产中需要仔细考虑。 在生产环境中,请考虑使用特定凭据(例如), ManagedIdentityCredential以避免延迟问题、意外凭据探测以及回退机制的潜在安全风险。
import asyncio
import os
from agent_framework import Agent, Message, Role
from agent_framework.azure import AzureOpenAIChatClient
from agent_framework.microsoft import PurviewPolicyMiddleware, PurviewSettings
from azure.identity import AzureCliCredential, InteractiveBrowserCredential
# Set default environment variables if not already set
os.environ.setdefault("AZURE_OPENAI_ENDPOINT", "<azureOpenAIEndpoint>")
os.environ.setdefault("AZURE_OPENAI_CHAT_DEPLOYMENT_NAME", "<azureOpenAIChatDeploymentName>")
async def main():
chat_client = AzureOpenAIChatClient(credential=AzureCliCredential())
purview_middleware = PurviewPolicyMiddleware(
credential=InteractiveBrowserCredential(
client_id="<clientId>",
),
settings=PurviewSettings(app_name="My Secure Agent")
)
agent = Agent(
chat_client=chat_client,
instructions="You are a secure assistant.",
middleware=[purview_middleware]
)
response = await agent.run(Message(role='user', contents=["Summarize zero trust in one sentence."]))
print(response)
if __name__ == "__main__":
asyncio.run(main())
后续步骤
将上述代码添加到代理后,请执行以下步骤来测试 Microsoft Purview 与代码的集成:
- Entra 注册:注册代理并将所需的 Microsoft Graph 权限(ProtectionScopes.Compute.All、ContentActivity.Write、Content.Process.All)添加到服务主体。 有关详细信息,请参阅 在 Microsoft Entra ID 和 dataSecurityAndGovernance 资源类型中注册应用程序。 在下一步中,需要Microsoft Entra 应用 ID。
- Purview 策略:使用 Microsoft Entra 应用 ID 配置 Purview 策略,使代理通信数据能够流入 Purview。 有关详细信息,请参阅 “配置 Microsoft Purview”。
资源
- Nuget: Microsoft.Agents.AI.Purview
- Github: Microsoft.Agents.AI.Purview
- 示例: AgentWithPurview