Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Extractor pipeline updates, closes #417

davidmezzetti6bbdeb0b259b
diff --git a/src/python/txtai/pipeline/text/extractor.py b/src/python/txtai/pipeline/text/extractor.py
index 6e542818..992de451 100644
--- a/src/python/txtai/pipeline/text/extractor.py
+++ b/src/python/txtai/pipeline/text/extractor.py
@@ -2,25 +2,34 @@
Extractor module
"""
+from transformers import AutoConfig
+
from ..base import Pipeline
from ..data import Tokenizer
+from .generator import Generator
from .questions import Questions
+from .sequences import Sequences
from .similarity import Similarity
class Extractor(Pipeline):
"""
- Class that uses an extractive question-answering model to extract content from a given text context.
+ Finds answers for a set of queries/questions. The extractor is a combination of a similarity instance (embeddings or similarity pipeline)
+ to build question context and a model that answers questions. The model can be either a prompt-driven large language model (LLM) or
+ an extractive qa model.
"""
- def __init__(self, similarity, path, quantize=False, gpu=True, model=None, tokenizer=None, minscore=None, mintokens=None, context=None):
+ # pylint: disable=R0913
+ def __init__(
+ self, similarity, path, quantize=False, gpu=True, model=None, tokenizer=None, minscore=None, mintokens=None, context=None, task=None
+ ):
"""
Builds a new extractor.
Args:
- similarity: similarity instance (embeddings or similarity instance)
- path: path to qa model
+ similarity: similarity instance (embeddings or similarity pipeline)
+ path: path to model, supports Questions, Generator, Sequences or custom pipeline
quantize: 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
@@ -28,16 +37,17 @@ class Extractor(Pipeline):
minscore: minimum score to include context match, defaults to None
mintokens: minimum number of tokens to include context match, defaults to None
context: topn context matches to include, defaults to 3
+ task: model task (language-generation, sequence-sequence or question-answering), defaults to auto-detect
"""
# Similarity instance
self.similarity = similarity
- # QA Pipeline
- self.pipeline = Questions(path, quantize, gpu, model)
+ # Question-Answer model. Can be prompt-driven LLM or extractive qa
+ self.model = self.load(path, quantize, gpu, model, task)
# Tokenizer class use default method if not set
- self.tokenizer = tokenizer if tokenizer else Tokenizer
+ self.tokenizer = tokenizer if tokenizer else Tokenizer() if hasattr(self.similarity, "scoring") and self.similarity.scoring else None
# Minimum score to include context match
self.minscore = minscore if minscore is not None else 0.0
@@ -45,25 +55,24 @@ class Extractor(Pipeline):
# Minimum number of tokens to include context match
self.mintokens = mintokens if mintokens is not None else 0.0
- # Top N context matches to include for question-answering
+ # Top n context matches to include for context
self.context = context if context else 3
- def __call__(self, queue, texts):
+ def __call__(self, queue, texts=None):
"""
- Extracts answers to input questions. This method runs queries against a list of text, finds the top n best matches
- and uses that as the question context. A question-answering model is then run against the context for the input question,
- with the answer returned.
+ Finds answers to input questions. This method runs queries to finds 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.
Args:
- queue: input queue (name, query, question, snippet)
- texts: list of text
+ queue: input question queue (name, query, question, snippet)
+ texts: optional list of text for context, otherwise runs embeddings search
Returns:
list of (name, answer)
"""
- # Execute embeddings query
- results = self.query([query for _, query, _, _ in queue], texts)
+ # Rank text by similarity for each query
+ results = self.rank([query for _, query, _, _ in queue], texts)
# Build question-context pairs
names, questions, contexts, topns, snippets = [], [], [], [], []
@@ -78,42 +87,64 @@ class Extractor(Pipeline):
topns.append([text for _, text, _ in topn])
snippets.append(snippet)
- # Run qa pipeline and return answers
+ # Run pipeline and return answers
return self.answers(names, questions, contexts, topns, snippets)
- def query(self, queries, texts):
+ def load(self, path, quantize, gpu, model, task):
"""
- Executes the extractor embeddings query. Returns results sorted by best match.
+ Loads a question-answer model.
Args:
- queries: list of embedding queries to run
- texts: list of text
+ path: path to model, loads a Questions, Generator or Sequences pipeline
+ quantize: 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
+ task: model task (language-generation, sequence-sequence or question-answering), defaults to auto-detect
Returns:
- list of (id, text, score)
+ Generator, Sequences, Questions or custom pipeline
"""
- if not queries:
- return []
+ # If path is not a string, return input
+ if not isinstance(path, str):
+ return path
- # Tokenize text
- segments, tokenlist = [], []
- for text in texts:
- tokens = self.tokenizer.tokenize(text)
- if tokens:
- segments.append(text)
- tokenlist.append(tokens)
+ # Autodetect task if not provided
+ if not task:
+ config = AutoConfig.from_pretrained(path)
+ architecture = config.architectures[0] if config.architectures else None
- # Add index id to segments to preserver ordering after filters
- segments = list(enumerate(segments))
+ if any(x for x in ["LMHead", "CausalLM"] if x in architecture):
+ task = "language-generation"
+ elif "ConditionalGeneration" in architecture:
+ task = "sequence-sequence"
- # Run batch queries for performance purposes
- if isinstance(self.similarity, Similarity):
- # Get list of (id, score) - sorted by highest score per query
- scores = self.similarity(queries, [t for _, t in segments])
- else:
- # Assume this is an embeddings instance, tokenize and run similarity queries
- scores = self.similarity.batchsimilarity([self.tokenizer.tokenize(x) for x in queries], tokenlist)
+ if task == "language-generation":
+ return Generator(path, quantize, gpu, model)
+ if task == "sequence-sequence":
+ return Sequences(path, quantize, gpu, model)
+
+ # Default to question-answering
+ return Questions(path, quantize, gpu, model)
+
+ def rank(self, queries, texts):
+ """
+ Rank texts against queries. If texts is not provided, an embeddings search will be executed.
+ Returns results sorted by best match.
+
+ Args:
+ queries: list of queries
+ texts: optional list of text
+
+ Returns:
+ list of (id, text, score) per query
+ """
+
+ if not queries:
+ return []
+
+ # Score text against queries
+ scores, segments, tokenlist = self.score(queries, texts)
# Build question-context pairs
results = []
@@ -122,11 +153,16 @@ class Extractor(Pipeline):
must = [token.strip("+") for token in query.split() if token.startswith("+") and len(token) > 1]
mnot = [token.strip("-") for token in query.split() if token.startswith("-") and len(token) > 1]
+ # Segment text is static when texts is passed in but different per query when an
+ # embeddings search is run
+ segment = segments[i] if isinstance(segments[i], list) else segments
+ tokens = tokenlist[i] if isinstance(segments[i], list) else tokenlist
+
# List of matches
matches = []
for x, score in scores[i]:
# Get segment text
- text = segments[x][1]
+ text = segment[x][1]
# Add result if:
# - all required tokens are present or there are not required tokens AND
@@ -136,17 +172,89 @@ class Extractor(Pipeline):
if (not must or all(token.lower() in text.lower() for token in must)) and (
not mnot or all(token.lower() not in text.lower() for token in mnot)
):
- if score >= self.minscore and len(tokenlist[x]) >= self.mintokens:
- matches.append(segments[x] + (score,))
+ if score >= self.minscore and len(tokens[x]) >= self.mintokens:
+ matches.append(segment[x] + (score,))
# Add query matches sorted by highest score
results.append(matches)
return results
+ def score(self, queries, texts):
+ """
+ Runs queries against texts (or an embeddings search if texts is empty) and builds list of
+ similarity scores for each query-text combination.
+
+ Args:
+ queries: list of queries
+ texts: optional list of text
+
+ Returns:
+ scores, segments, tokenlist
+ """
+
+ # Tokenize text
+ segments, tokenlist = [], []
+ if texts:
+ for text in texts:
+ # Run tokenizer method, if available, otherwise returns original text
+ tokens = self.tokenize(text)
+ if tokens:
+ segments.append(text)
+ tokenlist.append(tokens)
+
+ # Add index id to segments to preserve ordering after filters
+ segments = list(enumerate(segments))
+
+ # Get list of (id, score) - sorted by highest score per query
+ if isinstance(self.similarity, Similarity):
+ # Score using similarity pipeline
+ scores = self.similarity(queries, [t for _, t in segments])
+ elif texts:
+ # Score using embeddings.batchsimilarity
+ scores = self.similarity.batchsimilarity([self.tokenize(x) for x in queries], tokenlist)
+ else:
+ # Score using embeddings.batchsearch
+ scores, segments, tokenlist = self.batchsearch(queries)
+
+ return scores, segments, tokenlist
+
+ def batchsearch(self, queries):
+ """
+ Runs a batch embeddings search for a set of queries.
+
+ Args:
+ queries: list of queries to run
+
+ Returns:
+ scores, segments, tokenlist
+ """
+
+ scores, segments, tokenlist = [], [], []
+ for results in self.similarity.batchsearch([self.tokenize(x) for x in queries], self.context):
+ # Assume embeddings content is enabled and results are dictionaries
+ scores.append([(x, result["score"]) for x, result in enumerate(results)])
+ segments.append([(x, result["text"]) for x, result in enumerate(results)])
+ tokenlist.append([self.tokenize(result["text"]) for result in results])
+
+ return scores, segments, tokenlist
+
+ def tokenize(self, text):
+ """
+ Tokenizes text. Returns original text if tokenizer is not available.
+
+ Args:
+ text: input text
+
+ Returns:
+ tokens if tokenizer available otherwise original text
+ """
+
+ return self.tokenizer(text) if self.tokenizer else text
+
def answers(self, names, questions, contexts, topns, snippets):
"""
- Executes QA pipeline and formats extracted answers.
+ Executes pipeline and formats extracted answers.
Args:
names: question identifiers/names
@@ -161,8 +269,13 @@ class Extractor(Pipeline):
results = []
- # Run qa pipeline
- answers = self.pipeline(questions, contexts)
+ # Run model inference for question-context pairs
+ if isinstance(self.model, Questions):
+ # Questions pipeline takes questions and contexts separately
+ 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)])
# Extract and format answer
for x, answer in enumerate(answers):
diff --git a/test/python/testpipeline/testextractor.py b/test/python/testpipeline/testextractor.py
index 01cf5c6c..1bf6aea7 100644
--- a/test/python/testpipeline/testextractor.py
+++ b/test/python/testpipeline/testextractor.py
@@ -6,7 +6,7 @@ import platform
import unittest
from txtai.embeddings import Embeddings
-from txtai.pipeline import Extractor, Similarity
+from txtai.pipeline import Extractor, Questions, Similarity
class TestExtractor(unittest.TestCase):
@@ -74,7 +74,19 @@ class TestExtractor(unittest.TestCase):
Tests an empty extractor queries list
"""
- self.assertEqual(self.extractor.query(None, None), [])
+ self.assertEqual(self.extractor.rank(None, None), [])
+
+ def testGeneration(self):
+ """
+ Tests support for generator models
+ """
+
+ extractor = Extractor(self.embeddings, "facebook/opt-125m")
+
+ question = "How many home runs?"
+
+ answers = extractor([(question, question, question, True)], self.data)
+ self.assertIsNotNone(answers)
def testNoAnswer(self):
"""
@@ -103,13 +115,45 @@ class TestExtractor(unittest.TestCase):
answers = extractor([(question, question, question, True)], self.data)
self.assertTrue(answers[0][1].startswith("Giants hit 3 HRs"))
+ def testSearch(self):
+ """
+ Test qa extraction with an embeddings search for context
+ """
+
+ embeddings = Embeddings({"path": "sentence-transformers/nli-mpnet-base-v2", "content": True})
+ embeddings.index([(uid, text, None) for uid, text in enumerate(self.data)])
+
+ extractor = Extractor(embeddings, "distilbert-base-cased-distilled-squad")
+
+ question = "How many home runs?"
+
+ answers = extractor([(question, question, question, True)])
+ self.assertTrue(answers[0][1].startswith("Giants hit 3 HRs"))
+
+ def testSequences(self):
+ """
+ Test extraction with prompts and a Seq2Seq model
+ """
+
+ extractor = Extractor(self.embeddings, "google/flan-t5-small")
+
+ # Prompt template, context appended by extractor
+ prompt = """
+ Answer the following question and return a number.
+ Question: How many HRs?
+ Context:
+ """
+
+ answers = extractor([("prompt", prompt, prompt, True)], self.data)
+ self.assertEqual(answers[0][1], "3")
+
def testSimilarity(self):
"""
Test qa extraction using a Similarity pipeline to build context
"""
# Create extractor instance
- extractor = Extractor(Similarity("prajjwal1/bert-medium-mnli"), "distilbert-base-cased-distilled-squad")
+ extractor = Extractor(Similarity("prajjwal1/bert-medium-mnli"), Questions("distilbert-base-cased-distilled-squad"))
question = "How many home runs?"
@@ -132,3 +176,15 @@ class TestExtractor(unittest.TestCase):
"""
self.assertEqual(self.extractor.snippet(None, None), None)
+
+ def testTasks(self):
+ """
+ Tests loading models with task parameter
+ """
+
+ for task, model in [
+ ("language-generation", "hf-internal-testing/tiny-random-gpt2"),
+ ("sequence-sequence", "hf-internal-testing/tiny-random-t5"),
+ ]:
+ extractor = Extractor(self.embeddings, model, task=task)
+ self.assertIsNotNone(extractor)