aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@rspamd.com>2025-07-12 19:21:43 +0600
committerGitHub <noreply@github.com>2025-07-12 19:21:43 +0600
commit45e0b9f8a2cabb7689f602092091b4b14728ca22 (patch)
tree2f320b25f237a7dd5d32fa71392b645e8f710965
parentc38d2f3a1f2d5153e608f7bcef75f61b97d3e518 (diff)
parent43bddef43b02713c550fcac6a1529f3305b06a43 (diff)
downloadrspamd-master.tar.gz
rspamd-master.zip
Merge pull request #5540 from 1358/clean-gpt-response-for-openaiHEADmaster
Cleanup GPT response for openai-provider, too (<think>)
-rw-r--r--src/plugins/lua/gpt.lua22
1 files changed, 13 insertions, 9 deletions
diff --git a/src/plugins/lua/gpt.lua b/src/plugins/lua/gpt.lua
index 5776791a1..331dbbce2 100644
--- a/src/plugins/lua/gpt.lua
+++ b/src/plugins/lua/gpt.lua
@@ -253,6 +253,15 @@ local function maybe_extract_json(str)
return nil
end
+-- Helper function to remove <think>...</think> and trim leading newlines
+local function clean_gpt_response(text)
+ -- Remove <think>...</think> including multiline
+ text = text:gsub("<think>.-</think>", "")
+ -- Trim leading whitespace and newlines
+ text = text:gsub("^%s*\n*", "")
+ return text
+end
+
local function default_openai_json_conversion(task, input)
local parser = ucl.parser()
local res, err = parser:parse_string(input)
@@ -349,6 +358,10 @@ local function default_openai_plain_conversion(task, input)
rspamd_logger.errx(task, 'no content in the first message')
return
end
+
+ -- Clean message
+ first_message = clean_gpt_response(first_message)
+
local lines = lua_util.str_split(first_message, '\n')
local first_line = clean_reply_line(lines[1])
local spam_score = tonumber(first_line)
@@ -367,15 +380,6 @@ local function default_openai_plain_conversion(task, input)
return
end
--- Helper function to remove <think>...</think> and trim leading newlines
-local function clean_gpt_response(text)
- -- Remove <think>...</think> including multiline
- text = text:gsub("<think>.-</think>", "")
- -- Trim leading whitespace and newlines
- text = text:gsub("^%s*\n*", "")
- return text
-end
-
local function default_ollama_plain_conversion(task, input)
local parser = ucl.parser()
local res, err = parser:parse_string(input)