-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat(plugins): ai-transformer plugins fix(ai-transformers): use correct http opts variables fix(spec): ai-transformer plugin tests fix(ai-transformer): PR comments * Update kong/plugins/ai-response-transformer/schema.lua Co-authored-by: Michael Martin <[email protected]> * fix(azure-llm): missing api_version query param * Update spec/03-plugins/38-ai-proxy/01-unit_spec.lua Co-authored-by: Michael Martin <[email protected]> --------- Co-authored-by: Michael Martin <[email protected]> (cherry picked from commit 3ef9235) Co-authored-by: Jack Tysoe <[email protected]>
- Loading branch information
1 parent
afae9d0
commit 31f4048
Showing
34 changed files
with
1,977 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
changelog/unreleased/kong/add-ai-request-transformer-plugin.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
message: Introduced the new **AI Request Transformer** plugin that enables passing mid-flight consumer requests to an LLM for transformation or sanitization. | ||
type: feature | ||
scope: Plugin |
3 changes: 3 additions & 0 deletions
3
changelog/unreleased/kong/add-ai-response-transformer-plugin.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
message: Introduced the new **AI Response Transformer** plugin that enables passing mid-flight upstream responses to an LLM for transformation or sanitization. | ||
type: feature | ||
scope: Plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
local _M = {} | ||
|
||
-- imports | ||
local kong_meta = require "kong.meta" | ||
local fmt = string.format | ||
local llm = require("kong.llm") | ||
-- | ||
|
||
_M.PRIORITY = 777 | ||
_M.VERSION = kong_meta.version | ||
|
||
local function bad_request(msg) | ||
kong.log.info(msg) | ||
return kong.response.exit(400, { error = { message = msg } }) | ||
end | ||
|
||
local function internal_server_error(msg) | ||
kong.log.err(msg) | ||
return kong.response.exit(500, { error = { message = msg } }) | ||
end | ||
|
||
local function create_http_opts(conf) | ||
local http_opts = {} | ||
|
||
if conf.http_proxy_host then -- port WILL be set via schema constraint | ||
http_opts.proxy_opts = http_opts.proxy_opts or {} | ||
http_opts.proxy_opts.http_proxy = fmt("http://%s:%d", conf.http_proxy_host, conf.http_proxy_port) | ||
end | ||
|
||
if conf.https_proxy_host then | ||
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 | ||
|
||
return http_opts | ||
end | ||
|
||
function _M:access(conf) | ||
kong.service.request.enable_buffering() | ||
kong.ctx.shared.skip_response_transformer = true | ||
|
||
-- first find the configured LLM interface and driver | ||
local http_opts = create_http_opts(conf) | ||
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(), | ||
conf.prompt, | ||
http_opts, | ||
conf.transformation_extract_pattern | ||
) | ||
|
||
if err then | ||
return bad_request(err) | ||
end | ||
|
||
-- set the body for later plugins | ||
kong.service.request.set_raw_body(new_request_body) | ||
|
||
-- continue into other plugins including ai-response-transformer, | ||
-- which may exit early with a sub-request | ||
end | ||
|
||
|
||
return _M |
Oops, something went wrong.