你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

如何为 Azure AI 服务中的模型配置内容筛选器(预览版)

重要

本文中标记了“(预览版)”的项目目前为公共预览版。 此预览版未提供服务级别协议,不建议将其用于生产工作负载。 某些功能可能不受支持或者受限。 有关详细信息,请参阅 Microsoft Azure 预览版补充使用条款

集成到 Azure AI 服务的内容筛选系统与核心模型一起运行。 它使用多类分类模型组合分别检测四种严重程度(安全、低、中和高)的四类有害内容(暴力、仇恨、性和自残)。 它提供可选的二进制分类器,用于检测公共存储库中的越狱风险、现有文本和代码。 在以下文章中了解有关内容类别、严重性级别和内容筛选系统行为的详细信息

默认内容筛选配置设置为在提示和完成的所有四个内容危害类别的中等严重性阈值下进行筛选。 因此,严重级别检测为中或高的内容将被筛选,而严重性级别检测为低或安全的内容不会被筛选。

可以在资源级别配置内容筛选器,并与一个或多个部署相关联。

先决条件

若要完成本文,需要做好以下准备:

创建自定义内容筛选器

按照以下步骤创建自定义内容筛选器:

  1. 转到 Azure AI Foundry 门户

  2. 选择“安全 + 安保”。

  3. 选择选项卡“内容筛选器”,然后选择“创建内容筛选器”。

  4. 在“基本信息”下,为内容筛选器提供名称。

  5. 在“连接”下,选择连接到你的项目的“Azure AI 服务”资源。

  6. 在“输入筛选器”下,根据要求配置筛选器。 此配置会在请求到达模型本身之前应用。

  7. 在“输出筛选器”下,根据要求配置筛选器。 模型执行并且内容生成后,将应用此配置。

  8. 选择下一步

  9. (可选)可以将给定部署与创建的内容筛选器相关联。 可以随时更改关联的模型部署。

  10. 部署完成后,新的内容筛选器将应用于模型部署。

在代码中考虑内容筛选

将内容筛选应用到模型部署后,服务可以根据输入和输出截获请求。 触发内容筛选器时,将返回 400 错误代码,其中包含触发的规则的说明。

使用包管理器(例如 pip)安装包 azure-ai-inference

pip install azure-ai-inference>=1.0.0b5

警告

Azure AI 服务资源需要 Python 版本 azure-ai-inference>=1.0.0b5

然后,可以使用包来使用模型。 以下示例演示如何创建客户端来使用聊天补全:

import os
from azure.ai.inference import ChatCompletionsClient
from azure.core.credentials import AzureKeyCredential

model = ChatCompletionsClient(
    endpoint="https://<resource>.services.ai.azure.com/models",
    credential=AzureKeyCredential(os.environ["AZUREAI_ENDPOINT_KEY"]),
)

浏览我们的示例,并阅读 API 参考文档以开始使用。

以下示例显示了已触发内容安全的聊天完成请求的响应。

from azure.ai.inference.models import AssistantMessage, UserMessage, SystemMessage
from azure.core.exceptions import HttpResponseError

try:
    response = model.complete(
        messages=[
            SystemMessage(content="You are an AI assistant that helps people find information."),
            UserMessage(content="Chopping tomatoes and cutting them into cubes or wedges are great ways to practice your knife skills."),
        ]
    )

    print(response.choices[0].message.content)

except HttpResponseError as ex:
    if ex.status_code == 400:
        response = json.loads(ex.response._content.decode('utf-8'))
        if isinstance(response, dict) and "error" in response:
            print(f"Your request triggered an {response['error']['code']} error:\n\t {response['error']['message']}")
        else:
            raise ex
    else:
        raise ex

遵循最佳做法

建议通过迭代标记(例如,红队测试、压力测试和分析)和测量过程来告知内容筛选配置决策,以解决与特定模型、应用和部署案例相关的潜在危害。 在实施内容筛选等缓解措施后,重复测量以测试有效性。

基于 Microsoft 负责任 AI 标准的适用于 Azure OpenAI 的负责任 AI 的建议和最佳做法可以在 Azure OpenAI 的负责任 AI 概述中找到

重要

本文中标记了“(预览版)”的项目目前为公共预览版。 此预览版未提供服务级别协议,不建议将其用于生产工作负载。 某些功能可能不受支持或者受限。 有关详细信息,请参阅 Microsoft Azure 预览版补充使用条款

集成到 Azure AI 服务的内容筛选系统与核心模型一起运行。 它使用多类分类模型组合分别检测四种严重程度(安全、低、中和高)的四类有害内容(暴力、仇恨、性和自残)。 它提供可选的二进制分类器,用于检测公共存储库中的越狱风险、现有文本和代码。 在以下文章中了解有关内容类别、严重性级别和内容筛选系统行为的详细信息

默认内容筛选配置设置为在提示和完成的所有四个内容危害类别的中等严重性阈值下进行筛选。 因此,严重级别检测为中或高的内容将被筛选,而严重性级别检测为低或安全的内容不会被筛选。

可以在资源级别配置内容筛选器,并与一个或多个部署相关联。

先决条件

若要完成本文,需要做好以下准备:

添加带有自定义内容筛选的模型部署

建议使用 Azure AI Foundry 门户或使用 Bicep 在代码中创建内容筛选器。 不支持使用 Azure CLI 创建自定义内容筛选器或将其应用到部署。

在代码中考虑内容筛选

将内容筛选应用到模型部署后,服务可以根据输入和输出截获请求。 触发内容筛选器时,将返回 400 错误代码,其中包含触发的规则的说明。

使用包管理器(例如 pip)安装包 azure-ai-inference

pip install azure-ai-inference>=1.0.0b5

警告

Azure AI 服务资源需要 Python 版本 azure-ai-inference>=1.0.0b5

然后,可以使用包来使用模型。 以下示例演示如何创建客户端来使用聊天补全:

import os
from azure.ai.inference import ChatCompletionsClient
from azure.core.credentials import AzureKeyCredential

model = ChatCompletionsClient(
    endpoint="https://<resource>.services.ai.azure.com/models",
    credential=AzureKeyCredential(os.environ["AZUREAI_ENDPOINT_KEY"]),
)

浏览我们的示例,并阅读 API 参考文档以开始使用。

以下示例显示了已触发内容安全的聊天完成请求的响应。

from azure.ai.inference.models import AssistantMessage, UserMessage, SystemMessage
from azure.core.exceptions import HttpResponseError

try:
    response = model.complete(
        messages=[
            SystemMessage(content="You are an AI assistant that helps people find information."),
            UserMessage(content="Chopping tomatoes and cutting them into cubes or wedges are great ways to practice your knife skills."),
        ]
    )

    print(response.choices[0].message.content)

except HttpResponseError as ex:
    if ex.status_code == 400:
        response = json.loads(ex.response._content.decode('utf-8'))
        if isinstance(response, dict) and "error" in response:
            print(f"Your request triggered an {response['error']['code']} error:\n\t {response['error']['message']}")
        else:
            raise ex
    else:
        raise ex

遵循最佳做法

建议通过迭代标记(例如,红队测试、压力测试和分析)和测量过程来告知内容筛选配置决策,以解决与特定模型、应用和部署案例相关的潜在危害。 在实施内容筛选等缓解措施后,重复测量以测试有效性。

基于 Microsoft 负责任 AI 标准的适用于 Azure OpenAI 的负责任 AI 的建议和最佳做法可以在 Azure OpenAI 的负责任 AI 概述中找到

重要

本文中标记了“(预览版)”的项目目前为公共预览版。 此预览版未提供服务级别协议,不建议将其用于生产工作负载。 某些功能可能不受支持或者受限。 有关详细信息,请参阅 Microsoft Azure 预览版补充使用条款

集成到 Azure AI 服务的内容筛选系统与核心模型一起运行。 它使用多类分类模型组合分别检测四种严重程度(安全、低、中和高)的四类有害内容(暴力、仇恨、性和自残)。 它提供可选的二进制分类器,用于检测公共存储库中的越狱风险、现有文本和代码。 在以下文章中了解有关内容类别、严重性级别和内容筛选系统行为的详细信息

默认内容筛选配置设置为在提示和完成的所有四个内容危害类别的中等严重性阈值下进行筛选。 因此,严重级别检测为中或高的内容将被筛选,而严重性级别检测为低或安全的内容不会被筛选。

可以在资源级别配置内容筛选器,并与一个或多个部署相关联。

先决条件

若要完成本文,需要做好以下准备:

  • 安装 Azure CLI

  • 标识以下信息:

    • Azure 订阅 ID。

    • 你的 Azure AI 服务资源名称。

    • 在其中部署 Azure AI 服务资源的资源组。

    • 要部署的模型名称、提供商、版本和 SKU。 可以使用 Azure AI Foundry 门户或 Azure CLI 来标识它。 在此示例中,我们将部署以下模型:

      • 模型名称Phi-3.5-vision-instruct
      • 提供商Microsoft
      • 版本:2
      • 部署类型:全局标准

添加带有自定义内容筛选的模型部署

  1. 使用模板 ai-services-content-filter-template.bicep 描述内容筛选器策略:

    ai-services-content-filter-template.bicep

    @description('Name of the Azure AI Services account where the policy will be created')
    param accountName string
    
    @description('Name of the policy to be created')
    param policyName string
    
    @allowed(['Asynchronous_filter', 'Blocking', 'Default', 'Deferred'])
    param mode string = 'Default'
    
    @description('Base policy to be used for the new policy')
    param basePolicyName string = 'Microsoft.DefaultV2'
    
    param contentFilters array = [
      {
          name: 'Violence'
          severityThreshold: 'Medium'
          blocking: true
          enabled: true
          source: 'Prompt'
      }
      {
          name: 'Hate'
          severityThreshold: 'Medium'
          blocking: true
          enabled: true
          source: 'Prompt'
      }
      {
          name: 'Sexual'
          severityThreshold: 'Medium'
          blocking: true
          enabled: true
          source: 'Prompt'
      }
      {
          name: 'Selfharm'
          severityThreshold: 'Medium'
          blocking: true
          enabled: true
          source: 'Prompt'
      }
      {
          name: 'Jailbreak'
          blocking: true
          enabled: true
          source: 'Prompt'
      }
      {
          name: 'Indirect Attack'
          blocking: true
          enabled: true
          source: 'Prompt'
      }
      {
          name: 'Profanity'
          blocking: true
          enabled: true
          source: 'Prompt'
      }
      {
          name: 'Violence'
          severityThreshold: 'Medium'
          blocking: true
          enabled: true
          source: 'Completion'
      }
      {
          name: 'Hate'
          severityThreshold: 'Medium'
          blocking: true
          enabled: true
          source: 'Completion'
      }
      {
          name: 'Sexual'
          severityThreshold: 'Medium'
          blocking: true
          enabled: true
          source: 'Completion'
      }
      {
          name: 'Selfharm'
          severityThreshold: 'Medium'
          blocking: true
          enabled: true
          source: 'Completion'
      }
      {
          name: 'Protected Material Text'
          blocking: true
          enabled: true
          source: 'Completion'
      }
      {
          name: 'Protected Material Code'
          blocking: false
          enabled: true
          source: 'Completion'
      }
      {
          name: 'Profanity'
          blocking: true
          enabled: true
          source: 'Completion'
      }
    ]
    
    resource raiPolicy 'Microsoft.CognitiveServices/accounts/raiPolicies@2024-06-01-preview' = {
        name: '${accountName}/${policyName}'
        properties: {
            mode: mode
            basePolicyName: basePolicyName
            contentFilters: contentFilters
        }
    }
    
  2. 使用模板 ai-services-deployment-template.bicep 描述模型部署:

    ai-services-deployment-template.bicep

    @description('Name of the Azure AI services account')
    param accountName string
    
    @description('Name of the model to deploy')
    param modelName string
    
    @description('Version of the model to deploy')
    param modelVersion string
    
    @allowed([
      'AI21 Labs'
      'Cohere'
      'Core42'
      'DeepSeek'
      'Meta'
      'Microsoft'
      'Mistral AI'
      'OpenAI'
    ])
    @description('Model provider')
    param modelPublisherFormat string
    
    @allowed([
        'GlobalStandard'
        'Standard'
        'GlobalProvisioned'
        'Provisioned'
    ])
    @description('Model deployment SKU name')
    param skuName string = 'GlobalStandard'
    
    @description('Content filter policy name')
    param contentFilterPolicyName string = 'Microsoft.DefaultV2'
    
    @description('Model deployment capacity')
    param capacity int = 1
    
    resource modelDeployment 'Microsoft.CognitiveServices/accounts/deployments@2024-04-01-preview' = {
      name: '${accountName}/${modelName}'
      sku: {
        name: skuName
        capacity: capacity
      }
      properties: {
        model: {
          format: modelPublisherFormat
          name: modelName
          version: modelVersion
        }
        raiPolicyName: contentFilterPolicyName == null ? 'Microsoft.Nill' : contentFilterPolicyName
      }
    }
    
  3. 创建主部署定义:

    main.bicep

    param accountName string
    param modelName string
    param modelVersion string
    param modelPublisherFormat string
    param contentFilterPolicyName string
    
    module raiPolicy 'ai-services-content-filter-template.bicep' = {
      name: 'raiPolicy'
      scope: resourceGroup(resourceGroupName)
      params: {
        accountName: accountName
        policyName: contentFilterPolicyName
      }
    }
    
    module modelDeployment 'ai-services-deployment-template.bicep' = {
        name: 'modelDeployment'
        scope: resourceGroup(resourceGroupName)
        params: {
            accountName: accountName
            modelName: modelName
            modelVersion: modelVersion
            modelPublisherFormat: modelPublisherFormat
            contentFilterPolicyName: contentFilterPolicyName
        }
        dependsOn: [
            raiPolicy
        ]
    }
    
  4. 运行部署:

    RESOURCE_GROUP="<resource-group-name>"
    ACCOUNT_NAME="<azure-ai-model-inference-name>" 
    MODEL_NAME="Phi-3.5-vision-instruct"
    PROVIDER="Microsoft"
    VERSION=2
    RAI_POLICY_NAME="custom-policy"
    
    az deployment group create \
        --resource-group $RESOURCE_GROUP \
        --template-file main.bicep \
        --parameters accountName=$ACCOUNT_NAME raiPolicyName=$RAI_POLICY_NAME modelName=$MODEL_NAME modelVersion=$VERSION modelPublisherFormat=$PROVIDER
    

在代码中考虑内容筛选

将内容筛选应用到模型部署后,服务可以根据输入和输出截获请求。 触发内容筛选器时,将返回 400 错误代码,其中包含触发的规则的说明。

使用包管理器(例如 pip)安装包 azure-ai-inference

pip install azure-ai-inference>=1.0.0b5

警告

Azure AI 服务资源需要 Python 版本 azure-ai-inference>=1.0.0b5

然后,可以使用包来使用模型。 以下示例演示如何创建客户端来使用聊天补全:

import os
from azure.ai.inference import ChatCompletionsClient
from azure.core.credentials import AzureKeyCredential

model = ChatCompletionsClient(
    endpoint="https://<resource>.services.ai.azure.com/models",
    credential=AzureKeyCredential(os.environ["AZUREAI_ENDPOINT_KEY"]),
)

浏览我们的示例,并阅读 API 参考文档以开始使用。

以下示例显示了已触发内容安全的聊天完成请求的响应。

from azure.ai.inference.models import AssistantMessage, UserMessage, SystemMessage
from azure.core.exceptions import HttpResponseError

try:
    response = model.complete(
        messages=[
            SystemMessage(content="You are an AI assistant that helps people find information."),
            UserMessage(content="Chopping tomatoes and cutting them into cubes or wedges are great ways to practice your knife skills."),
        ]
    )

    print(response.choices[0].message.content)

except HttpResponseError as ex:
    if ex.status_code == 400:
        response = json.loads(ex.response._content.decode('utf-8'))
        if isinstance(response, dict) and "error" in response:
            print(f"Your request triggered an {response['error']['code']} error:\n\t {response['error']['message']}")
        else:
            raise ex
    else:
        raise ex

遵循最佳做法

建议通过迭代标记(例如,红队测试、压力测试和分析)和测量过程来告知内容筛选配置决策,以解决与特定模型、应用和部署案例相关的潜在危害。 在实施内容筛选等缓解措施后,重复测量以测试有效性。

基于 Microsoft 负责任 AI 标准的适用于 Azure OpenAI 的负责任 AI 的建议和最佳做法可以在 Azure OpenAI 的负责任 AI 概述中找到

后续步骤