Skip to content

Commit

Permalink
fix(ai-prompt-decorator): changes from PR discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
tysoekong authored and locao committed Jan 23, 2024
1 parent 75acc1e commit 2d7c0ee
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
20 changes: 7 additions & 13 deletions kong/plugins/ai-prompt-decorator/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ _M.VERSION = kong_meta.version


local function bad_request(msg)
kong.log.warn(msg)
kong.log.debug(msg)
return kong.response.exit(400, { error = { message = msg } })
end

Expand Down Expand Up @@ -49,30 +49,24 @@ end

function _M:access(conf)
kong.service.request.enable_buffering()
kong.ctx.shared.prompt_decorated = true
kong.ctx.shared.ai_prompt_decorated = true -- future use

-- if plugin ordering was altered, receive the "decorated" request
local err
local request = kong.ctx.replacement_request
if not request then
request, err = kong.request.get_body("application/json")

if err then
return bad_request("ai-prompt-decorator only supports application/json requests")
end
local request, err = kong.request.get_body("application/json")
if err then
return bad_request("this LLM route only supports application/json requests")
end

if not request.messages or #request.messages < 1 then
return bad_request("ai-prompt-decorator only supports llm/chat type requests")
return bad_request("this LLM route only supports llm/chat type requests")
end

local decorated_request, err = self.execute(request, conf)
if err then
return bad_request(err)
end

-- later AI plugins looks for decorated request in thread contesxt
kong.ctx.shared.replacement_request = decorated_request
kong.service.request.set_body(decorated_request, "application/json")
end

return _M
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then
local body = assert.res_status(400, r)
local json = cjson.decode(body)

assert.same(json, { error = { message = "ai-prompt-decorator only supports llm/chat type requests" }})
assert.same(json, { error = { message = "this LLM route only supports llm/chat type requests" }})
end)

it("sends in an empty messages array", function()
Expand All @@ -105,7 +105,7 @@ for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then
local body = assert.res_status(400, r)
local json = cjson.decode(body)

assert.same(json, { error = { message = "ai-prompt-decorator only supports llm/chat type requests" }})
assert.same(json, { error = { message = "this LLM route only supports llm/chat type requests" }})
end)
end)

Expand Down

0 comments on commit 2d7c0ee

Please sign in to comment.