-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat: add ai-prompt-guard plugin #12008
base: master
Are you sure you want to change the base?
Conversation
apisix/plugins/ai-prompt-guard.lua
Outdated
} | ||
|
||
function _M.check_schema(conf) | ||
return core.schema.check(schema, conf) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to confirm the items in allow_patterns[]
and deny_patterns[]
, they should be a valid regex
you can call this function: https://github.com/openresty/lua-resty-core/blob/master/lib/resty/core/regex.lua#L639
apisix/plugins/ai-prompt-guard.lua
Outdated
if #conf.allow_patterns > 0 then | ||
local any_allowed = false | ||
for _, pattern in ipairs(conf.allow_patterns) do | ||
if ngx.re.find(content_to_check, pattern, "jou") then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the pattern
is not a plain text, it is a regex
the current way is wrong
@membphis Made changes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
apisix/plugins/ai-prompt-guard.lua
Outdated
if not conf.match_all_roles and #messages > 0 and messages[#messages].role ~= "user" then | ||
return | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't understand this check? should we check all messages that's role is user when match_all_roles
is false? why only check last message in array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Combined with the match_all_conversation_history
configuration, we should first obtain the list of messages to be judged based on match_all_conversation_history
, and then decide whether to perform pattern judgment according to each message's role and match_all_roles
.
ac03b08
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rerun CI, some of them failed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
"plugins": { | ||
"ai-prompt-guard": { | ||
"match_all_roles": true, | ||
"allow_patterns": [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bad indentation too
Description
The
ai-prompt-guard
plugin safeguards your AI endpoints by inspecting and validating incoming prompt messages. It checks the content of requests against user-defined allowed and denied patterns to ensure that only approved inputs are processed. Based on its configuration, the plugin can either examine just the latest message or the entire conversation history, and it can be set to check prompts from all roles or only from end users.Checklist