Skip to content

Commit

Permalink
fix(llm): only send gzip header when response is from upstream (#13912)
Browse files Browse the repository at this point in the history
  • Loading branch information
fffonion authored and ProBrian committed Dec 13, 2024
1 parent f0fb1e9 commit e90e4d6
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions kong/llm/plugin/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,21 @@ function MetaPlugin:rewrite(sub_plugin, conf)
end

function MetaPlugin:header_filter(sub_plugin, conf)
-- we use openai's streaming mode (SSE)
if get_global_ctx("stream_mode") then
-- we are going to send plaintext event-stream frames for ALL models
kong.response.set_header("Content-Type", "text/event-stream")
-- TODO: disable gzip for SSE because it needs immediate flush for each chunk
-- and seems nginx doesn't support it
else

if get_global_ctx("accept_gzip") then
-- for error and exit response, just use plaintext headers
if kong.response.get_source() == "service" then
-- we use openai's streaming mode (SSE)
if get_global_ctx("stream_mode") then
-- we are going to send plaintext event-stream frames for ALL models
kong.response.set_header("Content-Type", "text/event-stream")
-- TODO: disable gzip for SSE because it needs immediate flush for each chunk
-- and seems nginx doesn't support it

elseif get_global_ctx("accept_gzip") then
kong.response.set_header("Content-Encoding", "gzip")
else
kong.response.clear_header("Content-Encoding")
end

else
kong.response.clear_header("Content-Encoding")
end

run_stage(STAGES.REQ_POST_PROCESSING, sub_plugin, conf)
Expand Down

0 comments on commit e90e4d6

Please sign in to comment.