From c8aca13cd459ec4c06e2ad5a6fd6c09c9d85ca81 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 28 Jun 2024 15:00:21 +0100 Subject: [PATCH] [Minor] Check words count properly --- src/plugins/lua/gpt.lua | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/plugins/lua/gpt.lua b/src/plugins/lua/gpt.lua index 7f4d55b17..dee83416d 100644 --- a/src/plugins/lua/gpt.lua +++ b/src/plugins/lua/gpt.lua @@ -133,16 +133,18 @@ local function default_condition(task) end if nwords > settings.max_tokens then - -- We need to truncate words + -- We need to truncate words (sometimes get_words_count returns a different number comparing to `get_words`) local words = sel_part:get_words('norm') - -- Trim something that does not fit - for i = nwords, settings.max_tokens, -1 do - rawset(words, i, nil) + nwords = #words + if nwords > settings.max_tokens then + -- Trim something that does not fit + for i = nwords, settings.max_tokens, -1 do + rawset(words, i, nil) + end + return true, table.concat(words, ' ') end - return true, table.concat(words, ' ') - else - return true, sel_part:get_content_oneline() end + return true, sel_part:get_content_oneline() end local function default_conversion(task, input) -- 2.39.5