共用方式為


在 Azure OpenAI 中使用封鎖清單

可設定的內容篩選條件足以滿足大部分的 con 帳篷模式 ration 需求。 不過,您可能需要篩選使用案例特定的字詞。

必要條件

  • Azure 訂用帳戶。 免費建立一個
  • 擁有 Azure 訂用帳戶之後,請在 Azure 入口網站 中建立 Azure OpenAI 資源,以取得您的令牌、密鑰和端點。 輸入資源的唯一名稱、選取您在應用程式表單上輸入的訂用帳戶、選取資源群組、支援的區域以及支援的定價層。 然後選取建立
    • 部署資源需要幾分鐘的時間。 完成之後,請選取 [移至資源]。 在左側窗格中,於 [資源管理] 下選取 [訂用帳戶金鑰和端點]。 端點和其中一個金鑰是用以呼叫 API。
  • Azure CLI 已安裝
  • 已安裝 cURL

使用封鎖清單

您可以使用 Azure OpenAI API 建立封鎖清單。 下列步驟可協助您開始使用。

取得您的令牌

首先,您需要取得令牌,以存取用來建立、編輯和刪除封鎖清單的 API。 您可以使用下列 Azure CLI 命令來取得此權杖:

az account get-access-token 

建立或修改封鎖清單

將下面的 cURL 命令複製到文字編輯器,並進行下列變更:

  1. 將 {subscriptionId} 取代為您的訂用帳戶標識符。
  2. 將 {resourceGroupName} 取代為您的資源組名。
  3. 以您的資源名稱取代 {accountName}。
  4. 以清單的自定義名稱取代 {raiBlocklistName} (在 URL 中)。 允許的字元: 0-9, A-Z, a-z, - . _ ~
  5. 將 {token} 取代為您從上述「取得令牌」步驟取得的令牌。
  6. 選擇性地將 「description」 欄位的值取代為自訂描述。
curl --location --request PUT 'https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.CognitiveServices/accounts/{accountName}/raiBlocklists/{raiBlocklistName}?api-version=2024-03-01-preview' \ 
--header 'Authorization: Bearer {token}' \ 
--header 'Content-Type: application/json' \ 
--data-raw '{ 
    "properties": { 
        "description": "This is a prompt blocklist"  
    } 
}' 

回應碼應 201 為 (建立新的清單) 或 200 (已更新現有的清單)。

將封鎖清單套用至內容篩選

如果您尚未建立內容篩選,您可以在左側的 [內容篩選] 索引標籤的 Studio 中執行此動作。 若要使用封鎖清單,請確定此內容篩選器已套用至 Azure OpenAI 部署。 您可以在左側的 [部署] 索引標籤中執行此動作。

若要將 完成 封鎖清單套用至內容篩選,請使用下列 cURL 命令:

  1. 以您的子標識碼取代 {subscriptionId}。
  2. 將 {resourceGroupName} 取代為您的資源組名。
  3. 以您的資源名稱取代 {accountName}。
  4. 以內容篩選的名稱取代 {raiPolicyName}
  5. 將 {token} 取代為您從上述「取得令牌」步驟取得的令牌。
  6. 以清單的自定義名稱取代本文中的 「raiBlocklistName」。 允許的字元: 0-9, A-Z, a-z, - . _ ~
curl --location --request PUT 'https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.CognitiveServices/accounts/{accountName}/raiPolicies/{raiPolicyName}?api-version=2024-03-01-preview' \ 
--header 'Authorization: Bearer {token}' \ 
--header 'Content-Type: application/json' \ 
--data-raw '{ 
    "properties": { 
        "basePolicyName": "Microsoft.Default", 
        "completionBlocklists": [{ 
            "blocklistName": "raiBlocklistName", 
            "blocking": true 
        }], 
        "contentFilters": [ ] 
    } 
}' 

將 blockItems 新增至清單

注意

一個清單中允許的字詞上限為10,000個。

將下面的 cURL 命令複製到文字編輯器,並進行下列變更:

  1. 以您的子標識碼取代 {subscriptionId}。
  2. 將 {resourceGroupName} 取代為您的資源組名。
  3. 以您的資源名稱取代 {accountName}。
  4. 以清單的自定義名稱取代 {raiBlocklistName} (在 URL 中)。 允許的字元: 0-9, A-Z, a-z, - . _ ~
  5. 以列表專案的自定義名稱取代 {raiBlocklistItemName}。
  6. 將 {token} 取代為您從上述「取得令牌」步驟取得的令牌。
  7. 將欄位的值 "blocking pattern" 取代為您想要新增至封鎖清單的專案。 blockItem 的最大長度為 1000 個字元。 也指定模式是否為 regex 或完全相符。
curl --location --request PUT 'https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.CognitiveServices/accounts/{accountName}/raiBlocklists/{raiBlocklistName}/raiBlocklistItems/{raiBlocklistItemName}?api-version=2024-03-01-preview' \ 
--header 'Authorization: Bearer {token}' \ 
--header 'Content-Type: application/json' \ 
--data-raw '{  
    "properties": {  
        "pattern": "blocking pattern",  
        "isRegex": false  
    }  
}' 

注意

將新字詞新增至封鎖清單可能需要大約5分鐘的時間。 請在 5 分鐘後進行測試。

回應碼應該是 200

{ 
  "name": "raiBlocklistItemName", 
  "id": "/subscriptions/subscriptionId/resourceGroups/resourceGroupName/providers/Microsoft.CognitiveServices/accounts/accountName/raiBlocklists/raiBlocklistName/raiBlocklistItems/raiBlocklistItemName", 
  "properties": { 
    "pattern": "blocking pattern", 
    "isRegex": false 
  } 
} 

使用封鎖清單分析文字

現在,您可以測試具有封鎖清單的部署。 若要這麼做,最簡單的方式是在 Azure OpenAI Studio 中。 如果內容在提示或完成時遭到封鎖,您應該會看到一則錯誤訊息,指出觸發內容篩選系統。

如需呼叫 Azure OpenAI 端點的指示,請流覽 快速入門

在下列範例中,具有封鎖清單的 GPT-35-Turbo 部署封鎖提示。 回應會傳 400 回錯誤。

{ 
    "error": { 
        "message": "The response was filtered due to the prompt triggering Azure OpenAI’s content management policy. Please modify your prompt and retry. To learn more about our content filtering policies please read our documentation: https://go.microsoft.com/fwlink/?linkid=2198766", 
        "type": null, 
        "param": "prompt", 
        "code": "content_filter", 
        "status": 400, 
        "innererror": { 
            "code": "ResponsibleAIPolicyViolation", 
            "content_filter_result": { 
                "custom_blocklists": [ 
                    { 
                        "filtered": true, 
                        "id": "raiBlocklistName" 
                    } 
                ], 
                "hate": { 
                    "filtered": false, 
                    "severity": "safe" 
                }, 
                "self_harm": { 
                    "filtered": false, 
                    "severity": "safe" 
                }, 
                "sexual": { 
                    "filtered": false, 
                    "severity": "safe" 
                }, 
                "violence": { 
                    "filtered": false, 
                    "severity": "safe" 
                } 
            } 
        } 
    } 
} 

如果完成本身遭到封鎖,回應會傳回 200,因為完成只會在符合封鎖清單內容時關閉。 批注顯示已比對封鎖清單。

{ 
    "id": "chatcmpl-85NkyY0AkeBMunOjyxivQSiTaxGAl", 
    "object": "chat.completion", 
    "created": 1696293652, 
    "model": "gpt-35-turbo", 
    "prompt_filter_results": [ 
        { 
            "prompt_index": 0, 
            "content_filter_results": { 
                "hate": { 
                    "filtered": false, 
                    "severity": "safe" 
                }, 
                "self_harm": { 
                    "filtered": false, 
                    "severity": "safe" 
                }, 
                "sexual": { 
                    "filtered": false, 
                    "severity": "safe" 
                }, 
                "violence": { 
                    "filtered": false, 
                    "severity": "safe" 
                } 
            } 
        } 
    ], 
    "choices": [ 
        { 
            "index": 0, 
            "finish_reason": "content_filter", 
            "message": { 
                "role": "assistant" 
            }, 
            "content_filter_results": { 
                "custom_blocklists": [ 
                    { 
                        "filtered": true, 
                        "id": "myBlocklistName" 
                    } 
                ], 
                "hate": { 
                    "filtered": false, 
                    "severity": "safe" 
                }, 
                "self_harm": { 
                    "filtered": false, 
                    "severity": "safe" 
                }, 
                "sexual": { 
                    "filtered": false, 
                    "severity": "safe" 
                }, 
                "violence": { 
                    "filtered": false, 
                    "severity": "safe" 
                } 
            } 
        } 
    ], 
    "usage": { 
        "completion_tokens": 75, 
        "prompt_tokens": 27, 
        "total_tokens": 102 
    } 
} 

在 Azure OpenAI Studio 中使用封鎖清單

您也可以在 Azure OpenAI Studio 中建立自定義封鎖清單,作為內容篩選組態的一部分(公開預覽)。 如需如何建立自定義內容篩選的指示, 請參閱這裡。 下列步驟示範如何透過 Azure OpenAI Studio 建立自定義封鎖清單作為內容篩選器的一部分。

  1. 選取 [內容篩選] 索引標籤旁的 [封鎖清單] 索引標籤。 screenshot of blocklist selection.
  2. 選取 [建立封鎖清單] Screenshot of blocklist create selection.
  3. 建立封鎖清單的名稱、新增描述,然後選取 [建立]。 Screenshot of blocklist naming.
  4. 建立自定義封鎖清單后,請選取自定義封鎖清單,然後選取 [新增字詞]。 Screenshot of custom blocklist add term.
  5. 新增應該篩選的字詞,然後選取 [建立]。 您也可以建立 regex。 Screenshot of custom blocklist add item.
  6. 您可以在封鎖清單中編輯和刪除每個字詞。 Screenshot of custom blocklist edit.
  7. 一旦封鎖清單準備就緒,請流覽至 [內容篩選條件][預覽] 區段,然後建立新的自定義內容篩選組態。 這會開啟具有數個 AI 內容安全性元件的精靈。 您可以在這裡找到有關如何設定主要篩選和選擇性模型的詳細資訊。 移至 [新增封鎖清單] (選擇性)。
  8. 您現在會看到所有可用的封鎖清單。 封鎖清單有兩種類型 – 您所建立的封鎖清單,以及 Microsoft 提供的預先建置封鎖清單,在此案例中為不雅內容封鎖清單 (英文)
  9. 您現在可以決定要在內容篩選組態中包含哪些可用的封鎖清單,而且您可以選取它是否應該套用至 和 篩選提示、完成或兩者。 在下列範例中,我們會套用剛才建立的 CustomBlocklist1 來提示和完成,而不雅內容封鎖清單只會完成。 最後一個步驟是按兩下一步來檢閱並完成內容篩選設定。 Screenshot of filtering configuration management.
  10. 您一律可以返回並編輯您的設定。 準備好之後,請選取 [建立內容篩選]。 包含封鎖清單的新組態現在可以套用至部署。 如需詳細指示,請參閱 這裡

下一步