Skip to content

Commit 3260931

Browse files
authored
feat: add ai-aliyun-content-moderation plugin (#12530)
1 parent 54418e1 commit 3260931

File tree

17 files changed

+2020
-144
lines changed

17 files changed

+2020
-144
lines changed

apisix-master-0.rockspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ dependencies = {
8484
"jsonpath = 1.0-1",
8585
"api7-lua-resty-aws == 2.0.2-1",
8686
"multipart = 0.5.9-1",
87+
"luautf8 = 0.1.6-1",
8788
}
8889

8990
build = {

apisix/cli/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ local _M = {
231231
"ai-proxy-multi",
232232
"ai-proxy",
233233
"ai-aws-content-moderation",
234+
"ai-aliyun-content-moderation",
234235
"proxy-mirror",
235236
"proxy-rewrite",
236237
"workflow",

apisix/cli/ngx_tpl.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ http {
806806
set $dubbo_method '';
807807
{% end %}
808808
809+
set $llm_content_risk_level '';
809810
set $request_type 'traditional_http';
810811
811812
set $llm_time_to_first_token '';

apisix/core/ctx.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ do
233233
upstream_upgrade = true,
234234
upstream_connection = true,
235235
upstream_uri = true,
236+
llm_content_risk_level = true,
237+
236238
request_type = true,
237239
llm_time_to_first_token = true,
238240
llm_model = true,

apisix/plugin.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ local expr = require("resty.expr.v1")
2323
local apisix_ssl = require("apisix.ssl")
2424
local re_split = require("ngx.re").split
2525
local ngx = ngx
26+
local ngx_ok = ngx.OK
27+
local ngx_print = ngx.print
2628
local crc32 = ngx.crc32_short
2729
local ngx_exit = ngx.exit
2830
local pkg_loaded = package.loaded
@@ -1296,5 +1298,40 @@ function _M.run_global_rules(api_ctx, global_rules, phase_name)
12961298
end
12971299
end
12981300

1301+
function _M.lua_response_filter(api_ctx, headers, body)
1302+
local plugins = api_ctx.plugins
1303+
if not plugins or #plugins == 0 then
1304+
-- if there is no any plugin, just print the original body to downstream
1305+
ngx_print(body)
1306+
return
1307+
end
1308+
for i = 1, #plugins, 2 do
1309+
local phase_func = plugins[i]["lua_body_filter"]
1310+
if phase_func then
1311+
local conf = plugins[i + 1]
1312+
if not meta_filter(api_ctx, plugins[i]["name"], conf)then
1313+
goto CONTINUE
1314+
end
1315+
1316+
run_meta_pre_function(conf, api_ctx, plugins[i]["name"])
1317+
local code, new_body = phase_func(conf, api_ctx, headers, body)
1318+
if code then
1319+
if code ~= ngx_ok then
1320+
ngx.status = code
1321+
end
1322+
1323+
ngx_print(new_body)
1324+
ngx_exit(ngx_ok)
1325+
end
1326+
if new_body then
1327+
body = new_body
1328+
end
1329+
end
1330+
1331+
::CONTINUE::
1332+
end
1333+
ngx_print(body)
1334+
end
1335+
12991336

13001337
return _M

0 commit comments

Comments
 (0)