💡 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
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.pyindex 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 = templateself.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 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 Falsekwargs: additional generation keyword argumentsReturns:@@ -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 inputsreturn results[0] if isinstance(text, str) or isinstance(text[0], dict) else results@@ -100,13 +103,14 @@ class Generation:# Full response as content elementsreturn 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 promptresult: result text+ stripthink: removes thinking tags if trueReturns:clean text@@ -115,6 +119,9 @@ 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+# Apply text cleaning rulesreturn text.replace("$=", "<=").strip()diff --git a/src/python/txtai/pipeline/llm/llm.py b/src/python/txtai/pipeline/llm/llm.pyindex 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 instanceself.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 Falsestop: list of stop strings, defaults to Nonedefaultrole: default role to apply to text inputs (prompt for raw prompts (default) or user for user chat messages)+ stripthink: strip thinking tags, defaults to Falsekwargs: additional generation keyword argumentsReturns:@@ -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):"""