janiussyafiq commented on code in PR #13606:
URL: https://github.com/apache/apisix/pull/13606#discussion_r3478712040
##########
apisix/plugins/ai-lakera-guard.lua:
##########
@@ -206,4 +215,60 @@ function _M.access(conf, ctx)
end
+function _M.lua_body_filter(conf, ctx, headers, body)
+ if conf.direction ~= "output" and conf.direction ~= "both" then
+ return
+ end
+
+ if ngx.status >= 400 then
+ return
+ end
+
+ -- Non-streaming: ai-proxy hands us the fully-assembled completion text.
+ if ctx.var.request_type == "ai_chat" then
+ local text = ctx.var.llm_response_text
+ if not text or text == "" then
+ return
+ end
+ local messages = { { role = "assistant", content = text } }
+ return moderate(ctx, conf, messages, "response",
conf.response_failure_message)
+ end
+
+ -- Streaming: lua_body_filter is invoked once per upstream chunk. We cannot
+ -- scan a partial completion and we must not let flagged tokens reach the
+ -- client, so we buffer every chunk (withholding it with an empty body) and
Review Comment:
agreed with the refactoring. fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]