Skip to content

Commit f6f3451

Browse files
code review
1 parent ef16068 commit f6f3451

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

apisix/plugins/ai-content-moderation.lua

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,18 @@
1515
-- limitations under the License.
1616
--
1717
local core = require("apisix.core")
18-
local aws = require("resty.aws")
18+
local aws_instance = require("resty.aws")()
1919
local http = require("resty.http")
2020
local fetch_secrets = require("apisix.secret").fetch_secrets
2121

22-
local aws_instance = aws()
2322
local next = next
2423
local pairs = pairs
2524
local unpack = unpack
2625
local type = type
2726
local ipairs = ipairs
2827
local require = require
29-
local INTERNAL_SERVER_ERROR = ngx.HTTP_INTERNAL_SERVER_ERROR
30-
local BAD_REQUEST = ngx.HTTP_BAD_REQUEST
28+
local HTTP_INTERNAL_SERVER_ERROR = ngx.HTTP_INTERNAL_SERVER_ERROR
29+
local HTTP_BAD_REQUEST = ngx.HTTP_BAD_REQUEST
3130

3231

3332
local aws_comprehend_schema = {
@@ -99,17 +98,17 @@ end
9998
function _M.rewrite(conf, ctx)
10099
conf = fetch_secrets(conf, true, conf, "")
101100
if not conf then
102-
return INTERNAL_SERVER_ERROR, "failed to retrieve secrets from conf"
101+
return HTTP_INTERNAL_SERVER_ERROR, "failed to retrieve secrets from conf"
103102
end
104103

105104
local body, err = core.request.get_json_request_body_table()
106105
if not body then
107-
return BAD_REQUEST, err
106+
return HTTP_BAD_REQUEST, err
108107
end
109108

110109
local msgs = body.messages
111110
if type(msgs) ~= "table" or #msgs < 1 then
112-
return BAD_REQUEST, "messages not found in request body"
111+
return HTTP_BAD_REQUEST, "messages not found in request body"
113112
end
114113

115114
local provider = conf.provider[next(conf.provider)]
@@ -145,12 +144,12 @@ function _M.rewrite(conf, ctx)
145144

146145
if not res then
147146
core.log.error("failed to send request to ", provider, ": ", err)
148-
return INTERNAL_SERVER_ERROR, err
147+
return HTTP_INTERNAL_SERVER_ERROR, err
149148
end
150149

151150
local results = res.body and res.body.ResultList
152151
if type(results) ~= "table" or core.table.isempty(results) then
153-
return INTERNAL_SERVER_ERROR, "failed to get moderation results from response"
152+
return HTTP_INTERNAL_SERVER_ERROR, "failed to get moderation results from response"
154153
end
155154

156155
for _, result in ipairs(results) do
@@ -160,14 +159,14 @@ function _M.rewrite(conf, ctx)
160159
goto continue
161160
end
162161
if item.Score > conf.moderation_categories[item.Name] then
163-
return BAD_REQUEST, "request body exceeds " .. item.Name .. " threshold"
162+
return HTTP_BAD_REQUEST, "request body exceeds " .. item.Name .. " threshold"
164163
end
165164
::continue::
166165
end
167166
end
168167

169168
if result.Toxicity > conf.toxicity_level then
170-
return BAD_REQUEST, "request body exceeds toxicity threshold"
169+
return HTTP_BAD_REQUEST, "request body exceeds toxicity threshold"
171170
end
172171
end
173172
end

0 commit comments

Comments
 (0)