💡 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
Extractor pipeline improvements, closes #577
davidmezzetti4447685ef933
diff --git a/src/python/txtai/pipeline/text/extractor.py b/src/python/txtai/pipeline/text/extractor.pyindex 63c07744..f3089623 100644--- a/src/python/txtai/pipeline/text/extractor.py+++ b/src/python/txtai/pipeline/text/extractor.py@@ -41,7 +41,7 @@ class Extractor(Pipeline):Args:similarity: similarity instance (embeddings or similarity pipeline)- path: path to model, supports Questions, Generator, Sequences or custom pipeline+ path: path to model, supports a LLM, Questions or custom pipelinequantize: True if model should be quantized before inference, False otherwise.gpu: if gpu inference should be used (only works if GPUs are available)model: optional existing pipeline model to wrap@@ -57,11 +57,11 @@ class Extractor(Pipeline):# Similarity instanceself.similarity = similarity- # Question-Answer model. Can be prompt-driven LLM or extractive qa+ # Model can be a LLM, Questions or custom pipelineself.model = self.load(path, quantize, gpu, model, task, **kwargs)# Tokenizer class use default method if not set- self.tokenizer = tokenizer if tokenizer else Tokenizer() if hasattr(self.similarity, "scoring") and self.similarity.scoring else None+ self.tokenizer = tokenizer if tokenizer else Tokenizer() if hasattr(self.similarity, "scoring") and self.similarity.isweighted() else None# Minimum score to include context matchself.minscore = minscore if minscore is not None else 0.0@@ -75,7 +75,7 @@ class Extractor(Pipeline):# Output formatself.output = output- def __call__(self, queue, texts=None):+ def __call__(self, queue, texts=None, **kwargs):"""Finds answers to input questions. This method runs queries to find the top n best matches and uses that as the context.A model is then run against the context for each input question, with the answer returned.@@ -83,6 +83,7 @@ class Extractor(Pipeline):Args:queue: input question queue (name, query, question, snippet), can be list of tuples or dictstexts: optional list of text for context, otherwise runs embeddings search+ kwargs: additional keyword arguments to pass to pipeline modelReturns:list of answers matching input format (tuple or dict) containing fields as specified by output format@@ -116,17 +117,17 @@ class Extractor(Pipeline):snippets.append(snippet)# Run pipeline and return answers- answers = self.answers(names, questions, contexts, [[text for _, text, _ in topn] for topn in topns], snippets)+ answers = self.answers(names, questions, contexts, [[text for _, text, _ in topn] for topn in topns], snippets, **kwargs)# Apply output formatting to answers and returnreturn self.apply(inputs, queries, answers, topns)def load(self, path, quantize, gpu, model, task, **kwargs):"""- Loads a question-answer model.+ Loads a LLM, Questions or custom pipeline.Args:- path: path to model, loads a Questions, Generator or Sequences pipeline+ path: path to model, supports a LLM, Questions or custom pipelinequantize: True if model should be quantized before inference, False otherwise.gpu: if gpu inference should be used (only works if GPUs are available)model: optional existing pipeline model to wrap@@ -134,7 +135,7 @@ class Extractor(Pipeline):kwargs: additional keyword arguments to pass to pipeline modelReturns:- Generator, Sequences, Questions or custom pipeline+ LLM, Questions or custom pipeline"""# Only try to load if path is a string@@ -144,11 +145,11 @@ class Extractor(Pipeline):# Attempt to resolve task if not providedtask = task if task else Models.task(path, **kwargs)- # Load model as Question pipeline+ # Load Questions pipelineif task == "question-answering":return Questions(path, quantize, gpu, model, **kwargs)- # Load model as LLM pipeline+ # Load LLM pipelinereturn LLM(path, quantize, gpu, model, task, **kwargs)def query(self, queries, texts):@@ -279,7 +280,7 @@ class Extractor(Pipeline):return self.tokenizer(text) if self.tokenizer else text- def answers(self, names, questions, contexts, topns, snippets):+ def answers(self, names, questions, contexts, topns, snippets, **kwargs):"""Executes pipeline and formats extracted answers.@@ -289,6 +290,7 @@ class Extractor(Pipeline):contexts: question contexttopns: same as question context but as a list with each candidate elementsnippets: flags to enable answer snippets per answer+ kwargs: additional keyword arguments to pass to pipeline modelReturns:list of (name, answer)@@ -302,7 +304,7 @@ class Extractor(Pipeline):answers = self.model(questions, contexts)else:# Combine question and context into single text field for generative pipelines- answers = self.model([f"{questions[x]} {context}" for x, context in enumerate(contexts)])+ answers = self.model([f"{questions[x]} {context}" for x, context in enumerate(contexts)], **kwargs)# Extract and format answerfor x, answer in enumerate(answers):