Skip to content

Commit

Permalink
fix(ai-proxy): set content-length for non compressed response
Browse files Browse the repository at this point in the history
  • Loading branch information
fffonion committed Dec 2, 2024
1 parent fb99349 commit a850c26
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion kong/llm/drivers/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ _M.operation_map = {
}

_M.clear_response_headers = {
shared = {
shared = { -- deprecared, not using
"Content-Length",
},
openai = {
Expand Down
2 changes: 2 additions & 0 deletions kong/llm/plugin/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ function MetaPlugin:header_filter(sub_plugin, conf)
-- and seems nginx doesn't support it

elseif get_global_ctx("accept_gzip") then
-- for gzip response, don't set content-length at all to align with upstream
kong.response.clear_header("Content-Length")
kong.response.set_header("Content-Encoding", "gzip")
end

Expand Down
15 changes: 8 additions & 7 deletions kong/llm/plugin/shared-filters/normalize-json-response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ local cjson = require("cjson")

local ai_plugin_ctx = require("kong.llm.plugin.ctx")
local ai_plugin_o11y = require("kong.llm.plugin.observability")
local ai_shared = require("kong.llm.drivers.shared")

local _M = {
NAME = "normalize-json-response",
Expand Down Expand Up @@ -57,6 +56,8 @@ local function transform_body(conf)
end

set_global_ctx("response_body", response_body) -- to be sent out later or consumed by other plugins

return #response_body
end

function _M:run(conf)
Expand All @@ -81,8 +82,9 @@ function _M:run(conf)
-- if not streaming, prepare the response body buffer
-- this must be called before sending any response headers so that
-- we can modify status code if needed
local body_length
if not get_global_ctx("stream_mode") then
transform_body(conf)
body_length = transform_body(conf)
end

-- populate cost
Expand All @@ -94,12 +96,11 @@ function _M:run(conf)
ai_plugin_o11y.metrics_set("llm_usage_cost", 0)
end

-- clear shared restricted headers
for _, v in ipairs(ai_shared.clear_response_headers.shared) do
kong.response.clear_header(v)
if not get_global_ctx("accept_gzip") and not get_global_ctx("stream_mode") then
-- otherwise use our transformed body length
kong.response.set_header("Content-Length", body_length)
end


if ngx.var.http_kong_debug or conf.model_name_header then
local model_t = ai_plugin_ctx.get_request_model_table_inuse()
assert(model_t and model_t.name, "model name is missing")
Expand All @@ -109,4 +110,4 @@ function _M:run(conf)
return true
end

return _M
return _M

0 comments on commit a850c26

Please sign in to comment.