]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
clean gpt response for openai, too 5540/head
authorTom Mittelstädt <1358-github@monster-box.de>
Thu, 10 Jul 2025 08:31:18 +0000 (10:31 +0200)
committerTom Mittelstädt <tm@langer-it-solutions.com>
Thu, 10 Jul 2025 11:31:27 +0000 (13:31 +0200)
src/plugins/lua/gpt.lua

index 5776791a101b47bb606e05253e17b0faf64f4abf..331dbbce221f8b34eb8c4a6766ad62f4fe594227 100644 (file)
@@ -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)