ai-aws-content-moderation
Moderates request content with AWS Comprehend before the LLM.
ai-aws-content-moderationPriority 1031What it does
ai-aws-content-moderation sends the text of each prompt to Amazon Comprehend for a toxicity score before the model receives it, and with Check response on, the model's answer too. Content whose overall score passes Moderation threshold, or whose score in one category passes its value under Moderation categories, is refused with Deny code.
It scores only what an AI proxy on the same route handles, ai-proxy or ai-proxy-multi. The refusal is answered 200 unless you change Deny code, and client libraries read that as a normal completion, not as an error.
When to use it
- A community assistant whose prompts must not carry insults or harassment, scored before any model reads them.
- A company on AWS that already holds credentials for Comprehend in its own region,
eu-west-1for example. - One category held stricter than the rest, for example
PROFANITYunder Moderation categories with a value below the overall threshold. - Temporary credentials of an assumed role, given with their Session token.
Not for refusing prompts by patterns you write yourself — see ai-prompt-guard (Allows or denies prompts that match configured patterns), prompts rated by risk level with Alibaba Cloud content moderation — see ai-aliyun-content-moderation (Moderates request content with Alibaba Cloud before the LLM) or asking your own authorization service whether a request may go on — see forward-auth (Delegates authentication to an external service).
Set it up
Refuse prompts Amazon Comprehend scores as toxic
Acts only on a route that also carries ai-proxy or ai-proxy-multi.
Under Basics fill Comprehend as in the JSON below. Under Behavior set Moderation threshold to 0.8 and set Deny code to 403.
{
"comprehend": {
"access_key_id": "$env://AWS_ACCESS_KEY_ID",
"secret_access_key": "<secret>",
"region": "eu-west-1"
},
"moderation_threshold": 0.8,
"deny_code": 403
}
Things to know
- Moderation categories — A category over its value denies the content whatever the overall bar says, so a value below the overall bar makes that category stricter than the rest.
- Moderation categories — A category outside the six the help names is refused when you save.
- Moderation categories — Each value is a score from 0 to 1, and a value above 1 is refused when you save.
- Region — An empty region is refused when you save.
- Check request — A request whose content cannot be scored, because the service fails or answers something unreadable, is refused with 500. A streamed answer is let through unscored.
- Check response — On a streamed answer the default final_packet mode only tags flagged text and sends it on, while realtime cuts the stream.
- Fail mode — Nothing is scanned unless an AI proxy on the same route picked a model first, and the default then lets the request through in silence.
- Request check mode — A selected
systemrole is checked on every request whatever the mode, and selectingassistantmakes the last block longer. - Request check roles — A role listed twice is refused when you save.
- Request check length limit — A segment may hold at most
1024bytes, and2000is refused when you save. - AdviceSet Deny code to a 4xx, for example
403, when your clients should treat a refused prompt as an error.
Settings
The fields as the console shows them, section by section.
Basics
comprehendobjectRequiredcomprehend.access_key_idstringRequiredcomprehend.regionstringRequiredcomprehend.secret_access_keystringRequiredStored on the engine and shown only to roles that manage this object.
comprehend.endpointstringpattern
^https?://comprehend.session_tokenstringThe session token of temporary credentials, such as an assumed role's, left empty for a long-lived key pair. Stored on the engine and shown only to roles that manage this object.
comprehend.ssl_verifybooleandefault truecheck_requestbooleandefault truecheck_responsebooleandefault falseBehavior
moderation_thresholdnumberdefault 0.5The bar for the overall toxicity score, read on every request even where a category below carries a threshold of its own.
moderation_categoriesobjectThresholds per category, keyed by PROFANITY, HATE_SPEECH, INSULT, HARASSMENT_OR_ABUSE, SEXUAL or VIOLENCE_OR_THREAT, each denying the content on its own when exceeded. A category over its value denies the content whatever the overall bar says. An object whose keys are a name matching ^(PROFANITY|HATE_SPEECH|INSULT|HARASSMENT_OR_ABUSE|SEXUAL|VIOLENCE_OR_THREAT)$, each with a number as its value.
deny_codeintegerdefault 200The status a denied request is answered with, where 200 reads as a normal completion to client libraries and a 4xx as an error.
deny_messagestringfail_modestringdefault skipWhat to do with a request the plugin cannot inspect: skip lets it through, warn also logs it, and error rejects it with 500. Plain traffic that did not pass through an AI proxy is such a request.
request_check_modestringdefault allWhich messages of the selected roles to moderate: last is the latest consecutive block, and all is every one of them. A selected system role is checked either way, and choosing assistant widens the last block.
request_check_rolesarray of stringsdefault ["user","tool","system","assistant"]The message roles moderated on the request side, all four by default. User, tool and assistant follow the mode above. A selected system role is checked on every request, and a tool result is read only where the protocol gives it a role of its own.
request_check_length_limitintegerdefault 1000The longest text segment sent for scoring, where longer content is cut into several segments of the same call.
response_check_length_limitintegerdefault 1000The longest response text segment sent for scoring, where longer content is cut into several segments of the same call.
timeoutintegerdefault 10000How long the moderation service has to answer one call.
Advanced
stream_check_modestringdefault final_packetRealtime checks a streamed answer in batches and replaces the rest on a hit, while final packet checks it once at the end. Final packet tags the last chunk with the risk level.
stream_check_intervalnumberdefault 3How long a batch of streamed text may gather before it is checked, in realtime mode.
stream_check_cache_sizeintegerdefault 128How much streamed text a batch may gather before it is checked, in realtime mode.
keepalivebooleandefault trueKeeps the connection to the moderation service open between calls for reuse.
keepalive_timeoutintegerdefault 60000How long a pooled connection to the moderation service may sit idle before it is closed.