From 43bddef43b02713c550fcac6a1529f3305b06a43 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Tom=20Mittelst=C3=A4dt?= <1358-github@monster-box.de>
Date: Thu, 10 Jul 2025 10:31:18 +0200
Subject: [PATCH] clean gpt response for openai, too
---
src/plugins/lua/gpt.lua | 22 +++++++++++++---------
1 file changed, 13 insertions(+), 9 deletions(-)
diff --git a/src/plugins/lua/gpt.lua b/src/plugins/lua/gpt.lua
index 5776791a10..331dbbce22 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 ... and trim leading newlines
+local function clean_gpt_response(text)
+ -- Remove ... including multiline
+ text = text:gsub(".-", "")
+ -- 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 ... and trim leading newlines
-local function clean_gpt_response(text)
- -- Remove ... including multiline
- text = text:gsub(".-", "")
- -- 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)
--
2.47.3