💡 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
Refactor embeddings configuration into separate component #778
davidmezzettie3a2768e3fac
diff --git a/src/python/txtai/embeddings/index/configuration.py b/src/python/txtai/embeddings/index/configuration.pynew file mode 100644index 00000000..e917b845--- /dev/null+++ b/src/python/txtai/embeddings/index/configuration.py@@ -0,0 +1,75 @@+"""+Configuration module+"""++import json+import os++from ...serialize import SerializeFactory+++class Configuration:+ """+ Loads and saves index configuration.+ """++ def load(self, path):+ """+ Loads index configuration. This method supports both config.json and config pickle files.++ Args:+ path: path to directory++ Returns:+ dict+ """++ # Configuration+ config = None++ # Determine if config is json or pickle+ jsonconfig = os.path.exists(f"{path}/config.json")++ # Set config file name+ name = "config.json" if jsonconfig else "config"++ # Load configuration+ with open(f"{path}/{name}", "r" if jsonconfig else "rb", encoding="utf-8" if jsonconfig else None) as handle:+ # Load JSON, also is backwards-compatible with pickle configuration+ config = json.load(handle) if jsonconfig else SerializeFactory.create("pickle").loadstream(handle)++ # Add format parameter+ config["format"] = "json" if jsonconfig else "pickle"++ # Build full path to embedding vectors file+ if config.get("storevectors"):+ config["path"] = os.path.join(path, config["path"])++ return config++ def save(self, config, path):+ """+ Saves index configuration. This method defaults to JSON and falls back to pickle.++ Args:+ config: configuration to save+ path: path to directory++ Returns:+ dict+ """++ # Default to JSON config+ jsonconfig = config.get("format", "json") == "json"++ # Set config file name+ name = "config.json" if jsonconfig else "config"++ # Write configuration+ with open(f"{path}/{name}", "w" if jsonconfig else "wb", encoding="utf-8" if jsonconfig else None) as handle:+ if jsonconfig:+ # Write config as JSON+ json.dump(config, handle, default=str, indent=2)+ else:+ # Backwards compatible method to load pickle configuration+ SerializeFactory.create("pickle").savestream(config, handle)