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 5, 2024
1 parent 8ce1925 commit a231e49
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 12 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 @@ -323,7 +323,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
8 changes: 4 additions & 4 deletions kong/plugins/ai-request-transformer/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ local function create_http_opts(conf)
http_opts.proxy_opts = http_opts.proxy_opts or {}
http_opts.proxy_opts.https_proxy = fmt("http://%s:%d", conf.https_proxy_host, conf.https_proxy_port)
end

http_opts.http_timeout = conf.http_timeout
http_opts.https_verify = conf.https_verify

Expand All @@ -47,15 +47,15 @@ function _M:access(conf)
conf.llm.__plugin_id = conf.__plugin_id
conf.llm.__key__ = conf.__key__
local ai_driver, err = llm:new(conf.llm, http_opts)

if not ai_driver then
return internal_server_error(err)
end

-- if asked, introspect the request before proxying
kong.log.debug("introspecting request with LLM")
local new_request_body, err = llm: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 All @@ -64,7 +64,7 @@ function _M:access(conf)
if err then
return bad_request(err)
end

-- set the body for later plugins
kong.service.request.set_raw_body(new_request_body)

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 @@ -107,7 +107,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 a231e49

Please sign in to comment.