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 improvements, closes #443

davidmezzetti210f2f3f17b1
diff --git a/src/python/txtai/app/base.py b/src/python/txtai/app/base.py
index 47eab9f8..3e51d747 100644
--- a/src/python/txtai/app/base.py
+++ b/src/python/txtai/app/base.py
@@ -112,8 +112,9 @@ class Application:
config["application"] = self
# Custom pipeline parameters
- if pipeline == "extractor":
- config["similarity"] = self.embeddings
+ if pipeline == "extractor" and "similarity" not in config:
+ # Add placeholder, will be set to embeddings index once initialized
+ config["similarity"] = None
elif pipeline == "similarity" and "path" not in config and "labels" in self.pipelines:
config["model"] = self.pipelines["labels"]
@@ -198,6 +199,11 @@ class Application:
# Initialize empty embeddings
self.embeddings = Embeddings(config)
+ # If an extractor pipeline is defined and the similarity attribute is None, set to embeddings index
+ extractor = self.pipelines.get("extractor")
+ if extractor and not extractor.similarity:
+ extractor.similarity = self.embeddings
+
def resolve(self, task):
"""
Resolves callable functions for a task.
@@ -574,14 +580,11 @@ class Application:
"""
if self.embeddings and "extractor" in self.pipelines:
- # Set similarity function
+ # Get extractor instance
extractor = self.pipelines["extractor"]
- if not extractor.similarity:
- extractor.similarity = self.embeddings
- # Convert queue to tuples
- queue = [(x["name"], x["query"], x.get("question"), x.get("snippet")) for x in queue]
- return [{"name": name, "answer": answer} for name, answer in extractor(queue, texts)]
+ # Run extractor and return results as dicts
+ return [{"name": name, "answer": answer} for name, answer in extractor(queue, texts, False)]
return None
diff --git a/src/python/txtai/pipeline/text/extractor.py b/src/python/txtai/pipeline/text/extractor.py
index e1bb12b9..d03dd606 100644
--- a/src/python/txtai/pipeline/text/extractor.py
+++ b/src/python/txtai/pipeline/text/extractor.py
@@ -58,19 +58,30 @@ class Extractor(Pipeline):
# Top n context matches to include for context
self.context = context if context else 3
- def __call__(self, queue, texts=None):
+ def __call__(self, queue, texts=None, flatten=None):
"""
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 question queue (name, query, question, snippet)
+ queue: input question queue (name, query, question, snippet), can be list of tuples or dicts
texts: optional list of text for context, otherwise runs embeddings search
+ flatten: flatten output to a list of answers, defaults to None
Returns:
- list of (name, answer)
+ list of (name, answer) or a list of answers depending on flatten
"""
+ # Convert dictionary inputs to tuples
+ if isinstance(queue[0], dict):
+ # Default flatten to True if name not provided, False otherwise
+ flatten = flatten if flatten is not None else "name" not in queue[0]
+
+ # Convert dict to tuple
+ queue = [tuple(row.get(x) for x in ["name", "query", "question", "snippet"]) for row in queue]
+
+ print(queue)
+
# Rank texts by similarity for each query
results = self.query([query for _, query, _, _ in queue], texts)
@@ -88,7 +99,8 @@ class Extractor(Pipeline):
snippets.append(snippet)
# Run pipeline and return answers
- return self.answers(names, questions, contexts, topns, snippets)
+ answers = self.answers(names, questions, contexts, topns, snippets)
+ return [answer for _, answer in answers] if flatten else answers
def load(self, path, quantize, gpu, model, task):
"""