💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Update LLM stripthink parameter to support gpt-oss patterns, closes #966
davidmezzetti9a1a1e400c0e
diff --git a/src/python/txtai/pipeline/llm/generation.py b/src/python/txtai/pipeline/llm/generation.pyindex 0dae67fb..bb2b23b3 100644--- a/src/python/txtai/pipeline/llm/generation.py+++ b/src/python/txtai/pipeline/llm/generation.py@@ -39,7 +39,7 @@ class Generation:stream: stream response if True, defaults to Falsestop: list of stop stringsdefaultrole: default role to apply to text inputs (prompt for raw prompts (default) or user for user chat messages)- stripthink: strip thinking tags, defaults to False+ stripthink: strip thinking text, defaults to Falsekwargs: additional generation keyword argumentsReturns:@@ -110,7 +110,7 @@ class Generation:Args:prompt: original input promptresult: result text- stripthink: removes thinking tags if true+ stripthink: removes thinking text if trueReturns:clean text@@ -119,8 +119,11 @@ class Generation:# Replace input prompttext = result.replace(prompt, "") if isinstance(prompt, str) else result- # Replace thinking tags, if necessary- text = re.sub(r"(?s)<think>.+?</think>", "", text).strip() if stripthink else text+ # Replace thinking text, if necessary+ if stripthink:+ text = re.sub(r"(?s)<think>.+?</think>", "", text)+ text = text.split("<|channel|>final<|message|>", 1)+ text = text[1] if len(text) > 1 else text[0]# Apply text cleaning rulesreturn text.replace("$=", "<=").strip()diff --git a/test/python/testpipeline/testllm/testllm.py b/test/python/testpipeline/testllm/testllm.pyindex 564e85dd..7d220b9b 100644--- a/test/python/testpipeline/testllm/testllm.py+++ b/test/python/testpipeline/testllm/testllm.py@@ -10,7 +10,7 @@ from transformers import AutoModelForCausalLM, AutoTokenizerfrom txtai.pipeline import LLM, Generation-# pylint: disable = C0411+# pylint: disable=C0411from utils import Utils@@ -111,6 +111,25 @@ class TestLLM(unittest.TestCase):model = LLM("sshleifer/tiny-gpt2")self.assertIsInstance(" ".join(x for x in model("Hello, how are", stream=True)), str)+ def testStripThink(self):+ """+ Test stripthink parameter+ """++ # pylint: disable=W0613+ def execute1(*args, **kwargs):+ return ["<think>test</think>you"]++ def execute2(*args, **kwargs):+ return ["<|channel|>final<|message|> you"]++ model = LLM("hf-internal-testing/tiny-random-LlamaForCausalLM")++ for method in [execute1, execute2]:+ # Override execute method+ model.generator.execute = method+ self.assertEqual(model("Hello, how are", stripthink=True), "you")+def testVision(self):"""Test vision LLM