diff --git a/apisix/plugins/ai-content-moderation.lua b/apisix/plugins/ai-content-moderation.lua index af1a66d07363..00d7069e2c0b 100644 --- a/apisix/plugins/ai-content-moderation.lua +++ b/apisix/plugins/ai-content-moderation.lua @@ -68,12 +68,12 @@ local schema = { maximum = 1, default = 0.5 }, - reject_requests = { - type = "boolean", - default = true, + type = { + type = "string", + enum = { "openai" }, } }, - required = { "provider" }, + required = { "provider", "type" }, } @@ -127,12 +127,10 @@ function _M.rewrite(conf, ctx) port = port, }) - local text_segments = {} - for _, msg in ipairs(msgs) do - core.table.insert_tail(text_segments, { - Text = msg.content - }) - end + local ai_module = require("apisix.plugins.ai." .. conf.type) + local create_request_text_segments = ai_module.create_request_text_segments + + local text_segments = create_request_text_segments(msgs) local res, err = comprehend:detectToxicContent({ LanguageCode = "en", TextSegments = text_segments, diff --git a/apisix/plugins/ai/openai.lua b/apisix/plugins/ai/openai.lua new file mode 100644 index 000000000000..42683fe6a604 --- /dev/null +++ b/apisix/plugins/ai/openai.lua @@ -0,0 +1,32 @@ +-- +-- 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 _M = {} + + +function _M.create_request_text_segments(msgs) + local text_segments = {} + for _, msg in ipairs(msgs) do + core.table.insert_tail(text_segments, { + Text = msg.content + }) + end + return text_segments +end + +return _M diff --git a/t/plugin/ai-content-moderation-secrets.t b/t/plugin/ai-content-moderation-secrets.t index 11b0152f8104..6c27e2dc53be 100644 --- a/t/plugin/ai-content-moderation-secrets.t +++ b/t/plugin/ai-content-moderation-secrets.t @@ -122,7 +122,8 @@ Success! Data written to: kv/apisix/foo "region": "us-east-1", "endpoint": "http://localhost:2668" } - } + }, + "type": "openai" } }, "upstream": { @@ -176,7 +177,8 @@ POST /echo "region": "us-east-1", "endpoint": "http://localhost:2668" } - } + }, + "type": "openai" } }, "upstream": { diff --git a/t/plugin/ai-content-moderation.t b/t/plugin/ai-content-moderation.t index 15db1af5e53f..50772938cef9 100644 --- a/t/plugin/ai-content-moderation.t +++ b/t/plugin/ai-content-moderation.t @@ -109,7 +109,8 @@ __DATA__ "region": "us-east-1", "endpoint": "http://localhost:2668" } - } + }, + "type": "openai" } }, "upstream": { @@ -171,7 +172,8 @@ POST /echo }, "moderation_categories": { "PROFANITY": 0.5 - } + }, + "type": "openai" } }, "upstream": { @@ -243,7 +245,8 @@ POST /echo }, "moderation_categories": { "PROFANITY": 0.7 - } + }, + "type": "openai" } }, "upstream": {