|
| 1 | +-- |
| 2 | +-- Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +-- contributor license agreements. See the NOTICE file distributed with |
| 4 | +-- this work for additional information regarding copyright ownership. |
| 5 | +-- The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +-- (the "License"); you may not use this file except in compliance with |
| 7 | +-- the License. You may obtain a copy of the License at |
| 8 | +-- |
| 9 | +-- http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +-- |
| 11 | +-- Unless required by applicable law or agreed to in writing, software |
| 12 | +-- distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +-- See the License for the specific language governing permissions and |
| 15 | +-- limitations under the License. |
| 16 | +-- |
| 17 | +local core = require("apisix.core") |
| 18 | +local aws_instance = require("resty.aws")() |
| 19 | +local http = require("resty.http") |
| 20 | +local fetch_secrets = require("apisix.secret").fetch_secrets |
| 21 | + |
| 22 | +local next = next |
| 23 | +local pairs = pairs |
| 24 | +local unpack = unpack |
| 25 | +local type = type |
| 26 | +local ipairs = ipairs |
| 27 | +local require = require |
| 28 | +local HTTP_INTERNAL_SERVER_ERROR = ngx.HTTP_INTERNAL_SERVER_ERROR |
| 29 | +local HTTP_BAD_REQUEST = ngx.HTTP_BAD_REQUEST |
| 30 | + |
| 31 | + |
| 32 | +local aws_comprehend_schema = { |
| 33 | + type = "object", |
| 34 | + properties = { |
| 35 | + access_key_id = { type = "string" }, |
| 36 | + secret_access_key = { type = "string" }, |
| 37 | + region = { type = "string" }, |
| 38 | + endpoint = { |
| 39 | + type = "string", |
| 40 | + pattern = [[^https?://]] |
| 41 | + }, |
| 42 | + ssl_verify = { |
| 43 | + type = "boolean", |
| 44 | + default = true |
| 45 | + } |
| 46 | + }, |
| 47 | + required = { "access_key_id", "secret_access_key", "region", } |
| 48 | +} |
| 49 | + |
| 50 | +local moderation_categories_pattern = "^(PROFANITY|HATE_SPEECH|INSULT|".. |
| 51 | + "HARASSMENT_OR_ABUSE|SEXUAL|VIOLENCE_OR_THREAT)$" |
| 52 | +local schema = { |
| 53 | + type = "object", |
| 54 | + properties = { |
| 55 | + provider = { |
| 56 | + type = "object", |
| 57 | + properties = { |
| 58 | + aws_comprehend = aws_comprehend_schema |
| 59 | + }, |
| 60 | + maxProperties = 1, |
| 61 | + -- ensure only one provider can be configured while implementing support for |
| 62 | + -- other providers |
| 63 | + required = { "aws_comprehend" } |
| 64 | + }, |
| 65 | + moderation_categories = { |
| 66 | + type = "object", |
| 67 | + patternProperties = { |
| 68 | + [moderation_categories_pattern] = { |
| 69 | + type = "number", |
| 70 | + minimum = 0, |
| 71 | + maximum = 1 |
| 72 | + } |
| 73 | + }, |
| 74 | + additionalProperties = false |
| 75 | + }, |
| 76 | + moderation_threshold = { |
| 77 | + type = "number", |
| 78 | + minimum = 0, |
| 79 | + maximum = 1, |
| 80 | + default = 0.5 |
| 81 | + }, |
| 82 | + llm_provider = { |
| 83 | + type = "string", |
| 84 | + enum = { "openai" }, |
| 85 | + } |
| 86 | + }, |
| 87 | + required = { "provider", "llm_provider" }, |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +local _M = { |
| 92 | + version = 0.1, |
| 93 | + priority = 1040, -- TODO: might change |
| 94 | + name = "ai-content-moderation", |
| 95 | + schema = schema, |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +function _M.check_schema(conf) |
| 100 | + return core.schema.check(schema, conf) |
| 101 | +end |
| 102 | + |
| 103 | + |
| 104 | +function _M.rewrite(conf, ctx) |
| 105 | + conf = fetch_secrets(conf, true, conf, "") |
| 106 | + if not conf then |
| 107 | + return HTTP_INTERNAL_SERVER_ERROR, "failed to retrieve secrets from conf" |
| 108 | + end |
| 109 | + |
| 110 | + local body, err = core.request.get_json_request_body_table() |
| 111 | + if not body then |
| 112 | + return HTTP_BAD_REQUEST, err |
| 113 | + end |
| 114 | + |
| 115 | + local msgs = body.messages |
| 116 | + if type(msgs) ~= "table" or #msgs < 1 then |
| 117 | + return HTTP_BAD_REQUEST, "messages not found in request body" |
| 118 | + end |
| 119 | + |
| 120 | + local provider = conf.provider[next(conf.provider)] |
| 121 | + |
| 122 | + local credentials = aws_instance:Credentials({ |
| 123 | + accessKeyId = provider.access_key_id, |
| 124 | + secretAccessKey = provider.secret_access_key, |
| 125 | + sessionToken = provider.session_token, |
| 126 | + }) |
| 127 | + |
| 128 | + local default_endpoint = "https://comprehend." .. provider.region .. ".amazonaws.com" |
| 129 | + local scheme, host, port = unpack(http:parse_uri(provider.endpoint or default_endpoint)) |
| 130 | + local endpoint = scheme .. "://" .. host |
| 131 | + aws_instance.config.endpoint = endpoint |
| 132 | + aws_instance.config.ssl_verify = provider.ssl_verify |
| 133 | + |
| 134 | + local comprehend = aws_instance:Comprehend({ |
| 135 | + credentials = credentials, |
| 136 | + endpoint = endpoint, |
| 137 | + region = provider.region, |
| 138 | + port = port, |
| 139 | + }) |
| 140 | + |
| 141 | + local ai_module = require("apisix.plugins.ai." .. conf.llm_provider) |
| 142 | + local create_request_text_segments = ai_module.create_request_text_segments |
| 143 | + |
| 144 | + local text_segments = create_request_text_segments(msgs) |
| 145 | + local res, err = comprehend:detectToxicContent({ |
| 146 | + LanguageCode = "en", |
| 147 | + TextSegments = text_segments, |
| 148 | + }) |
| 149 | + |
| 150 | + if not res then |
| 151 | + core.log.error("failed to send request to ", provider, ": ", err) |
| 152 | + return HTTP_INTERNAL_SERVER_ERROR, err |
| 153 | + end |
| 154 | + |
| 155 | + local results = res.body and res.body.ResultList |
| 156 | + if type(results) ~= "table" or core.table.isempty(results) then |
| 157 | + return HTTP_INTERNAL_SERVER_ERROR, "failed to get moderation results from response" |
| 158 | + end |
| 159 | + |
| 160 | + for _, result in ipairs(results) do |
| 161 | + if conf.moderation_categories then |
| 162 | + for _, item in pairs(result.Labels) do |
| 163 | + if not conf.moderation_categories[item.Name] then |
| 164 | + goto continue |
| 165 | + end |
| 166 | + if item.Score > conf.moderation_categories[item.Name] then |
| 167 | + return HTTP_BAD_REQUEST, "request body exceeds " .. item.Name .. " threshold" |
| 168 | + end |
| 169 | + ::continue:: |
| 170 | + end |
| 171 | + end |
| 172 | + |
| 173 | + if result.Toxicity > conf.moderation_threshold then |
| 174 | + return HTTP_BAD_REQUEST, "request body exceeds toxicity threshold" |
| 175 | + end |
| 176 | + end |
| 177 | +end |
| 178 | + |
| 179 | +return _M |
0 commit comments