💡 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 pipeline to support template parameter, closes #566
davidmezzetti96acee4f6b7e
diff --git a/src/python/txtai/pipeline/text/llm.py b/src/python/txtai/pipeline/text/llm.pyindex 300375db..0f704b67 100644--- a/src/python/txtai/pipeline/text/llm.py+++ b/src/python/txtai/pipeline/text/llm.py@@ -3,6 +3,7 @@ LLM Module"""from ...models import Models+from ...util import TemplateFormatterfrom ..hfpipeline import HFPipeline@@ -13,12 +14,15 @@ class LLM(HFPipeline):sequence to sequence model."""- def __init__(self, path=None, quantize=False, gpu=True, model=None, task=None, **kwargs):+ def __init__(self, path=None, quantize=False, gpu=True, model=None, task=None, template=None, **kwargs):super().__init__(self.task(path, task, **kwargs), path if path else "google/flan-t5-base", quantize, gpu, model, **kwargs)# Load tokenizer, if necessaryself.pipeline.tokenizer = self.pipeline.tokenizer if self.pipeline.tokenizer else Models.tokenizer(path, **kwargs)+ # Template to apply to all inputs. Must include a {text} parameter.+ self.template = template+def __call__(self, text, prefix=None, maxlength=512, workers=0, **kwargs):"""Generates text using input text@@ -41,6 +45,11 @@ class LLM(HFPipeline):if prefix:texts = [f"{prefix}{x}" for x in texts]+ # Apply template, if necessary+ if self.template:+ formatter = TemplateFormatter()+ texts = [formatter.format(self.template, text=x) for x in texts]+# Run pipelineresults = self.pipeline(texts, max_length=maxlength, num_workers=workers, **kwargs)diff --git a/src/python/txtai/util/__init__.py b/src/python/txtai/util/__init__.pyindex 7912e008..ef78ad70 100644--- a/src/python/txtai/util/__init__.py+++ b/src/python/txtai/util/__init__.py@@ -3,3 +3,4 @@ Utility imports"""from .resolver import Resolver+from .template import TemplateFormatterdiff --git a/src/python/txtai/util/template.py b/src/python/txtai/util/template.pynew file mode 100644index 00000000..374c34c8--- /dev/null+++ b/src/python/txtai/util/template.py@@ -0,0 +1,16 @@+"""+Template module+"""++from string import Formatter+++class TemplateFormatter(Formatter):+ """+ Custom Formatter that requires each argument to be consumed.+ """++ def check_unused_args(self, used_args, args, kwargs):+ difference = set(kwargs).difference(used_args)+ if difference:+ raise KeyError(difference)diff --git a/src/python/txtai/workflow/task/template.py b/src/python/txtai/workflow/task/template.pyindex f1e073da..71a717f4 100644--- a/src/python/txtai/workflow/task/template.py+++ b/src/python/txtai/workflow/task/template.py@@ -4,6 +4,7 @@ Template modulefrom string import Formatter+from ...util import TemplateFormatterfrom .file import Task@@ -108,14 +109,3 @@ class ExtractorTask(TemplateTask):# Default mode is to use element text for both query and questionreturn {"query": element, "question": super().prepare(element)}---class TemplateFormatter(Formatter):- """- Helper class used to format template checks.- """-- def check_unused_args(self, used_args, args, kwargs):- difference = set(kwargs).difference(used_args)- if difference:- raise KeyError(difference)diff --git a/test/python/testpipeline/testllm.py b/test/python/testpipeline/testllm.pyindex e274a41f..add96b2d 100644--- a/test/python/testpipeline/testllm.py+++ b/test/python/testpipeline/testllm.py@@ -38,7 +38,7 @@ class TestLLM(unittest.TestCase):model = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2")tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2")- model = LLM((model, tokenizer))+ model = LLM((model, tokenizer), template="{text}")start = "Hello, how are"# Test that text is generated