Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

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.py
index 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 False
stop: list of stop strings
defaultrole: 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 False
kwargs: additional generation keyword arguments
Returns:
@@ -110,7 +110,7 @@ class Generation:
Args:
prompt: original input prompt
result: result text
- stripthink: removes thinking tags if true
+ stripthink: removes thinking text if true
Returns:
clean text
@@ -119,8 +119,11 @@ class Generation:
# Replace input prompt
text = 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 rules
return text.replace("$=", "<=").strip()
diff --git a/test/python/testpipeline/testllm/testllm.py b/test/python/testpipeline/testllm/testllm.py
index 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, AutoTokenizer
from txtai.pipeline import LLM, Generation
-# pylint: disable = C0411
+# pylint: disable=C0411
from 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