Microsoft Purview

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

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

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

Prerequisites

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

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

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

在您的代理的工作流程中介軟體流程中,您可以新增 Microsoft Purview 政策中介軟體,攔截提示與回應,以判斷是否符合 Microsoft Purview 設定的政策。 代理框架 SDK 能夠攔截代理對代理或終端使用者的聊天客戶端提示與回應。

以下程式碼範例示範如何將 Microsoft Purview 政策中介軟體加入您的代理程式碼中。 如果你是 Agent Framework 的新手,請參考 「用 Agent Framework 建立並執行代理」。


using Azure.AI.Projects;
using Azure.Core;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Purview;

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 AIProjectClient(
    new Uri(endpoint),
    new DefaultAzureCredential())
    .AsAIAgent(
        model: deploymentName,
        instructions: "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);

Warning

DefaultAzureCredential 開發方便,但在生產過程中需謹慎考量。 在生產環境中,建議使用特定的憑證(例如 ManagedIdentityCredential),以避免延遲問題、意外的憑證探測,以及備援機制帶來的安全風險。

import asyncio
import os
from agent_framework import Agent, Message
from agent_framework.openai import OpenAIChatCompletionClient
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_COMPLETION_MODEL", "<azureOpenAIChatDeploymentName>")

async def main():
    chat_client = OpenAIChatCompletionClient(
        model=os.environ["AZURE_OPENAI_CHAT_COMPLETION_MODEL"],
        azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
        api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
        credential=AzureCliCredential(),
    )
    purview_settings: PurviewSettings = {
        "app_name": "My Secure Agent",
    }
    purview_middleware = PurviewPolicyMiddleware(
        credential=InteractiveBrowserCredential(
            client_id="<clientId>",
        ),
        settings=purview_settings,
    )
    agent = Agent(
        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())

PurviewSettings 是 TypedDict,不是執行時設定類別。 傳送包含文件中說明的鍵的映射,並且一定要提供 Azure 憑證給中介軟體。

Python 內容評估與執行

中介軟體會評估每一個非空訊息內容項目,而不僅僅是純文字。 文本與推理文本被視為文本。 Base64 資料 URI 中的承載資料會被視為二進位內容。 非 base64 的資料、URI 內容、函式呼叫、函式結果及其他結構化內容會以文字序列化,避免跳過。 只有空的內容和 usage 內容會被省略。

當中介軟體無法解析使用者識別碼、租用戶或應用程式位置時,原則評估會以失敗關閉。 從經過驗證的伺服器端憑證而非呼叫者控制的訊息欄位來推導身份。 只有在您明確選擇可用性而非強制執行時,才設定 ignore_exceptions;此設定會對每個錯誤略過強制執行。

選擇 Python 中介軟體放置位置

PurviewPolicyMiddleware 和 PurviewChatPolicyMiddleware 在不同的管線邊界執行相同政策:

行為 代理中介軟體 聊天中介軟體
評估範圍 每次代理程式執行時,評估呼叫者的輸入與最終回應。 評估每次模型呼叫的預先準備請求與回應。
工具與情境資料 它不會在工具執行前評估上下文提供者的輸出或模型工具呼叫。 在執行前評估上下文提供者輸出、重播歷史及模型工具呼叫。 工具結果會在下一次模型呼叫時進行評估。
已封鎖的回應歷程 原始回應可能在中介軟體為呼叫端替換它之前就已經儲存好。 替代內容會在 Agent Framework 儲存回合之前提供。 提供者管理的儲存裝置可能仍保留原始回應。

當政策必須涵蓋完整模型請求時,使用 PurviewChatPolicyMiddleware 進行資料遺失防護。 當在代理執行邊界只需一次檢查就足夠時,請使用 PurviewPolicyMiddleware。 在任一個中介軟體清單中,將 Purview 中介軟體放在最後,這樣後續的中介軟體在 Purview 評估內容後就無法替換內容。

這兩種中介軟體類型都會完整緩衝串流回應,並在釋出任何更新前評估它。 因此,串流在連接 Purview 中介軟體時不會以增量方式傳送內容。


下一步

現在你已經將上述程式碼加入代理程式,請執行以下步驟來測試 Microsoft Purview 是否整合進你的程式碼中:

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

Resources

備註

Go 對此功能的支援即將推出。 最新狀態請參閱 Agent Framework Go 倉庫 。