shreemaan-abhishek commented on code in PR #11541: URL: https://github.com/apache/apisix/pull/11541#discussion_r1770836225
########## apisix/plugins/ai-content-moderation.lua: ########## @@ -0,0 +1,176 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +local core = require("apisix.core") +local aws = require("resty.aws") +local http = require("resty.http") +local fetch_secrets = require("apisix.secret").fetch_secrets + +local aws_instance = aws() +local next = next +local pairs = pairs +local unpack = unpack +local type = type +local ipairs = ipairs +local require = require +local internal_server_error = ngx.HTTP_INTERNAL_SERVER_ERROR +local bad_request = ngx.HTTP_BAD_REQUEST + + +local aws_comprehend_schema = { + type = "object", + properties = { + access_key_id = { type = "string" }, + secret_access_key = { type = "string" }, + region = { type = "string" }, + endpoint = { + type = "string", + pattern = [[^https?://]] + }, + }, + required = { "access_key_id", "secret_access_key", "region", } +} + +local moderation_categories_pattern = "^(PROFANITY|HATE_SPEECH|INSULT|".. + "HARASSMENT_OR_ABUSE|SEXUAL|VIOLENCE_OR_THREAT)$" +local schema = { + type = "object", + properties = { + provider = { + type = "object", + properties = { + aws_comprehend = aws_comprehend_schema + }, + -- change to oneOf/enum while implementing support for other services + required = { "aws_comprehend" } + }, + moderation_categories = { + type = "object", + patternProperties = { + -- luacheck: push max code line length 300 + [moderation_categories_pattern] = { + -- luacheck: pop + type = "number", + minimum = 0, + maximum = 1 + } + }, + additionalProperties = false + }, + toxicity_level = { + type = "number", + minimum = 0, + maximum = 1, + default = 0.5 + }, + type = { + type = "string", + enum = { "openai" }, + } + }, + required = { "provider", "type" }, +} + + +local _M = { + version = 0.1, + priority = 1040, -- TODO: might change + name = "ai-content-moderation", + schema = schema, +} + + +function _M.check_schema(conf) + return core.schema.check(schema, conf) +end + +function _M.rewrite(conf, ctx) + conf = fetch_secrets(conf, true, conf, "") + if not conf then + return internal_server_error, "failed to retrieve secrets from conf" + end + + local body, err = core.request.get_json_request_body_table() + if not body then + return bad_request, err + end + + local msgs = body.messages + if type(msgs) ~= "table" or #msgs < 1 then + return bad_request, "messages not found in request body" + end + + local provider = conf.provider[next(conf.provider)] Review Comment: the schema should avoid this from happening. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
