Skip to content

Commit

Permalink
repair gemini split logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tysoekong committed Jul 4, 2024
1 parent de01625 commit aaaee48
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions kong/llm/drivers/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,9 @@ function _M.frame_to_events(frame, raw_json_mode)
return
end

-- check if it's raw json and just return the split up data frame
-- Cohere / Other flat-JSON format parser
-- just return the split up data frame
if (not kong or not kong.ctx.plugin.truncated_frame) and string.sub(str_ltrim(frame), 1, 1) == "{" then
for event in frame:gmatch("[^\r\n]+") do
events[#events + 1] = {
data = event,
}
end

-- some new LLMs return the JSON object-by-object,
-- because that totally makes sense to parse?!
elseif raw_json_mode then
if raw_json_mode then
-- if this is the first frame, it will begin with array opener '['
frame = (string.sub(str_ltrim(frame), 1, 1) == "[" and string.sub(str_ltrim(frame), 2)) or frame

Expand All @@ -253,8 +243,18 @@ function _M.frame_to_events(frame, raw_json_mode)
events[#events+1] = { data = v }
end

-- check if it's raw json and just return the split up data frame
-- Cohere / Other flat-JSON format parser
-- just return the split up data frame
elseif (not kong or not kong.ctx.plugin.truncated_frame) and string.sub(str_ltrim(frame), 1, 1) == "{" then
for event in frame:gmatch("[^\r\n]+") do
events[#events + 1] = {
data = event,
}
end

-- standard SSE parser
else
-- standard SSE parser
local event_lines = split(frame, "\n")
local struct = { event = nil, id = nil, data = nil }

Expand Down

0 comments on commit aaaee48

Please sign in to comment.