💡 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, closes #778
davidmezzetti0a82ea31cdf8
diff --git a/src/python/txtai/embeddings/base.py b/src/python/txtai/embeddings/base.pyindex e2d7d547..fa95831f 100644--- a/src/python/txtai/embeddings/base.py+++ b/src/python/txtai/embeddings/base.py@@ -3,7 +3,6 @@ Embeddings module"""import json-import pickleimport osimport shutilimport tempfile@@ -17,9 +16,8 @@ from ..database import DatabaseFactoryfrom ..graph import GraphFactoryfrom ..scoring import ScoringFactoryfrom ..vectors import VectorsFactory-from ..version import __pickle__-from .index import Action, Functions, Indexes, IndexIds, Reducer, Stream, Transform+from .index import Action, Configuration, Functions, Indexes, IndexIds, Reducer, Stream, Transformfrom .search import Explain, Ids, Query, Search, Terms@@ -521,7 +519,7 @@ class Embeddings:return os.path.exists(apath)# Return true if path has a config.json or config file with an offset set- return path and (os.path.exists(f"{path}/config.json") or os.path.exists(f"{path}/config")) and "offset" in self.loadconfig(path)+ return path and (os.path.exists(f"{path}/config.json") or os.path.exists(f"{path}/config")) and "offset" in Configuration().load(path)def load(self, path=None, cloud=None, config=None, **kwargs):"""@@ -545,7 +543,7 @@ class Embeddings:self.archive.load(apath)# Load index configuration- self.config = self.loadconfig(path)+ self.config = Configuration().load(path)# Apply config overridesself.config = {**self.config, **config} if config else self.config@@ -616,7 +614,7 @@ class Embeddings:self.config["path"] = os.path.basename(self.config["path"])# Save index configuration- self.saveconfig(path)+ Configuration().save(self.config, path)# Save approximate nearest neighbor indexif self.ann:@@ -819,65 +817,6 @@ class Embeddings:params = [("keyword", False), ("defaults", True)]return all(self.config.get(key, default) == default for key, default in params)- def loadconfig(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:- config = json.load(handle) if jsonconfig else pickle.load(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 saveconfig(self, path):- """- Saves index configuration. This method defaults to JSON and falls back to pickle.-- Args:- path: path to directory-- Returns:- dict- """-- # Default to JSON config- jsonconfig = self.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(self.config, handle, default=str, indent=2)- else:- # Write config as pickle format- pickle.dump(self.config, handle, protocol=__pickle__)-def loadvectors(self):"""Loads a vector model set in config.@@ -985,7 +924,7 @@ class Embeddings:if "graph" in self.config:# Get or create graph configuration- config = self.config["graph"] if self.config["graph"] else {}+ config = self.config["graph"] if "graph" in self.config else {}# Create configuration with custom columns, if necessaryconfig = self.columns(config)diff --git a/src/python/txtai/embeddings/index/__init__.py b/src/python/txtai/embeddings/index/__init__.pyindex 9778aa60..013d4191 100644--- a/src/python/txtai/embeddings/index/__init__.py+++ b/src/python/txtai/embeddings/index/__init__.py@@ -5,6 +5,7 @@ Index importsfrom .action import Actionfrom .autoid import AutoId+from .configuration import Configurationfrom .documents import Documentsfrom .functions import Functionsfrom .indexes import Indexes