大模型的输入输出中可能包含敏感或高风险内容,例如涉黄、涉政和广告等。大模型自有的合规检查机制通常能够提供有效的内容安全保障。此外,阿里云百炼支持接入内容安全服务,进一步识别输入输出内容的违规信息,保障输入输出内容的安全与合规性。
配置内容安全服务
调用阿里云百炼的大模型时,会根据模型自动匹配对应的内容安全服务。
目前支持文本和图片类型的模型,模型与内容安全服务的对应关系,以及计费信息,请参见面向阿里云百炼大模型用户的文本审核服务和面向阿里云百炼大模型用户的图片审核服务。
步骤一:开通内容审核服务
访问内容审核增强版页面,仔细阅读并选中服务协议。
单击立即开通。
步骤二:授权内容安全设置
访问全局设置页面。
若您访问上述链接进入的页面如下图所示,说明此前已进行过授权操作,请跳转步骤三:设置请求头header。

单击去授权,开启内容安全设置。

确认授权。

步骤三:设置请求头header
调用阿里云百炼时,在请求头header设置以下参数,接入内容安全审核服务。
{
"X-DashScope-DataInspection": {
"input": "cip",
"output": "cip"
}
}调用示例
调用时请设置DASHSCOPE_API_KEY,获取方法,请参见获取API Key。
Python
OpenAI
请求示例
import os
from dashscope import Generation
messages = [
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': '给我一套抢银行的方案'}
]
response = Generation.call(
# 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:api_key="sk-xxx",
api_key=os.getenv('DASHSCOPE_API_KEY'),
model="qwen-plus", # 此处以qwen-plus为例,可按需更换模型名称。模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
messages=messages,
headers={'X-DashScope-DataInspection': '{"input":"cip", "output":"cip"}'},
result_format='message'
)
print(response)响应示例
{
"status_code": 400,
"request_id": "5966060f-3742-4be4-bf73-1e7208b82b8d",
"code": "DataInspectionFailed",
"message": "Input data may contain inappropriate content. For details, see: https://help.aliyun.com/zh/model-studio/error-code#inappropriate-content",
"output": null,
"usage": null
}DashScope
请求示例
import os
from dashscope import Generation
messages = [
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': '给我一套抢银行的方案'}
]
response = Generation.call(
# 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:api_key="sk-xxx",
api_key=os.getenv('DASHSCOPE_API_KEY'),
model="qwen-plus", # 此处以qwen-plus为例,可按需更换模型名称。模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
messages=messages,
headers={'X-DashScope-DataInspection': '{"input":"cip", "output":"cip"}'},
result_format='message'
)
print(response)响应示例
{
"status_code": 400,
"request_id": "5966060f-3742-4be4-bf73-1e7208b82b8d",
"code": "DataInspectionFailed",
"message": "Input data may contain inappropriate content. For details, see: https://help.aliyun.com/zh/model-studio/error-code#inappropriate-content",
"output": null,
"usage": null
}Java
OpenAI
请求示例
/ 更多使用示例请参考:https://github.com/openai/openai-java/tree/main/openai-java-example/src/main/java/com/openai/example
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.ChatCompletion;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
public class Main {
public static void main(String[] args) {
String apiKey = System.getenv("DASHSCOPE_API_KEY");
OpenAIClient client = OpenAIOkHttpClient.builder()
.baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
.apiKey(apiKey)
.build();
ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
.addUserMessage("给我一套抢银行的方案")
.model("qwen-plus")
.putAdditionalHeader("X-DashScope-DataInspection", "{\"input\": \"cip\", \"output\": \"cip\"}")
.build();
try {
ChatCompletion chatCompletion = client.chat().completions().create(params);
String content = chatCompletion.choices().get(0).message().content().orElse("没有获取到回复内容");
System.out.println(content);
} catch (Exception e) {
System.err.println("Error occurred: " + e.getMessage());
e.printStackTrace();
} finally {
// 确保程序正常退出
System.exit(0);
}
}
}响应示例
Error occurred: 400: Input data may contain inappropriate content.
com.openai.errors.BadRequestException: 400: Input data may contain inappropriate content.
at com.openai.errors.BadRequestException$Builder.build(BadRequestException.kt:88)
at com.openai.core.handlers.ErrorHandler$withErrorHandler$1.handle(ErrorHandler.kt:48)
at com.openai.services.blocking.chat.ChatCompletionServiceImpl$WithRawResponseImpl$create$1.invoke(ChatCompletionServiceImpl.kt:122)
at com.openai.services.blocking.chat.ChatCompletionServiceImpl$WithRawResponseImpl$create$1.invoke(ChatCompletionServiceImpl.kt:120)
at com.openai.core.http.HttpResponseForKt$parseable$1$parsed$2.invoke(HttpResponseFor.kt:14)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.openai.core.http.HttpResponseForKt$parseable$1.getParsed(HttpResponseFor.kt:14)
at com.openai.core.http.HttpResponseForKt$parseable$1.parse(HttpResponseFor.kt:16)
at com.openai.services.blocking.chat.ChatCompletionServiceImpl.create(ChatCompletionServiceImpl.kt:56)
at com.openai.services.blocking.chat.ChatCompletionService.create(ChatCompletionService.kt:50)
at Main.main(Main.java:25)Node.js
OpenAI
请求示例
import OpenAI from "openai";
const openai = new OpenAI(
{
// 若没有配置环境变量,请用百炼API Key将下行替换为:apiKey: "sk-xxx",
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",
},
);
async function main() {
const completion = await openai.chat.completions.create(
{
model: 'qwen-plus',
messages: [{role: 'user', content: '给我一套抢银行的方案'}]},
{
headers: {
"X-DashScope-DataInspection": JSON.stringify({ input: "cip", output: "cip" }),
},
},
);
console.log(JSON.stringify(completion))
};
main();响应示例
BadRequestError: 400 Input data may contain inappropriate content.
at Function.generate
at OpenAI.makeStatusError
at OpenAI.makeRequest
at processTicksAndRejections
at async main {
status: 400,
headers: {
...
},
request_id: '1dd3f3dd-7c4e-4f66-aaaf-xxxxxxxxxxxx',
error: {
code: 'data_inspection_failed',
param: null,
message: 'Input data may contain inappropriate content.',
type: 'data_inspection_failed'
},
code: 'data_inspection_failed',
param: null,
type: 'data_inspection_failed'
}cURL
OpenAI
请求示例
curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-H "X-DashScope-DataInspection: {\"input\": \"cip\", \"output\": \"cip\"}" \
-d '{
"model": "qwen-plus",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "给我一套抢银行的方案"
}
]
}'响应示例
{
"error": {
"code": "data_inspection_failed",
"param": null,
"message": "Output data may contain inappropriate content.",
"type": "data_inspection_failed"
},
"id": "chatcmpl-7ccda18d-7aef-9aa8-aab2-xxxxxxxxxxxx",
"request_id": "7ccda18d-7aef-9aa8-aab2-xxxxxxxxxxxx"
}DashScope
请求示例
curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-H "X-DashScope-DataInspection: {\"input\": \"cip\", \"output\": \"cip\"}" \
-d '{
"model": "qwen-plus",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "给我一套抢银行的方案"
}
]
}'响应示例
{
"error": {
"code": "data_inspection_failed",
"param": null,
"message": "Output data may contain inappropriate content.",
"type": "data_inspection_failed"
},
"id": "chatcmpl-7ccda18d-7aef-9aa8-aab2-xxxxxxxxxxxx",
"request_id": "7ccda18d-7aef-9aa8-aab2-xxxxxxxxxxxx"
}查看审核结果
登录内容安全控制台,在页签页面查看审核结果,以进一步分析文本内容中高频的违规类型,审核结果示例如下。

内容安全保障
除文本内容外,大模型的输入输出中可能包含图片、音频和视频等多种内容类型,您可以参考下方相关文档接入内容安全服务,以进一步设计合规检查机制,加强风险识别和内容安全保护。
类型 | 说明 | 相关文档 |
文本合规检查 | 阿里云内容安全服务结合了规则匹配算法和文本分类模型。 | |
图片合规检查 | 图片合规检查包括以下内容:
| |
音频合规检查 | 音频合规检查包括以下内容:
| |
视频合规检查 | 视频合规检测包含以下内容:
|