Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: content-moderation plugin #11541

Open
wants to merge 22 commits into
base: master
Choose a base branch
from

Conversation

shreemaan-abhishek
Copy link
Contributor

@shreemaan-abhishek shreemaan-abhishek commented Aug 30, 2024

Description

The content-moderation plugin processes the request body to check for toxicity and rejects the request if it exceeds the configured threshold.

In later PRs, other plugins like ai-prompt-decorator and ai-prompt-template can use function from this plugin to ensure content moderation in requests proxying LLMs.

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@shreemaan-abhishek shreemaan-abhishek marked this pull request as ready for review August 30, 2024 16:32
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. doc Documentation things plugin labels Aug 30, 2024

if result.Toxicity > conf.toxicity_level then
return 400, "request body exceeds toxicity threshold"
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

response body also need filter by content moderation service.

@dosubot dosubot bot added size:XXL This PR changes 1000+ lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Sep 2, 2024
@shreemaan-abhishek shreemaan-abhishek marked this pull request as draft September 2, 2024 07:39
@shreemaan-abhishek shreemaan-abhishek marked this pull request as ready for review September 10, 2024 08:33
@@ -334,6 +335,26 @@ function _M.get_body(max_size, ctx)
end


function _M.get_body_table()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ai-proxy PR also has this code so later we can merge from master after ai-proxy is merged.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that the name of the method there changed :D

function _M.rewrite(conf, ctx)
conf = fetch_secrets(conf, true, conf, "")
if not conf then
return 500, "failed to retrieve secrets from conf"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use Ngx predefined constants?

end

local msgs = body.messages
if not msgs or type(msgs) ~= "table" or #msgs < 1 then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not msgs or type(msgs) ~= "table" or #msgs < 1 then
if type(msgs) ~= "table" or #msgs < 1 then

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#msgs < 1 -> core.table.isempty(msgs)


local body, err = core.request.get_body_table()
if not body then
return 400, err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto and the following the same

@@ -334,6 +335,26 @@ function _M.get_body(max_size, ctx)
end


function _M.get_body_table()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that the name of the method there changed :D

local type = type
local ipairs = ipairs
local require = require
local internal_server_error = ngx.HTTP_INTERNAL_SERVER_ERROR
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
local internal_server_error = ngx.HTTP_INTERNAL_SERVER_ERROR
local INTERNAL_SERVER_ERROR = ngx.HTTP_INTERNAL_SERVER_ERROR

const var. sugg. this style

return bad_request, "messages not found in request body"
end

local provider = conf.provider[next(conf.provider)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if conf.provider has multiple properties?

end

local results = res.body and res.body.ResultList
if not results or type(results) ~= "table" or #results < 1 then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not results or type(results) ~= "table" or #results < 1 then
if type(results) ~= "table" or core.table.isempty(results) then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Documentation things plugin size:XXL This PR changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants