]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
Remove "thinking" and fix errorlog
authorNameWeb <NameWeb@users.noreply.github.com>
Fri, 9 May 2025 08:29:05 +0000 (10:29 +0200)
committerGitHub <noreply@github.com>
Fri, 9 May 2025 08:29:05 +0000 (10:29 +0200)
Some models used by Ollama will include "thinking" before the actual response. We now remove this.

Also fixed error logging, which probably intended to also log "first_message".

src/plugins/lua/gpt.lua

index 5d1cf5e067eb41a81fa57fc188038299f419e67c..19c36435623462d0c1e6409ed400804714563b3c 100644 (file)
@@ -359,10 +359,19 @@ local function default_openai_plain_conversion(task, input)
     return spam_score, reason, categories
   end
 
-  rspamd_logger.errx(task, 'cannot parse plain gpt reply: %s (all: %s)', lines[1])
+  rspamd_logger.errx(task, 'cannot parse plain gpt reply: %s (all: %s)', lines[1], first_message)
   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)
@@ -387,6 +396,10 @@ local function default_ollama_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)
@@ -397,7 +410,7 @@ local function default_ollama_plain_conversion(task, input)
     return spam_score, reason, categories
   end
 
-  rspamd_logger.errx(task, 'cannot parse plain gpt reply: %s', lines[1])
+  rspamd_logger.errx(task, 'cannot parse plain gpt reply: %s (all: %s)', lines[1], first_message)
   return
 end