💡 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
Respect numeric zero label flattening thresholds (#1398)
HuaTNA224ac2de1aee
diff --git a/src/python/txtai/pipeline/text/labels.py b/src/python/txtai/pipeline/text/labels.pyindex 5afd49ea..d2e2d27b 100644--- a/src/python/txtai/pipeline/text/labels.py+++ b/src/python/txtai/pipeline/text/labels.py@@ -88,13 +88,13 @@ class Labels(HFPipeline):for result in results:if self.dynamic:- if flatten:+ if flatten is not None and flatten is not False:result = [label for x, label in enumerate(result["labels"]) if result["scores"][x] >= threshold]yield result[:1] if isinstance(flatten, bool) else resultelse:yield [(labels.index(label), result["scores"][x]) for x, label in enumerate(result["labels"])]else:- if flatten:+ if flatten is not None and flatten is not False:result = [x["label"] for x in result if x["score"] >= threshold and (not labels or x["label"] in labels)]yield result[:1] if isinstance(flatten, bool) else resultelse:diff --git a/test/python/testpipeline/testtext/testlabels.py b/test/python/testpipeline/testtext/testlabels.pyindex 6b050574..afb9ddae 100644--- a/test/python/testpipeline/testtext/testlabels.py+++ b/test/python/testpipeline/testtext/testlabels.py@@ -59,6 +59,20 @@ class TestLabels(unittest.TestCase):results = [l[0] for l in self.labels(["This is the best sentence ever", "This is terrible"], ["positive", "negative"], flatten=True)]self.assertEqual(results, ["positive", "negative"])+ def testLabelZeroThreshold(self):+ """+ Test numeric zero flattening thresholds with dynamic and fixed labels+ """++ text = "This is the best sentence ever"+ for classifier, candidates in ((self.labels, ["positive", "negative"]), (Labels(dynamic=False), ["POSITIVE", "NEGATIVE"])):+ with self.subTest(dynamic=classifier.dynamic):+ self.assertCountEqual(classifier(text, candidates, flatten=0.0), candidates)+ self.assertCountEqual(classifier([text], candidates, flatten=0.0)[0], candidates)++ # Boolean False still requests the original score tuples.+ self.assertEqual(classifier(text, candidates, flatten=False), classifier(text, candidates))+def testLabelFixed(self):"""Test labels with a fixed label text classification model