Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Update Faiss ANN to support IVF strings without number of cells, closes #594

davidmezzetti717680296808
diff --git a/src/python/txtai/ann/faiss.py b/src/python/txtai/ann/faiss.py
index 67e6b3b1..72be5547 100644
--- a/src/python/txtai/ann/faiss.py
+++ b/src/python/txtai/ann/faiss.py
@@ -115,7 +115,8 @@ class Faiss(ANN):
components = self.setting("components")
if components:
- return components
+ # Format and return components string
+ return self.components(components, train)
# Derive quantization. Prefer backend-specific setting. Fallback to root-level parameter.
quantize = self.setting("quantize", self.config.get("quantize"))
@@ -171,7 +172,28 @@ class Faiss(ANN):
# Calculate number of IVF cells where x = min(4 * sqrt(embeddings count), embeddings count / 39)
# Faiss requires at least 39 * x data points
- return min(round(4 * math.sqrt(count)), int(count / 39))
+ return max(min(round(4 * math.sqrt(count)), int(count / 39)), 1)
+
+ def components(self, components, train):
+ """
+ Formats a components string. This method automatically calculates the optimal number of IVF cells, if omitted.
+
+ Args:
+ components: input components string
+ train: number of rows selected for model training
+
+ Returns:
+ formatted components string
+ """
+
+ # Optimal number of IVF cells
+ x = self.cells(train)
+
+ # Add number of IVF cells, if missing
+ components = [f"IVF{x}" if component == "IVF" else component for component in components.split(",")]
+
+ # Return components string
+ return ",".join(components)
def nprobe(self):
"""
diff --git a/test/python/testann.py b/test/python/testann.py
index b856cbb5..1f642434 100644
--- a/test/python/testann.py
+++ b/test/python/testann.py
@@ -60,6 +60,7 @@ class TestANN(unittest.TestCase):
# Test with custom settings
self.runTests("faiss", {"faiss": {"nprobe": 2, "components": "PCA16,IDMap,SQ8", "sample": 1.0}}, False)
+ self.runTests("faiss", {"faiss": {"components": "IVF,SQ8"}}, False)
@unittest.skipIf(os.name == "nt", "mmap not supported on Windows")
def testFaissMmap(self):