|
| 1 | +--- |
| 2 | +title: ai-aliyun-content-moderation |
| 3 | +keywords: |
| 4 | + - Apache APISIX |
| 5 | + - API 网关 |
| 6 | + - Plugin |
| 7 | + - ai-aliyun-content-moderation |
| 8 | + - 阿里云 |
| 9 | + - 内容审核 |
| 10 | +description: 本文档包含有关 Apache APISIX ai-aliyun-content-moderation 插件的信息。 |
| 11 | +--- |
| 12 | + |
| 13 | +<!-- |
| 14 | +# |
| 15 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 16 | +# contributor license agreements. See the NOTICE file distributed with |
| 17 | +# this work for additional information regarding copyright ownership. |
| 18 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 19 | +# (the "License"); you may not use this file except in compliance with |
| 20 | +# the License. You may obtain a copy of the License at |
| 21 | +# |
| 22 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 23 | +# |
| 24 | +# Unless required by applicable law or agreed to in writing, software |
| 25 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 26 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 27 | +# See the License for the specific language governing permissions and |
| 28 | +# limitations under the License. |
| 29 | +# |
| 30 | +--> |
| 31 | + |
| 32 | +## 描述 |
| 33 | + |
| 34 | +`ai-aliyun-content-moderation` 插件集成了阿里云的内容审核服务,用于在与大语言模型 (LLM) 交互时检查请求和响应内容是否包含不当材料。它支持实时流式检查和最终数据包审核两种模式。 |
| 35 | + |
| 36 | +此插件必须在使用 `ai-proxy` 或 `ai-proxy-multi` 插件的路由中使用。 |
| 37 | + |
| 38 | +## 插件属性 |
| 39 | + |
| 40 | +| **字段** | **必选项** | **类型** | **描述** | |
| 41 | +| ---------------------------- | ---------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 42 | +| endpoint | 是 | String | 阿里云服务端点 URL | |
| 43 | +| region_id | 是 | String | 阿里云区域标识符 | |
| 44 | +| access_key_id | 是 | String | 阿里云访问密钥 ID | |
| 45 | +| access_key_secret | 是 | String | 阿里云访问密钥密码 | |
| 46 | +| check_request | 否 | Boolean | 启用请求内容审核。默认值:`true` | |
| 47 | +| check_response | 否 | Boolean | 启用响应内容审核。默认值:`false` | |
| 48 | +| stream_check_mode | 否 | String | 流式审核模式。默认值:`"final_packet"`。有效值:`["realtime", "final_packet"]` | |
| 49 | +| stream_check_cache_size | 否 | Integer | 实时模式下每次审核批次的最大字符数。默认值:`128`。必须 `>= 1` | |
| 50 | +| stream_check_interval | 否 | Number | 实时模式下批次检查之间的间隔秒数。默认值:`3`。必须 `>= 0.1` | |
| 51 | +| request_check_service | 否 | String | 用于请求审核的阿里云服务。默认值:`"llm_query_moderation"` | |
| 52 | +| request_check_length_limit | 否 | Number | 每个请求审核块的最大字符数。默认值:`2000` | |
| 53 | +| response_check_service | 否 | String | 用于响应审核的阿里云服务。默认值:`"llm_response_moderation"` | |
| 54 | +| response_check_length_limit | 否 | Number | 每个响应审核块的最大字符数。默认值:`5000` | |
| 55 | +| risk_level_bar | 否 | String | 内容拒绝的阈值。默认值:`"high"`。有效值:`["none", "low", "medium", "high", "max"]` | |
| 56 | +| deny_code | 否 | Number | 被拒绝内容的 HTTP 状态码。默认值:`200` | |
| 57 | +| deny_message | 否 | String | 被拒绝内容的自定义消息。默认值:`-` | |
| 58 | +| timeout | 否 | Integer | 请求超时时间(毫秒)。默认值:`10000`。必须 `>= 1` | |
| 59 | +| ssl_verify | 否 | Boolean | 启用 SSL 证书验证。默认值:`true` | |
| 60 | + |
| 61 | +## 使用示例 |
| 62 | + |
| 63 | +首先初始化这些 shell 变量: |
| 64 | + |
| 65 | +```shell |
| 66 | +ADMIN_API_KEY=edd1c9f034335f136f87ad84b625c8f1 |
| 67 | +ALIYUN_ACCESS_KEY_ID=your-aliyun-access-key-id |
| 68 | +ALIYUN_ACCESS_KEY_SECRET=your-aliyun-access-key-secret |
| 69 | +ALIYUN_REGION=cn-hangzhou |
| 70 | +ALIYUN_ENDPOINT=https://green.cn-hangzhou.aliyuncs.com |
| 71 | +OPENAI_KEY=your-openai-api-key |
| 72 | +``` |
| 73 | + |
| 74 | +创建一个带有 `ai-aliyun-content-moderation` 和 `ai-proxy` 插件的路由: |
| 75 | + |
| 76 | +```shell |
| 77 | +curl "http://127.0.0.1:9180/apisix/admin/routes/1" -X PUT \ |
| 78 | + -H "X-API-KEY: ${ADMIN_API_KEY}" \ |
| 79 | + -d '{ |
| 80 | + "uri": "/v1/chat/completions", |
| 81 | + "plugins": { |
| 82 | + "ai-proxy": { |
| 83 | + "provider": "openai", |
| 84 | + "auth": { |
| 85 | + "header": { |
| 86 | + "Authorization": "Bearer '"$OPENAI_KEY"'" |
| 87 | + } |
| 88 | + }, |
| 89 | + "override": { |
| 90 | + "endpoint": "http://localhost:6724/v1/chat/completions" |
| 91 | + } |
| 92 | + }, |
| 93 | + "ai-aliyun-content-moderation": { |
| 94 | + "endpoint": "'"$ALIYUN_ENDPOINT"'", |
| 95 | + "region_id": "'"$ALIYUN_REGION"'", |
| 96 | + "access_key_id": "'"$ALIYUN_ACCESS_KEY_ID"'", |
| 97 | + "access_key_secret": "'"$ALIYUN_ACCESS_KEY_SECRET"'", |
| 98 | + "risk_level_bar": "high", |
| 99 | + "check_request": true, |
| 100 | + "check_response": true, |
| 101 | + "deny_code": 400, |
| 102 | + "deny_message": "您的请求违反了内容政策" |
| 103 | + } |
| 104 | + } |
| 105 | + }' |
| 106 | +``` |
| 107 | + |
| 108 | +这里使用 `ai-proxy` 插件是因为它简化了对 LLM 的访问。不过,您也可以在上游配置中配置 LLM。 |
| 109 | + |
| 110 | +现在发送一个请求: |
| 111 | + |
| 112 | +```shell |
| 113 | +curl http://127.0.0.1:9080/v1/chat/completions -i \ |
| 114 | + -H "Content-Type: application/json" \ |
| 115 | + -d '{ |
| 116 | + "model": "gpt-3.5-turbo", |
| 117 | + "messages": [ |
| 118 | + {"role": "user", "content": "I want to kill you"} |
| 119 | + ], |
| 120 | + "stream": false |
| 121 | + }' |
| 122 | +``` |
| 123 | + |
| 124 | +然后请求将被阻止,并返回如下错误: |
| 125 | + |
| 126 | +```text |
| 127 | +HTTP/1.1 400 Bad Request |
| 128 | +Content-Type: application/json |
| 129 | +
|
| 130 | +{"id":"chatcmpl-123","object":"chat.completion","model":"gpt-3.5-turbo","choices":[{"index":0,"message":{"role":"assistant","content":"您的请求违反了内容政策"},"finish_reason":"stop"}],"usage":{"prompt_tokens":0,"completion_tokens":0,"total_tokens":0}} |
| 131 | +``` |
0 commit comments