Skip to content

Commit

Permalink
modularise on basis of provider
Browse files Browse the repository at this point in the history
  • Loading branch information
shreemaan-abhishek committed Sep 2, 2024
1 parent ee34e37 commit 12529f0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
18 changes: 8 additions & 10 deletions apisix/plugins/ai-content-moderation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
}


Expand Down Expand Up @@ -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,
Expand Down
32 changes: 32 additions & 0 deletions apisix/plugins/ai/openai.lua
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions t/plugin/ai-content-moderation-secrets.t
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ Success! Data written to: kv/apisix/foo
"region": "us-east-1",
"endpoint": "http://localhost:2668"
}
}
},
"type": "openai"
}
},
"upstream": {
Expand Down Expand Up @@ -176,7 +177,8 @@ POST /echo
"region": "us-east-1",
"endpoint": "http://localhost:2668"
}
}
},
"type": "openai"
}
},
"upstream": {
Expand Down
9 changes: 6 additions & 3 deletions t/plugin/ai-content-moderation.t
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ __DATA__
"region": "us-east-1",
"endpoint": "http://localhost:2668"
}
}
},
"type": "openai"
}
},
"upstream": {
Expand Down Expand Up @@ -171,7 +172,8 @@ POST /echo
},
"moderation_categories": {
"PROFANITY": 0.5
}
},
"type": "openai"
}
},
"upstream": {
Expand Down Expand Up @@ -243,7 +245,8 @@ POST /echo
},
"moderation_categories": {
"PROFANITY": 0.7
}
},
"type": "openai"
}
},
"upstream": {
Expand Down

0 comments on commit 12529f0

Please sign in to comment.