Skip to main content

ai-aliyun-content-moderation

Moderates request content with Alibaba Cloud before the LLM.

Category AIAdd it to Route, Service, Consumer, Consumer group, Global rule, Plugin configEngine plugin ai-aliyun-content-moderationPriority 1029

What it does​

ai-aliyun-content-moderation asks Alibaba Cloud content moderation to rate each prompt before the model receives it, and with Check response on, the model's answer too. A prompt rated at or above Risk level bar is refused with Deny code, and one rated below goes on to the model.

It rates only what an AI proxy on the same route handles, ai-proxy or ai-proxy-multi. In the example, a prompt the service rates high is answered 403 with the answer the service advises, and a prompt rated none reaches the model, whose answer the client gets.

When to use it​

  • A chat assistant for customers in China, whose prompts are rated by Alibaba Cloud in the cn-shanghai region before any model sees them.
  • A team that already moderates what its users post with Alibaba Cloud and wants the same verdict on prompts sent to a model.
  • Routes that spread prompts over several models with ai-proxy-multi, rated the same way as on a route with one model.
  • A product for a young audience, where a lower Risk level bar refuses prompts the default bar lets through.

Not for refusing prompts by patterns you write yourself — see ai-prompt-guard (Allows or denies prompts that match configured patterns), toxicity scores per category from Amazon Comprehend — see ai-aws-content-moderation (Moderates request content with AWS Comprehend 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 Alibaba Cloud content moderation rates high risk or above​

Acts only on a route that also carries ai-proxy or ai-proxy-multi.

Under Basics set Endpoint to https://green-cip.cn-shanghai.aliyuncs.com, set Region ID to cn-shanghai, set Access key ID to $env://ALIBABA_CLOUD_ACCESS_KEY_ID and set Access key secret to <secret>. Under Behavior leave Risk level bar at high and set Deny code to 403.

ai-aliyun-content-moderation · as the console saves it
{
"endpoint": "https://green-cip.cn-shanghai.aliyuncs.com",
"region_id": "cn-shanghai",
"access_key_id": "$env://ALIBABA_CLOUD_ACCESS_KEY_ID",
"access_key_secret": "<secret>",
"risk_level_bar": "high",
"deny_code": 403
}

Things to know​

  • Risk level bar — With the bar at high, a prompt the service rates high is refused, and one it rates none reaches the model and gets its answer.
  • Deny message — Left empty, the refused client reads the answer the moderation service advises for that prompt, in the body of the 403 in the example.
  • The same configuration refuses the same prompt on a route whose model is chosen by ai-proxy-multi instead of ai-proxy.
  • Endpoint — A moderation call that fails is only logged: the content travels on to the model unchecked.
  • Endpoint — An address without its scheme is refused when you save.
  • Check response — On a streamed answer the default final_packet mode only tags flagged text and sends it on, while realtime cuts the stream.
  • Risk level bar — A risk at or above the bar is denied, so a bar of none denies every request the service scores.
  • 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 roles — Only system, tool and user can be listed; assistant is refused when you save.
  • Keepalive — Off, every request this plugin checks answers 500: the connection to the moderation service is refused its pool setting before anything is sent.
  • 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​

EndpointendpointstringRequired

The moderation service address for your region, such as https://green-cip.cn-shanghai.aliyuncs.com.

at least 1 character · an http or https address
pattern^https?://
Region IDregion_idstringRequired
at least 1 character
Access key IDaccess_key_idstringRequired
at least 1 character
Access key secretaccess_key_secretstringRequired

Stored on the engine and shown only to roles that manage this object.

at least 1 character
Check requestcheck_requestbooleandefault true
Check responsecheck_responsebooleandefault false

Behavior​

Risk level barrisk_level_barstringdefault high
one of high, low, max, medium, none
Deny codedeny_codeintegerdefault 200

The status a denied request is answered with, where 200 reads as a normal completion to client libraries and a 4xx as an error.

200 to 599
Deny messagedeny_messagestring
Fail modefail_modestringdefault skip

What 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.

one of error, skip, warn
Request check moderequest_check_modestringdefault last

Which messages of the selected roles to moderate: last is the latest consecutive block, and all is every one of them. A system role selected below is checked on every request, whatever the mode.

one of all, last
Request check rolesrequest_check_rolesarray of stringsdefault ["user"]

The message roles moderated on the request side. User and tool 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.

at least 1 item · one of system, tool, user
Request check servicerequest_check_servicestringdefault llm_query_moderation
at least 1 character
Response check serviceresponse_check_servicestringdefault llm_response_moderation
at least 1 character
Request check length limitrequest_check_length_limitintegerdefault 2000
at least 1
Response check length limitresponse_check_length_limitintegerdefault 5000
at least 1
Timeouttimeoutintegerdefault 10000

How long the moderation service has to answer one call.

at least 1 · in milliseconds
SSL verifyssl_verifybooleandefault true

Advanced​

Stream check modestream_check_modestringdefault final_packet

Realtime checks a streamed answer in batches as it arrives, and final packet checks it once at the end and tags the last chunk.

one of final_packet, realtime
Stream check intervalstream_check_intervalnumberdefault 3

How long a batch of streamed text may gather before it is checked, in realtime mode.

at least 0.1 · in seconds
Stream check cache sizestream_check_cache_sizeintegerdefault 128

How much streamed text a batch may gather before it is checked, in realtime mode.

at least 1
Keepalivekeepalivebooleandefault true
one of true
Keepalive poolkeepalive_poolintegerdefault 30
at least 1
Keepalive timeoutkeepalive_timeoutintegerdefault 60000
at least 1000 · in milliseconds

Every plugin also has Enabled, Priority, Filter, Error response and Pre-function: see Plugin execution settings.

G8 dev · Apache APISIX® 3.19.0
Apache, Apache APISIX and APISIX are trademarks of The Apache Software Foundation.
Facts
Category
AI
Add it to
Route, Service, Consumer, Consumer group, Global rule, Plugin config
Engine plugin
ai-aliyun-content-moderation
Priority
1029