feat: ai-content-moderation plugin#11541
Conversation
| end | ||
|
|
||
|
|
||
| function _M.get_body_table() |
There was a problem hiding this comment.
ai-proxy PR also has this code so later we can merge from master after ai-proxy is merged.
There was a problem hiding this comment.
note that the name of the method there changed :D
|
|
||
| The `ai-content-moderation` plugin processes the request body to check for toxicity and rejects the request if it exceeds the configured threshold. | ||
|
|
||
| **_This plugin must be used in routes that proxy requests to LLMs only._** |
There was a problem hiding this comment.
Just routes? no services?
Or do you just want to stress the upstream should be LLM providers.
There was a problem hiding this comment.
Or do you just want to stress the upstream should be LLM providers.
this.
There was a problem hiding this comment.
In that case I actually think this sentence is redundant. how about just mention It is used when integrating with LLMs. in the paragraph above?
| "ai-proxy": { | ||
| "auth": { | ||
| "header": { | ||
| "Authorization": "Bearer token" |
There was a problem hiding this comment.
| "Authorization": "Bearer token" | |
| "Authorization": "Bearer <your-api-token>" |
or
"Authorization": "Bearer '"$OPENAI_API_KEY"'"
this takes env var
|
|
||
| ```shell | ||
| curl http://127.0.0.1:9080/post -i -XPOST -H 'Content-Type: application/json' -d '{ | ||
| "info": "<some very seriously profane message>" |
There was a problem hiding this comment.
Is this a dummy? Shouldn't the format to OpenAI be something like this?
"messages": [
{ "role": "system", "content": "system prompt goes here" },
{ "role": "user", "content": "offensive user prompts" }
]
| request body exceeds toxicity threshold | ||
| ``` | ||
|
|
||
| Send a request with normal request body: |
There was a problem hiding this comment.
| Send a request with normal request body: | |
| Send a request with compliant content in the request body: |
| Send a request with normal request body: | ||
|
|
||
| ```shell | ||
| curl http://127.0.0.1:9080/post -i -XPOST -H 'Content-Type: application/json' -d 'APISIX is wonderful' |
There was a problem hiding this comment.
The opening paragraph says This plugin must be used in routes that proxy requests to LLMs only yet the example does not involve proxying to LLM. It feels a bit self-conflicting.
The example demonstrates exactly the integration could be used for general purpose and checking requests NOT proxying to LLM.
| | provider.aws_comprehend.secret_access_key | Yes | String | AWS secret access key | | ||
| | provider.aws_comprehend.region | Yes | String | AWS region | | ||
| | provider.aws_comprehend.endpoint | No | String | AWS Comprehend service endpoint. Must match the pattern `^https?://` | | ||
| | moderation_categories | No | Object | Configuration for moderation categories. Must be one of: PROFANITY, HATE_SPEECH, INSULT, HARASSMENT_OR_ABUSE, SEXUAL, VIOLENCE_OR_THREAT | |
There was a problem hiding this comment.
| | moderation_categories | No | Object | Configuration for moderation categories. Must be one of: PROFANITY, HATE_SPEECH, INSULT, HARASSMENT_OR_ABUSE, SEXUAL, VIOLENCE_OR_THREAT | | |
| | moderation_categories | No | Object | Key-value pairs of moderation category and their score. In each pair, the key should be one of the `PROFANITY`, `HATE_SPEECH`, `INSULT`, `HARASSMENT_OR_ABUSE`, `SEXUAL`, or `VIOLENCE_OR_THREAT`; and the value should be between 0 and 1 (inclusive). | |
| | provider.aws_comprehend.region | Yes | String | AWS region | | ||
| | provider.aws_comprehend.endpoint | No | String | AWS Comprehend service endpoint. Must match the pattern `^https?://` | | ||
| | moderation_categories | No | Object | Configuration for moderation categories. Must be one of: PROFANITY, HATE_SPEECH, INSULT, HARASSMENT_OR_ABUSE, SEXUAL, VIOLENCE_OR_THREAT | | ||
| | toxicity_level | No | Number | Threshold for overall toxicity detection. Range: 0 - 1. Default: 0.5 | |
There was a problem hiding this comment.
| | toxicity_level | No | Number | Threshold for overall toxicity detection. Range: 0 - 1. Default: 0.5 | | |
| | toxicity_level | No | Number | The degree to which content is harmful, offensive, or inappropriate. A higher value indicates more toxic content allowed. Range: 0 - 1. Default: 0.5 | |
|
|
||
| **_This plugin must be used in routes that proxy requests to LLMs only._** | ||
|
|
||
| **_As of now only the AWS Comprehend service is supported for content moderation, PRs for introducing support for other service providers are welcome._** |
There was a problem hiding this comment.
| **_As of now only the AWS Comprehend service is supported for content moderation, PRs for introducing support for other service providers are welcome._** | |
| **_As of now, the plugin only supports the integration with [AWS Comprehend](https://aws.amazon.com/comprehend/) for content moderation. PRs for introducing support for other service providers are welcomed._** |
| function _M.check_schema(conf) | ||
| return core.schema.check(schema, conf) | ||
| end | ||
|
|
There was a problem hiding this comment.
Two blank lines between functions?
| type = "object", | ||
| properties = { | ||
| provider = { | ||
| type = "object", |
There was a problem hiding this comment.
| type = "object", | |
| type = "object", | |
| maxProperties = 1, |
To make sure next(conf.provider) always returns aws_comprehend
| return bad_request, "messages not found in request body" | ||
| end | ||
|
|
||
| local provider = conf.provider[next(conf.provider)] |
There was a problem hiding this comment.
The current schema definition does not seem to be able to prevent multiple properties from being entered incorrectly. It is recommended that you consider adding a maxProperties = 1 constraint to the schema.
|
LGTM |
Description
The
content-moderationplugin processes the request body to check for toxicity and rejects the request if it exceeds the configured threshold.In later PRs, other plugins like ai-prompt-decorator and ai-prompt-template can use function from this plugin to ensure content moderation in requests proxying LLMs.
Checklist