💡 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
Persist Scoring component metadata with MessagePack, closes #776
davidmezzettib66c28325630
diff --git a/src/python/txtai/scoring/base.py b/src/python/txtai/scoring/base.pyindex 632b9f84..63503ad9 100644--- a/src/python/txtai/scoring/base.py+++ b/src/python/txtai/scoring/base.py@@ -17,7 +17,7 @@ class Scoring:"""# Scoring configuration- self.config = config if config else {}+ self.config = config if config is not None else {}# Transform columnscolumns = self.config.get("columns", {})diff --git a/src/python/txtai/scoring/tfidf.py b/src/python/txtai/scoring/tfidf.pyindex 6f9b63e1..ec3c81fe 100644--- a/src/python/txtai/scoring/tfidf.py+++ b/src/python/txtai/scoring/tfidf.py@@ -4,7 +4,6 @@ TFIDF moduleimport mathimport os-import picklefrom collections import Counterfrom multiprocessing.pool import ThreadPool@@ -12,7 +11,7 @@ from multiprocessing.pool import ThreadPoolimport numpy as npfrom ..pipeline import Tokenizer-from ..version import __pickle__+from ..serialize import Serializerfrom .base import Scoringfrom .terms import Terms@@ -194,27 +193,40 @@ class TFIDF(Scoring):return self.terms.count() if self.terms else self.totaldef load(self, path):- with open(path, "rb") as handle:- # Load scoring- self.__dict__.update(pickle.load(handle))+ # Load scoring+ state = Serializer.load(path)- # Load terms- if self.config.get("terms"):- self.terms = Terms(self.config["terms"], self.score, self.idf)- self.terms.load(path + ".terms")+ # Convert to Counter instances+ for key in ["docfreq", "wordfreq", "tags"]:+ state[key] = Counter(state[key])++ # Convert documents to dict+ state["documents"] = dict(state["documents"]) if state["documents"] else state["documents"]++ # Set parameters on this object+ self.__dict__.update(state)++ # Load terms+ if self.config.get("terms"):+ self.terms = Terms(self.config["terms"], self.score, self.idf)+ self.terms.load(path + ".terms")def save(self, path):- with open(path, "wb") as handle:- # Don't serialize following fields- skipfields = ("config", "terms", "tokenizer")+ # Don't serialize following fields+ skipfields = ("config", "terms", "tokenizer")- # Save scoring- state = {key: value for key, value in self.__dict__.items() if key not in skipfields}- pickle.dump(state, handle, protocol=__pickle__)+ # Get object state+ state = {key: value for key, value in self.__dict__.items() if key not in skipfields}- # Save terms- if self.terms:- self.terms.save(path + ".terms")+ # Update documents to tuples+ state["documents"] = list(state["documents"].items()) if state["documents"] else state["documents"]++ # Save scoring+ Serializer.save(state, path)++ # Save terms+ if self.terms:+ self.terms.save(path + ".terms")def close(self):if self.terms: