Skip to content

Commit

Permalink
feat(plugins): enable ai plugins to read request body from buffered file
Browse files Browse the repository at this point in the history
  • Loading branch information
fffonion committed Jun 7, 2024
1 parent e801752 commit ff8aef7
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 9 deletions.
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/ai-plugin-read-file.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "allow AI plugin to read request from buffered file"
type: feature
scope: "Plugin"
2 changes: 1 addition & 1 deletion kong/plugins/ai-prompt-decorator/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function plugin:access(conf)
kong.ctx.shared.ai_prompt_decorated = true -- future use

-- if plugin ordering was altered, receive the "decorated" request
local request = kong.request.get_body("application/json")
local request = kong.request.get_body("application/json", nil, conf.max_request_body_size)
if type(request) ~= "table" then
return bad_request("this LLM route only supports application/json requests")
end
Expand Down
4 changes: 3 additions & 1 deletion kong/plugins/ai-prompt-decorator/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ return {
{ config = {
type = "record",
fields = {
{ prompts = prompts_record }
{ prompts = prompts_record },
{ max_request_body_size = { type = "integer", default = 8 * 1024, gt = 0,
description = "max allowed body size allowed to be introspected" } },
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/ai-prompt-guard/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function plugin:access(conf)
kong.ctx.shared.ai_prompt_guarded = true -- future use

-- if plugin ordering was altered, receive the "decorated" request
local request = kong.request.get_body("application/json")
local request = kong.request.get_body("application/json", nil, conf.max_request_body_size)
if type(request) ~= "table" then
return bad_request("this LLM route only supports application/json requests")
end
Expand Down
6 changes: 6 additions & 0 deletions kong/plugins/ai-prompt-guard/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ return {
type = "boolean",
required = true,
default = false } },
{ max_request_body_size = {
type = "integer",
default = 8 * 1024,
gt = 0,
description = "max allowed body size allowed to be introspected",}
},
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions kong/plugins/ai-prompt-template/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ function AIPromptTemplateHandler:access(conf)
kong.ctx.shared.ai_prompt_templated = true

if conf.log_original_request then
kong.log.set_serialize_value(LOG_ENTRY_KEYS.REQUEST_BODY, kong.request.get_raw_body())
kong.log.set_serialize_value(LOG_ENTRY_KEYS.REQUEST_BODY, kong.request.get_raw_body(conf.max_request_body_size))
end

local request = kong.request.get_body("application/json")
local request = kong.request.get_body("application/json", nil, conf.max_request_body_size)
if type(request) ~= "table" then
return bad_request("this LLM route only supports application/json requests")
end
Expand Down
6 changes: 6 additions & 0 deletions kong/plugins/ai-prompt-template/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ return {
required = true,
default = false,
}},
{ max_request_body_size = {
type = "integer",
default = 8 * 1024,
gt = 0,
description = "max allowed body size allowed to be introspected",
}},
}
}}
},
Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/ai-proxy/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ function _M:access(conf)
-- first, calculate the coordinates of the request
local content_type = kong.request.get_header("Content-Type") or "application/json"

request_table = kong.request.get_body(content_type)
request_table = kong.request.get_body(content_type, nil, conf.max_request_body_size)

if not request_table then
if not string.find(content_type, "multipart/form-data", nil, true) then
Expand Down
7 changes: 7 additions & 0 deletions kong/plugins/ai-proxy/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ local ai_proxy_only_config = {
default = "allow",
one_of = { "allow", "deny", "always" }},
},
{
max_request_body_size = {
type = "integer",
default = 8 * 1024,
gt = 0,
description = "max allowed body size allowed to be introspected",}
},
}

for i, v in pairs(ai_proxy_only_config) do
Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/ai-request-transformer/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function _M:access(conf)
-- if asked, introspect the request before proxying
kong.log.debug("introspecting request with LLM")
local new_request_body, err = ai_driver:ai_introspect_body(
kong.request.get_raw_body(),
kong.request.get_raw_body(conf.max_request_body_size),
conf.prompt,
http_opts,
conf.transformation_extract_pattern
Expand Down
10 changes: 9 additions & 1 deletion kong/plugins/ai-request-transformer/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ return {
default = true,
}},

{
max_request_body_size = {
type = "integer",
default = 8 * 1024,
gt = 0,
description = "max allowed body size allowed to be introspected",}
},

-- from forward-proxy
{ http_proxy_host = typedefs.host },
{ http_proxy_port = typedefs.port },
Expand All @@ -46,7 +54,7 @@ return {
{ llm = llm.config_schema },
},
}},

},
entity_checks = {
{
Expand Down
4 changes: 3 additions & 1 deletion kong/plugins/ai-response-transformer/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ function _M:access(conf)

kong.log.debug("intercepting plugin flow with one-shot request")
local httpc = http.new()
local res, err = subrequest(httpc, kong.request.get_raw_body(), http_opts)
local res, err = subrequest(httpc,
kong.request.get_raw_body(conf.max_request_body_size),
http_opts)
if err then
return internal_server_error(err)
end
Expand Down
7 changes: 7 additions & 0 deletions kong/plugins/ai-response-transformer/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ return {
default = true,
}},

{ max_request_body_size = {
type = "integer",
default = 8 * 1024,
gt = 0,
description = "max allowed body size allowed to be introspected",}
},

-- from forward-proxy
{ http_proxy_host = typedefs.host },
{ http_proxy_port = typedefs.port },
Expand Down

0 comments on commit ff8aef7

Please sign in to comment.