💡 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 kwargs to labels/similarity pipeline, closes #882
davidmezzettif71fa1fd5e98
diff --git a/src/python/txtai/pipeline/text/labels.py b/src/python/txtai/pipeline/text/labels.pyindex 82a1006b..d3247ecc 100644--- a/src/python/txtai/pipeline/text/labels.py+++ b/src/python/txtai/pipeline/text/labels.py@@ -16,7 +16,7 @@ class Labels(HFPipeline):# Set if labels are dynamic (zero shot) or fixed (standard text classification)self.dynamic = dynamic- def __call__(self, text, labels=None, multilabel=False, flatten=None, workers=0):+ def __call__(self, text, labels=None, multilabel=False, flatten=None, workers=0, **kwargs):"""Applies a text classifier to text. Returns a list of (id, score) sorted by highest score,where id is the index in labels. For zero shot classification, a list of labels is required.@@ -32,6 +32,7 @@ class Labels(HFPipeline):multilabel: labels are independent if True, scores are normalized to sum to 1 per text item if False, raw scores returned if Noneflatten: flatten output to a list of labels if present. Accepts a boolean or float value to only keep scores greater than that number.workers: number of concurrent workers to use for processing data, defaults to None+ kwargs: additional keyword argsReturns:list of (id, score) or list of labels depending on flatten parameter@@ -45,7 +46,7 @@ class Labels(HFPipeline):function = "none" if multilabel is None else "sigmoid" if multilabel or len(self.labels()) == 1 else "softmax"# Run text classification pipeline- results = self.pipeline(text, top_k=None, function_to_apply=function, num_workers=workers)+ results = self.pipeline(text, top_k=None, function_to_apply=function, num_workers=workers, **kwargs)# Convert results to a list if necessaryif isinstance(text, str):diff --git a/src/python/txtai/pipeline/text/similarity.py b/src/python/txtai/pipeline/text/similarity.pyindex 739f2d37..1ef9f6e6 100644--- a/src/python/txtai/pipeline/text/similarity.py+++ b/src/python/txtai/pipeline/text/similarity.py@@ -21,7 +21,7 @@ class Similarity(Labels):self.crossencoder = CrossEncoder(model=self.pipeline) if crossencode else None# pylint: disable=W0222- def __call__(self, query, texts, multilabel=True):+ def __call__(self, query, texts, multilabel=True, **kwargs):"""Computes the similarity between query and list of text. Returns a list of(id, score) sorted by highest score, where id is the index in texts.@@ -34,6 +34,7 @@ class Similarity(Labels):query: query text|listtexts: list of textmultilabel: labels are independent if True, scores are normalized to sum to 1 per text item if False, raw scores returned if None+ kwargs: additional keyword argsReturns:list of (id, score)@@ -44,7 +45,7 @@ class Similarity(Labels):return self.crossencoder(query, texts, multilabel)# Call Labels pipeline for texts using input query as the candidate label- scores = super().__call__(texts, [query] if isinstance(query, str) else query, multilabel)+ scores = super().__call__(texts, [query] if isinstance(query, str) else query, multilabel, **kwargs)# Sort on query index idscores = [[score for _, score in sorted(row)] for row in scores]