diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2024-12-10 15:09:07 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2024-12-10 15:09:07 +0000 |
commit | b0e0367901b4223f68a6de8edf1fede2c7eedcd0 (patch) | |
tree | 6886d55ab1a270546f7ff16a6b26397b07fed23b | |
parent | 1b78893987deb9913e0cd7e5534c592f4a3ad1a9 (diff) | |
download | rspamd-b0e0367901b4223f68a6de8edf1fede2c7eedcd0.tar.gz rspamd-b0e0367901b4223f68a6de8edf1fede2c7eedcd0.zip |
[Feature] GPT: Try harder to find JSON in NN reply
-rw-r--r-- | src/plugins/lua/gpt.lua | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/plugins/lua/gpt.lua b/src/plugins/lua/gpt.lua index 10cff0fb5..36938c0d1 100644 --- a/src/plugins/lua/gpt.lua +++ b/src/plugins/lua/gpt.lua @@ -153,6 +153,40 @@ local function default_condition(task) return true, sel_part:get_content_oneline() end +local function maybe_extract_json(str) + -- Find the first opening brace + local startPos = str:find("{") + if not startPos then + return nil + end + + local openBraces = 0 + local endPos = startPos + local len = #str + + -- Iterate through the string to find matching braces + for i = startPos, len do + local char = str:sub(i, i) + if char == "{" then + openBraces = openBraces + 1 + elseif char == "}" then + openBraces = openBraces - 1 + -- When we find the matching closing brace + if openBraces == 0 then + endPos = i + break + end + end + end + + -- If we found a complete JSON-like structure + if openBraces == 0 then + return str:sub(startPos, endPos) + end + + return nil +end + local function default_conversion(task, input) local parser = ucl.parser() local res, err = parser:parse_string(input) @@ -178,6 +212,9 @@ local function default_conversion(task, input) return end + -- Apply heuristic to extract JSON + first_message = maybe_extract_json(first_message) or first_message + parser = ucl.parser() res, err = parser:parse_string(first_message) if not res then |