Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Add parameter to strip thinking tags from LLM outputs, closes #920

davidmezzetti255164992a67
diff --git a/src/python/txtai/pipeline/llm/generation.py b/src/python/txtai/pipeline/llm/generation.py
index 9067f38c..0dae67fb 100644
--- a/src/python/txtai/pipeline/llm/generation.py
+++ b/src/python/txtai/pipeline/llm/generation.py
@@ -2,6 +2,8 @@
Generation module
"""
+import re
+
from ...util import TemplateFormatter
@@ -24,7 +26,7 @@ class Generation:
self.template = template
self.kwargs = kwargs
- def __call__(self, text, maxlength, stream, stop, defaultrole, **kwargs):
+ def __call__(self, text, maxlength, stream, stop, defaultrole, stripthink, **kwargs):
"""
Generates text. Supports the following input formats:
@@ -37,6 +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
kwargs: additional generation keyword arguments
Returns:
@@ -63,7 +66,7 @@ class Generation:
return results
# Clean generated text
- results = [self.clean(texts[x], result) for x, result in enumerate(results)]
+ results = [self.clean(texts[x], result, stripthink) for x, result in enumerate(results)]
# Extract results based on inputs
return results[0] if isinstance(text, str) or isinstance(text[0], dict) else results
@@ -100,13 +103,14 @@ class Generation:
# Full response as content elements
return list(self.stream(texts, maxlength, stream, stop, **kwargs))
- def clean(self, prompt, result):
+ def clean(self, prompt, result, stripthink):
"""
Applies a series of rules to clean generated text.
Args:
prompt: original input prompt
result: result text
+ stripthink: removes thinking tags if true
Returns:
clean text
@@ -115,6 +119,9 @@ 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
+
# Apply text cleaning rules
return text.replace("$=", "<=").strip()
diff --git a/src/python/txtai/pipeline/llm/llm.py b/src/python/txtai/pipeline/llm/llm.py
index a347bd4c..40728434 100644
--- a/src/python/txtai/pipeline/llm/llm.py
+++ b/src/python/txtai/pipeline/llm/llm.py
@@ -38,7 +38,7 @@ class LLM(Pipeline):
# Generation instance
self.generator = GenerationFactory.create(path, method, **kwargs)
- def __call__(self, text, maxlength=512, stream=False, stop=None, defaultrole="prompt", **kwargs):
+ def __call__(self, text, maxlength=512, stream=False, stop=None, defaultrole="prompt", stripthink=False, **kwargs):
"""
Generates text. Supports the following input formats:
@@ -51,6 +51,7 @@ class LLM(Pipeline):
stream: stream response if True, defaults to False
stop: list of stop strings, defaults to None
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
kwargs: additional generation keyword arguments
Returns:
@@ -61,7 +62,7 @@ class LLM(Pipeline):
logger.debug(text)
# Run LLM generation
- return self.generator(text, maxlength, stream, stop, defaultrole, **kwargs)
+ return self.generator(text, maxlength, stream, stop, defaultrole, stripthink, **kwargs)
def isvision(self):
"""