你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
集成到 Azure AI Foundry 的内容筛选系统与 Azure AI Foundry 模型一起运行。 它使用多类分类模型的合奏,在四个严重级别(安全、低、中、高)检测四类有害内容(暴力、仇恨、性与自我伤害)。 它提供可选的二进制分类器,用于检测公共存储库中的越狱风险、现有文本和代码。 有关内容类别、严重性级别和内容筛选系统行为的详细信息,请参阅 以下文章。
对于提示和完成, 默认内容筛选 配置会筛选所有四个有害类别的中等严重性阈值的内容。 在中等或高严重级别检测到的内容将被筛选掉,而检测到的低级别或安全严重性级别的内容不会被筛选。
可以在资源级别配置内容筛选器,并将其与一个或多个部署相关联。
先决条件
若要完成本文,需要做好以下准备:
一份 Azure 订阅。 如果你正在使用 GitHub 模型,则可以升级体验并在此过程中创建 Azure 订阅。 读取从 GitHub 模型升级到 Azure AI Foundry 的升级。
Azure AI Foundry 资源(以前称为 Azure AI 服务资源)。 有关详细信息,请参阅 创建 Azure AI Foundry 资源。
- 连接到你的 Azure AI 服务资源的 AI 项目。 可以按照 Azure AI Foundry 项目中的 Azure AI Foundry 模型服务配置 步骤。
创建自定义内容筛选器
按照以下步骤创建自定义内容筛选器:
从左窗格中选择 Guardrails 和控件 。
选择“ 内容筛选器 ”选项卡,然后选择“ 创建内容筛选器”。
在 “基本信息 ”页上,输入内容筛选器的名称。
对于 “连接”,请选择连接到项目的 Azure AI Services 资源的连接。
选择 “下一步 ”转到 “输入筛选器 ”页。
根据要求配置输入筛选器。 此配置会在请求到达模型本身之前应用。
选择 “下一步 ”转到“ 输出筛选器 ”页。
根据要求配置输出筛选器。 模型执行并且内容生成后,将应用此配置。
选择 “下一步 ”转到 “连接 ”页。
在此页上,可以选择将模型部署与创建的内容筛选器相关联。 可以随时更改关联的模型部署。
选择 “下一步 ”以查看筛选器设置。 然后选择“ 创建筛选器”。
部署完成后,新的内容筛选器将应用于模型部署。
在代码中考虑内容筛选
将内容筛选应用到模型部署时,服务可以根据输入和输出截获请求。 如果内容筛选器触发,服务将返回一个 400 错误代码,其中包含触发错误的规则的说明。
使用包管理器(例如 pip)安装包 azure-ai-inference:
pip install azure-ai-inference
然后,可以使用该包来调用模型。 以下示例演示如何创建客户端程序来调用聊天结果:
import os
from azure.ai.inference import ChatCompletionsClient
from azure.core.credentials import AzureKeyCredential
client = ChatCompletionsClient(
endpoint="https://<resource>.services.ai.azure.com/models",
credential=AzureKeyCredential(os.environ["AZURE_INFERENCE_CREDENTIAL"]),
)
以下示例显示了已触发保护和控制的聊天补全请求的响应。
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
遵循最佳做法
若要解决与特定模型、应用程序和部署方案相关的潜在危害,请使用迭代标识过程(如红团队测试、压力测试和分析)和度量过程来通知内容筛选配置决策。 实现内容筛选等缓解措施后,重复度量以测试有效性。
有关 Azure OpenAI 的负责任的 AI 的建议和最佳做法(基于 Microsoft负责任的 AI 标准)请参阅 Azure OpenAI 的负责任的 AI 概述。
集成到 Azure AI Foundry 的内容筛选系统与 Azure AI Foundry 模型一起运行。 它使用多类分类模型的合奏,在四个严重级别(安全、低、中、高)检测四类有害内容(暴力、仇恨、性与自我伤害)。 它提供可选的二进制分类器,用于检测公共存储库中的越狱风险、现有文本和代码。 有关内容类别、严重性级别和内容筛选系统行为的详细信息,请参阅 以下文章。
对于提示和完成, 默认内容筛选 配置会筛选所有四个有害类别的中等严重性阈值的内容。 在中等或高严重级别检测到的内容将被筛选掉,而检测到的低级别或安全严重性级别的内容不会被筛选。
可以在资源级别配置内容筛选器,并将其与一个或多个部署相关联。
先决条件
若要完成本文,需要做好以下准备:
一份 Azure 订阅。 如果你正在使用 GitHub 模型,则可以升级体验并在此过程中创建 Azure 订阅。 读取从 GitHub 模型升级到 Azure AI Foundry 的升级。
Azure AI Foundry 资源(以前称为 Azure AI 服务资源)。 有关详细信息,请参阅 创建 Azure AI Foundry 资源。
添加带有自定义内容筛选的模型部署
建议使用 Azure AI Foundry 门户或使用 Bicep 在代码中创建内容筛选器。 不支持使用 Azure CLI 创建自定义内容筛选器或将其应用到部署。
在代码中考虑内容筛选
将内容筛选应用到模型部署时,服务可以根据输入和输出截获请求。 如果内容筛选器触发,服务将返回一个 400 错误代码,其中包含触发错误的规则的说明。
使用包管理器(例如 pip)安装包 azure-ai-inference:
pip install azure-ai-inference
然后,可以使用该包来调用模型。 以下示例演示如何创建客户端程序来调用聊天结果:
import os
from azure.ai.inference import ChatCompletionsClient
from azure.core.credentials import AzureKeyCredential
client = ChatCompletionsClient(
endpoint="https://<resource>.services.ai.azure.com/models",
credential=AzureKeyCredential(os.environ["AZURE_INFERENCE_CREDENTIAL"]),
)
以下示例显示了已触发保护和控制的聊天补全请求的响应。
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
遵循最佳做法
若要解决与特定模型、应用程序和部署方案相关的潜在危害,请使用迭代标识过程(如红团队测试、压力测试和分析)和度量过程来通知内容筛选配置决策。 实现内容筛选等缓解措施后,重复度量以测试有效性。
有关 Azure OpenAI 的负责任的 AI 的建议和最佳做法(基于 Microsoft负责任的 AI 标准)请参阅 Azure OpenAI 的负责任的 AI 概述。
集成到 Azure AI Foundry 的内容筛选系统与 Azure AI Foundry 模型一起运行。 它使用多类分类模型的合奏,在四个严重级别(安全、低、中、高)检测四类有害内容(暴力、仇恨、性与自我伤害)。 它提供可选的二进制分类器,用于检测公共存储库中的越狱风险、现有文本和代码。 有关内容类别、严重性级别和内容筛选系统行为的详细信息,请参阅 以下文章。
对于提示和完成, 默认内容筛选 配置会筛选所有四个有害类别的中等严重性阈值的内容。 在中等或高严重级别检测到的内容将被筛选掉,而检测到的低级别或安全严重性级别的内容不会被筛选。
可以在资源级别配置内容筛选器,并将其与一个或多个部署相关联。
先决条件
若要完成本文,需要做好以下准备:
一份 Azure 订阅。 如果你正在使用 GitHub 模型,则可以升级体验并在此过程中创建 Azure 订阅。 读取从 GitHub 模型升级到 Azure AI Foundry 的升级。
Azure AI Foundry 资源(以前称为 Azure AI 服务资源)。 有关详细信息,请参阅 创建 Azure AI Foundry 资源。
安装 Azure CLI。
标识以下信息:
Azure 订阅 ID。
你的 Azure AI 服务资源名称。
在其中部署 Azure AI 服务资源的资源组。
要部署的模型名称、提供程序、版本和 SKU。 可以使用 Azure AI Foundry 门户或 Azure CLI 查找此信息。 在此示例中,部署以下模型:
-
模型名称:
Phi-4-mini-instruct -
提供商:
Microsoft -
版本:
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 } }使用模板
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' 'xAI' 'Meta' 'Microsoft' 'Mistral AI' 'OpenAI' ]) @description('Model provider') param modelPublisherFormat string @allowed([ 'GlobalStandard' 'DataZoneStandard' '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 } }创建主部署定义:
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 ] }运行部署:
RESOURCE_GROUP="<resource-group-name>" ACCOUNT_NAME="<azure-ai-model-inference-name>" MODEL_NAME="Phi-4-mini-instruct" PROVIDER="Microsoft" VERSION=1 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
然后,可以使用该包来调用模型。 以下示例演示如何创建客户端程序来调用聊天结果:
import os
from azure.ai.inference import ChatCompletionsClient
from azure.core.credentials import AzureKeyCredential
client = ChatCompletionsClient(
endpoint="https://<resource>.services.ai.azure.com/models",
credential=AzureKeyCredential(os.environ["AZURE_INFERENCE_CREDENTIAL"]),
)
以下示例显示了已触发保护和控制的聊天补全请求的响应。
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
遵循最佳做法
若要解决与特定模型、应用程序和部署方案相关的潜在危害,请使用迭代标识过程(如红团队测试、压力测试和分析)和度量过程来通知内容筛选配置决策。 实现内容筛选等缓解措施后,重复度量以测试有效性。
有关 Azure OpenAI 的负责任的 AI 的建议和最佳做法(基于 Microsoft负责任的 AI 标准)请参阅 Azure OpenAI 的负责任的 AI 概述。