共用方式為


使用 Microsoft Purview SDK 搭配 Agent Framework

Microsoft Purview 為 AI 應用提供企業級的資料安全、合規與治理能力。 透過將 Purview API 整合進 Agent Framework SDK,開發者能打造設計上安全的智慧代理,同時確保提示與回應中的敏感資料受到保護並符合組織政策。

為什麼要將 Purview 與 Agent Framework 整合?

  • 防止敏感資料外洩:根據資料遺失防護(DLP)政策,進行敏感內容的線上封鎖。
  • 啟用治理:在 Purview 中記錄 AI 互動,用於審計、通訊合規、內部風險管理、電子發現及資料生命週期管理。
  • 加速採用:企業客戶要求 AI 應用程式符合法規要求。 Purview 整合功能解除部署阻擋。

先決條件

在開始之前,請確保您擁有:

  • Microsoft Azure 訂閱中已配置 Microsoft Purview。
  • Microsoft 365 訂閱,搭配 E5 授權和即用即付計費設定。
  • Agent Framework SDK:要安裝 Agent Framework SDK:
    • Python:執行 pip install agent-framework --pre
    • .NET:從 NuGet 安裝。

如何將 Microsoft Purview 整合進您的代理程式中

在您的代理的工作流程中介軟體流程中,您可以新增 Microsoft Purview 政策中介軟體,攔截提示與回應,以判斷是否符合 Microsoft Purview 設定的政策。 代理框架 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 是否整合進你的程式碼中:

  1. Entra 註冊:註冊你的代理程式,並在服務主體中新增所需的 Microsoft Graph 權限(ProtectionScopes.Compute.AllContentActivity.WriteContent.Process.All)。 欲了解更多資訊,請參閱 「以 Microsoft Entra ID 註冊應用程式 」及「 dataSecurityAndGovernance」資源類型。 下一步你需要 Microsoft Entra 應用程式 ID。
  2. Purview 政策:使用 Microsoft Entra 應用程式 ID 設定 Purview 政策,使客服人員通訊資料能流入 Purview。 更多資訊請參閱 「配置 Microsoft Purview」。

資源