feat: add ai-prompt-guard plugin#12008
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.
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.
the pattern is not a plain text, it is a regex
the current way is wrong
|
@membphis Made changes |
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.
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.
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
membphis
left a comment
There was a problem hiding this comment.
Rerun CI, some of them failed
| "plugins": { | ||
| "ai-prompt-guard": { | ||
| "match_all_roles": true, | ||
| "allow_patterns": [ |


Description
The
ai-prompt-guardplugin 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