Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Improve late pooling configuration errors (#1217)

Morgan Carr6ebfbbb71681
diff --git a/docs/embeddings/configuration/vectors.md b/docs/embeddings/configuration/vectors.md
index 0c214bc7..ea884634 100644
--- a/docs/embeddings/configuration/vectors.md
+++ b/docs/embeddings/configuration/vectors.md
@@ -176,7 +176,8 @@ muvera:
```
Settings to control the size of MUVERA fixed dimensional outputs. Default is 20 * 2^5 * 16 = 10,240 dimensions.
-Set `muvera` to `false` to disable it or `true` to use the default settings.
+Set `muvera` to `false` to disable it or `true` to use the default settings. `false` only makes sense when the multi-vector output
+is consumed directly, for example through `PoolingFactory`. Embeddings vectors require `muvera` or `lemur`.
### lemur
```yaml
diff --git a/src/python/txtai/models/pooling/late.py b/src/python/txtai/models/pooling/late.py
index d46c4d87..63eec2f7 100644
--- a/src/python/txtai/models/pooling/late.py
+++ b/src/python/txtai/models/pooling/late.py
@@ -291,7 +291,8 @@ class LatePooling(Pooling):
config = self.load(path, "artifact.metadata")
params = ["query_token_id", "query_maxlen", "doc_token_id", "doc_maxlen"]
- return [config.get(p) for p in params]
+ # Use defaults when the settings file is missing
+ return [config.get(p) for p in params] if config else [None] * len(params)
def loadlinear(self, path, models):
"""
diff --git a/src/python/txtai/models/pooling/lemur.py b/src/python/txtai/models/pooling/lemur.py
index 15558866..851da3ba 100644
--- a/src/python/txtai/models/pooling/lemur.py
+++ b/src/python/txtai/models/pooling/lemur.py
@@ -209,6 +209,10 @@ class Lemur:
with open(config, encoding="utf-8") as source:
self.config = json.load(source)
+ # Encoder repos also have config.json and model.safetensors, check this is a LEMUR artifact
+ if "modeltype" not in self.config:
+ raise ValueError(f"{path} is not a LEMUR artifact, create one with LemurTrainer")
+
self.model = LemurModel(**self.config).to(self.device)
training = self.config.get("training", {})
self.selectedepoch = training.get("selectedepoch")
diff --git a/src/python/txtai/vectors/base.py b/src/python/txtai/vectors/base.py
index 0c07c47a..bd058973 100644
--- a/src/python/txtai/vectors/base.py
+++ b/src/python/txtai/vectors/base.py
@@ -378,6 +378,9 @@ class Vectors:
embeddings = self.encode(data, category)
if embeddings is not None:
+ if embeddings.ndim != 2:
+ raise ValueError(f"Expected 2 dimensional vectors, produced {embeddings.ndim} dimensions")
+
# Truncate embeddings, if necessary
if self.dimensionality and self.dimensionality < embeddings.shape[1]:
embeddings = self.truncate(embeddings)
diff --git a/test/python/testmodels/testpooling.py b/test/python/testmodels/testpooling.py
index 419b4078..372fccc1 100644
--- a/test/python/testmodels/testpooling.py
+++ b/test/python/testmodels/testpooling.py
@@ -221,6 +221,18 @@ class TestPooling(unittest.TestCase):
with self.assertRaisesRegex(ValueError, message):
pool.centersettings(center, linear, True)
+ def testLateSettings(self):
+ """
+ Test late pooling defaults when the settings file is missing
+ """
+
+ pool = PoolingFactory.create({"path": "neuml/colbert-bert-tiny", "device": self.device, "modelargs": {"muvera": None}})
+
+ # A missing config_sentence_transformers.json (PyLate) or artifact.metadata (Stanford) reads as default settings
+ with patch.object(LatePooling, "load", return_value=None):
+ self.assertEqual(pool.settings("neuml/colbert-bert-tiny", [{"path": "1_Dense"}]), [None] * 4)
+ self.assertEqual(pool.settings("neuml/colbert-bert-tiny", None), [None] * 4)
+
def testLemur(self):
"""
Test late pooling with LEMUR fixed dimensional encoding
@@ -482,6 +494,17 @@ class TestPooling(unittest.TestCase):
with self.assertRaisesRegex(ValueError, "each document must be a non-empty 2D token-vector array"):
lemur.documents(invalid)
+ def testLemurPathValidation(self):
+ """
+ Test LEMUR rejects a path that is not a LEMUR artifact
+ """
+
+ # An encoder repo also has config.json and model.safetensors
+ for model in ["neuml/colbert-bert-tiny", "neuml/pylate-bert-tiny"]:
+ with self.subTest(model=model):
+ with self.assertRaisesRegex(ValueError, "not a LEMUR artifact"):
+ PoolingFactory.create({"path": model, "device": self.device, "modelargs": {"lemur": {"path": model}}})
+
def testLemurStateValidation(self):
"""
Test LEMUR rejects invalid categories and unfitted encoding
diff --git a/test/python/testvectors/testdense/testhuggingface.py b/test/python/testvectors/testdense/testhuggingface.py
index 6fbb07c1..1a1689ef 100644
--- a/test/python/testvectors/testdense/testhuggingface.py
+++ b/test/python/testvectors/testdense/testhuggingface.py
@@ -7,6 +7,7 @@ import unittest
import numpy as np
+from txtai.embeddings import Embeddings
from txtai.vectors import VectorsFactory
@@ -42,6 +43,15 @@ class TestHFVectors(unittest.TestCase):
with open(stream, "rb") as queue:
self.assertEqual(np.load(queue).shape, (500, 768))
+ def testLateInteraction(self):
+ """
+ Test late interaction models produce 2 dimensional vectors
+ """
+
+ embeddings = Embeddings({"path": "neuml/colbert-bert-tiny", "backend": "numpy", "content": False, "gpu": False, "vectors": {"muvera": False}})
+ with self.assertRaisesRegex(ValueError, "Expected 2 dimensional vectors"):
+ embeddings.index(["Short text.", "A considerably longer text exercises padding behavior.", "Third text."])
+
def testText(self):
"""
Test transformers text conversion