Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Add configuration override to embeddings.load, closes #657

davidmezzetti6d7f7e6f2d09
diff --git a/src/python/txtai/app/base.py b/src/python/txtai/app/base.py
index bad3de6c..1eff99cf 100644
--- a/src/python/txtai/app/base.py
+++ b/src/python/txtai/app/base.py
@@ -143,11 +143,11 @@ class Application:
config = config.copy()
# Resolve callable functions
- config["tasks"] = [self.resolve(task) for task in config["tasks"]]
+ config["tasks"] = [self.resolvetask(task) for task in config["tasks"]]
# Resolve stream functions
if "stream" in config:
- config["stream"] = self.resolve(config["stream"])
+ config["stream"] = self.resolvetask(config["stream"])
# Get scheduler config
schedule = config.pop("schedule", None)
@@ -171,44 +171,26 @@ class Application:
loaddata: If True (default), load existing index data, if available. Otherwise, only load models.
"""
- # Resolve functions in embeddings config
- config = self.config.get("embeddings") if self.config else None
+ # Get embeddings configuration
+ config = self.config.get("embeddings")
if config:
- # Create copy of config
- config = config.copy()
-
- if "functions" in config:
- # Resolve callable functions
- functions = []
- for fn in config["functions"]:
- original = fn
- try:
- if isinstance(fn, dict):
- fn = fn.copy()
- fn["function"] = self.function(fn["function"])
- else:
- fn = self.function(fn)
-
- # pylint: disable=W0703
- except Exception:
- # Not a resolvable function, pipeline or workflow - further resolution will happen in embeddings
- fn = original
-
- functions.append(fn)
-
- config["functions"] = functions
-
- if "transform" in config:
- # Resolve transform function
- config["transform"] = self.function(config["transform"])
+ # Resolve application functions in embeddings config
+ config = self.resolveconfig(config.copy())
# Load embeddings index if loaddata and index exists
if loaddata and Embeddings().exists(self.config.get("path"), self.config.get("cloud")):
- # Load existing index if available
+ # Initialize empty embeddings
self.embeddings = Embeddings()
- self.embeddings.load(self.config.get("path"), self.config.get("cloud"))
+
+ # Pass path and cloud settings. Set application functions as config overrides.
+ self.embeddings.load(
+ self.config.get("path"),
+ self.config.get("cloud"),
+ {key: config[key] for key in ["functions", "transform"] if key in config} if config else None,
+ )
+
elif "embeddings" in self.config:
- # Initialize empty embeddings
+ # Create new embeddings with config
self.embeddings = Embeddings(config)
# If an extractor pipeline is defined and the similarity attribute is None, set to embeddings index
@@ -216,7 +198,7 @@ class Application:
if extractor and not extractor.similarity:
extractor.similarity = self.embeddings
- def resolve(self, task):
+ def resolvetask(self, task):
"""
Resolves callable functions for a task.
@@ -267,6 +249,44 @@ class Application:
return task
+ def resolveconfig(self, config):
+ """
+ Resolves callable functions stored in embeddings configuration.
+
+ Args:
+ config: embeddings config
+
+ Returns:
+ resolved config
+ """
+
+ if "functions" in config:
+ # Resolve callable functions
+ functions = []
+ for fn in config["functions"]:
+ original = fn
+ try:
+ if isinstance(fn, dict):
+ fn = fn.copy()
+ fn["function"] = self.function(fn["function"])
+ else:
+ fn = self.function(fn)
+
+ # pylint: disable=W0703
+ except Exception:
+ # Not a resolvable function, pipeline or workflow - further resolution will happen in embeddings
+ fn = original
+
+ functions.append(fn)
+
+ config["functions"] = functions
+
+ if "transform" in config:
+ # Resolve transform function
+ config["transform"] = self.function(config["transform"])
+
+ return config
+
def function(self, function):
"""
Get a handle to a callable function.
@@ -378,6 +398,9 @@ class Application:
if self.embeddings and self.documents:
with self.lock:
+ # Reset index
+ self.indexes(False)
+
# Build scoring index if term weighting is enabled
if self.embeddings.isweighted():
self.embeddings.score(self.documents)
diff --git a/src/python/txtai/embeddings/base.py b/src/python/txtai/embeddings/base.py
index 6daa3a90..63d89630 100644
--- a/src/python/txtai/embeddings/base.py
+++ b/src/python/txtai/embeddings/base.py
@@ -521,13 +521,14 @@ class Embeddings:
and (os.path.exists(f"{path}/embeddings") or os.path.exists(f"{path}/scoring"))
)
- def load(self, path=None, cloud=None, **kwargs):
+ def load(self, path=None, cloud=None, config=None, **kwargs):
"""
Loads an existing index from path.
Args:
path: input path
cloud: cloud storage configuration
+ config: configuration overrides
kwargs: additional configuration as keyword args
"""
@@ -544,6 +545,9 @@ class Embeddings:
# Load index configuration
self.config = self.loadconfig(path)
+ # Apply config overrides
+ self.config = {**self.config, **config} if config else self.config
+
# Approximate nearest neighbor index - stores dense vectors
self.ann = self.createann()
if self.ann:
@@ -679,14 +683,8 @@ class Embeddings:
"""
if self.config:
- # Copy and edit config
- config = self.config.copy()
-
- # Remove ids array if present
- config.pop("ids", None)
-
# Print configuration
- print(json.dumps(config, sort_keys=True, default=str, indent=2))
+ print(json.dumps(self.config, sort_keys=True, default=str, indent=2))
def issparse(self):
"""