💡 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 support for custom scoring instances, closes #544
davidmezzettie48a8885ce73
diff --git a/src/python/txtai/scoring/factory.py b/src/python/txtai/scoring/factory.pyindex 40c1d27e..cd15a60f 100644--- a/src/python/txtai/scoring/factory.py+++ b/src/python/txtai/scoring/factory.py@@ -2,6 +2,8 @@Factory module"""+from ..util import Resolver+from .base import Scoringfrom .bm25 import BM25from .sif import SIF@@ -24,18 +26,46 @@ class ScoringFactory:Scoring"""+ # Scoring instance+ scoring = None+# Support string and dict configurationif isinstance(config, str):config = {"method": config}- method = config.get("method") if config else None+ # Get scoring method+ method = config.get("method", "bm25")if method == "bm25":- return BM25(config)- if method == "sif":- return SIF(config)- if method == "tfidf":+ scoring = BM25(config)+ elif method == "sif":+ scoring = SIF(config)+ elif method == "tfidf":# Default scoring class implements tf-idf- return Scoring(config)+ scoring = Scoring(config)+ else:+ # Resolve custom method+ scoring = ScoringFactory.resolve(method, config)++ # Store config back+ config["method"] = method++ return scoring++ @staticmethod+ def resolve(backend, config):+ """+ Attempt to resolve a custom backend.++ Args:+ backend: backend class+ config: index configuration parameters++ Returns:+ Graph+ """- return None+ try:+ return Resolver()(backend)(config)+ except Exception as e:+ raise ImportError(f"Unable to resolve scoring backend: '{backend}'") from ediff --git a/test/python/testpipeline/testtrainer.py b/test/python/testpipeline/testtrainer.pyindex b5afdc54..b992acc1 100644--- a/test/python/testpipeline/testtrainer.py+++ b/test/python/testpipeline/testtrainer.py@@ -202,7 +202,7 @@ class TestTrainer(unittest.TestCase):data = []for x in self.data:- data.append({"text": x["text"], "label": [float(x["label"])] * 2})+ data.append({"text": x["text"], "label": [0.0, 1.0] if x["label"] else [1.0, 0.0]})trainer = HFTrainer()model, tokenizer = trainer("google/bert_uncased_L-2_H-128_A-2", data)diff --git a/test/python/testscoring.py b/test/python/testscoring.pyindex 54c46083..8b688673 100644--- a/test/python/testscoring.py+++ b/test/python/testscoring.py@@ -41,6 +41,21 @@ class TestScoring(unittest.TestCase):self.runTests("bm25")+ def testCustom(self):+ """+ Test custom method+ """++ self.runTests("txtai.scoring.BM25")++ def testCustomNotFound(self):+ """+ Test unresolvable custom method+ """++ with self.assertRaises(ImportError):+ ScoringFactory.create("notfound.scoring")+def testSIF(self):"""Test sif@@ -55,13 +70,6 @@ class TestScoring(unittest.TestCase):self.runTests("tfidf")- def testUnknown(self):- """- Test unknown method- """-- self.assertIsNone(ScoringFactory.create("unknown"))-def runTests(self, method):"""Runs a series of tests for a scoring method.