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 similarity pipeline to support late interaction models, closes #953

davidmezzetti2866dd962e59
diff --git a/setup.py b/setup.py
index ddf07f00..9b96f502 100644
--- a/setup.py
+++ b/setup.py
@@ -6,10 +6,10 @@ with open("README.md", "r", encoding="utf-8") as f:
DESCRIPTION = "".join([line for line in f if "gh-dark-mode-only" not in line])
# Required dependencies
-install = ["faiss-cpu>=1.7.1.post2", "msgpack>=1.0.7", "torch>=1.12.1", "transformers>=4.45.0"]
+install = ["faiss-cpu>=1.7.1.post2", "msgpack>=1.0.7", "torch>=2.1", "transformers>=4.45.0"]
-# Required dependencies that are also transformers dependencies
-install += ["huggingface-hub>=0.19.0", "numpy>=1.18.4", "pyyaml>=5.3", "regex>=2022.8.17"]
+# Required dependencies that are also base transformers dependencies
+install += ["huggingface-hub>=0.34.0", "numpy>=1.18.4", "pyyaml>=5.3", "regex>=2022.8.17", "safetensors>=0.4.5"]
# Optional dependencies
extras = {}
diff --git a/src/python/txtai/pipeline/text/__init__.py b/src/python/txtai/pipeline/text/__init__.py
index 146f4d00..df9cc507 100644
--- a/src/python/txtai/pipeline/text/__init__.py
+++ b/src/python/txtai/pipeline/text/__init__.py
@@ -5,6 +5,7 @@ Text imports
from .crossencoder import CrossEncoder
from .entity import Entity
from .labels import Labels
+from .lateencoder import LateEncoder
from .questions import Questions
from .similarity import Similarity
from .summary import Summary
diff --git a/src/python/txtai/pipeline/text/lateencoder.py b/src/python/txtai/pipeline/text/lateencoder.py
new file mode 100644
index 00000000..442aaa53
--- /dev/null
+++ b/src/python/txtai/pipeline/text/lateencoder.py
@@ -0,0 +1,89 @@
+"""
+Late encoder module
+"""
+
+import numpy as np
+import torch
+
+from ...models import Models, PoolingFactory
+from ..base import Pipeline
+
+
+class LateEncoder(Pipeline):
+ """
+ Computes similarity between query and list of text using a late interaction model.
+ """
+
+ def __init__(self, path=None, **kwargs):
+ # Get device
+ self.device = Models.deviceid(kwargs.get("gpu", True))
+
+ # Load model
+ self.model = PoolingFactory.create(
+ {
+ "method": kwargs.get("method"),
+ "path": path if path else "colbert-ir/colbertv2.0",
+ "device": self.device,
+ "tokenizer": kwargs.get("tokenizer"),
+ "maxlength": kwargs.get("maxlength"),
+ "modelargs": kwargs.get("vectors", {}),
+ }
+ )
+
+ def __call__(self, query, texts, limit=None, **kwargs):
+ """
+ Computes the similarity between query and list of text. Returns a list of
+ (id, score) sorted by highest score, where id is the index in texts.
+
+ This method supports query as a string or a list. If the input is a string,
+ the return type is a 1D list of (id, score). If text is a list, a 2D list
+ of (id, score) is returned with a row per string.
+
+ Args:
+ query: query text|list
+ texts: list of text
+ limit: maximum comparisons to return, defaults to all
+
+ Returns:
+ list of (id, score)
+ """
+
+ queries = [query] if isinstance(query, str) else query
+
+ # Encode text to vectors
+ queries = torch.from_numpy(self.model.encode(queries)).to(self.device)
+ data = torch.from_numpy(self.model.encode(texts)).to(self.device)
+
+ # Compute maximum similarity score
+ scores = []
+ for q in queries:
+ scores.extend(self.score(q.unsqueeze(0), data, limit))
+
+ return scores[0] if isinstance(query, str) else scores
+
+ def score(self, queries, data, limit):
+ """
+ Computes the maximum similarity score between query vectors and data vectors.
+
+ Args:
+ queries: query vectors
+ data: data vectors
+ limit: query limit
+
+ Returns:
+ list of (id, score)
+ """
+
+ # Compute bulk dot product using einstein notation
+ scores = torch.einsum("ash,bth->abst", queries, data).max(axis=-1).values.mean(axis=-1)
+ scores = scores.cpu().numpy()
+
+ # Get top n matching indices and scores
+ indices = np.argpartition(-scores, limit if limit and limit < scores.shape[0] else scores.shape[0] - 1)[:, :limit]
+ scores = np.take_along_axis(scores, indices, axis=1)
+
+ results = []
+ for x, index in enumerate(indices):
+ results.append(list(zip(index.tolist(), scores[x].tolist())))
+
+ return results
diff --git a/src/python/txtai/pipeline/text/similarity.py b/src/python/txtai/pipeline/text/similarity.py
index 1ef9f6e6..bed805f5 100644
--- a/src/python/txtai/pipeline/text/similarity.py
+++ b/src/python/txtai/pipeline/text/similarity.py
@@ -6,19 +6,26 @@ import numpy as np
from .crossencoder import CrossEncoder
from .labels import Labels
+from .lateencoder import LateEncoder
class Similarity(Labels):
"""
- Computes similarity between query and list of text using a text classifier.
+ Computes similarity between query and list of text using a transformers model.
"""
- def __init__(self, path=None, quantize=False, gpu=True, model=None, dynamic=True, crossencode=False, **kwargs):
- # Use zero-shot classification if dynamic is True and crossencode is False, otherwise use standard text classification
- super().__init__(path, quantize, gpu, model, False if crossencode else dynamic, **kwargs)
+ def __init__(self, path=None, quantize=False, gpu=True, model=None, dynamic=True, crossencode=False, lateencode=False, **kwargs):
+ self.crossencoder, self.lateencoder = None, None
- # Load as a cross-encoder if crossencode set to True
- self.crossencoder = CrossEncoder(model=self.pipeline) if crossencode else None
+ if lateencode:
+ # Load a late interaction encoder if lateencode set to True
+ self.lateencoder = LateEncoder(path=path, gpu=gpu, **kwargs)
+ else:
+ # Use zero-shot classification if dynamic is True and crossencode is False, otherwise use standard text classification
+ super().__init__(path, quantize, gpu, model, False if crossencode else dynamic, **kwargs)
+
+ # Load as a cross-encoder if crossencode set to True
+ self.crossencoder = CrossEncoder(model=self.pipeline) if crossencode else None
# pylint: disable=W0222
def __call__(self, query, texts, multilabel=True, **kwargs):
@@ -44,6 +51,9 @@ class Similarity(Labels):
# pylint: disable=E1102
return self.crossencoder(query, texts, multilabel)
+ if self.lateencoder:
+ return self.lateencoder(query, texts, **kwargs)
+
# Call Labels pipeline for texts using input query as the candidate label
scores = super().__call__(texts, [query] if isinstance(query, str) else query, multilabel, **kwargs)
diff --git a/test/python/testpipeline/testtext/testlabels.py b/test/python/testpipeline/testtext/testlabels.py
index aff9458f..029316f1 100644
--- a/test/python/testpipeline/testtext/testlabels.py
+++ b/test/python/testpipeline/testtext/testlabels.py
@@ -4,7 +4,7 @@ Labels module tests
import unittest
-from txtai.pipeline import Labels, Similarity
+from txtai.pipeline import Labels
class TestLabels(unittest.TestCase):
@@ -28,25 +28,6 @@ class TestLabels(unittest.TestCase):
]
cls.labels = Labels("prajjwal1/bert-medium-mnli")
- cls.similarity = Similarity(model=cls.labels)
-
- def testCrossEncoder(self):
- """
- Test cross-encoder similarity model
- """
-
- similarity = Similarity("cross-encoder/ms-marco-MiniLM-L-2-v2", crossencode=True)
- uid = similarity("Who won the lottery?", self.data)[0][0]
- self.assertEqual(self.data[uid], self.data[4])
-
- def testCrossEncoderBatch(self):
- """
- Test cross-encoder similarity model with multiple inputs
- """
-
- similarity = Similarity("cross-encoder/ms-marco-MiniLM-L-2-v2", crossencode=True)
- results = [r[0][0] for r in similarity(["Who won the lottery?", "Where did an iceberg collapse?"], self.data)]
- self.assertEqual(results, [4, 1])
def testLabel(self):
"""
@@ -102,38 +83,3 @@ class TestLabels(unittest.TestCase):
# Verify results
self.assertEqual(labels("This is the best sentence ever", flatten=True)[0], "POSITIVE")
self.assertEqual(labels("This is the best sentence ever", multilabel=True, flatten=True)[0], "POSITIVE")
-
- def testSimilarity(self):
- """
- Test similarity with single query
- """
-
- uid = self.similarity("feel good story", self.data)[0][0]
- self.assertEqual(self.data[uid], self.data[4])
-
- def testSimilarityBatch(self):
- """
- Test similarity with multiple queries
- """
-
- results = [r[0][0] for r in self.similarity(["feel good story", "climate change"], self.data)]
- self.assertEqual(results, [4, 1])
-
- def testSimilarityFixed(self):
- """
- Test similarity with a fixed label text classification model
- """
-
- similarity = Similarity(dynamic=False)
-
- # Test with query as label text and label id
- self.assertLessEqual(similarity("negative", ["This is the best sentence ever"])[0][1], 0.1)
- self.assertLessEqual(similarity("0", ["This is the best sentence ever"])[0][1], 0.1)
-
- def testSimilarityLong(self):
- """
- Test similarity with long text
- """
-
- uid = self.similarity("other", ["Very long text " * 1000, "other text"])[0][0]
- self.assertEqual(uid, 1)
diff --git a/test/python/testpipeline/testtext/testsimilarity.py b/test/python/testpipeline/testtext/testsimilarity.py
new file mode 100644
index 00000000..5193c738
--- /dev/null
+++ b/test/python/testpipeline/testtext/testsimilarity.py
@@ -0,0 +1,101 @@
+"""
+Similarity module tests
+"""
+
+import unittest
+
+from txtai.pipeline import Similarity
+
+
+class TestSimilarity(unittest.TestCase):
+ """
+ Similarity tests.
+ """
+
+ @classmethod
+ def setUpClass(cls):
+ """
+ Create single labels instance.
+ """
+
+ cls.data = [
+ "US tops 5 million confirmed virus cases",
+ "Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg",
+ "Beijing mobilises invasion craft along coast as Taiwan tensions escalate",
+ "The National Park Service warns against sacrificing slower friends in a bear attack",
+ "Maine man wins $1M from $25 lottery ticket",
+ "Make huge profits without work, earn up to $100,000 a day",
+ ]
+
+ cls.similarity = Similarity("prajjwal1/bert-medium-mnli")
+
+ def testCrossEncoder(self):
+ """
+ Test cross-encoder similarity model
+ """
+
+ similarity = Similarity("cross-encoder/ms-marco-MiniLM-L-2-v2", crossencode=True)
+ uid = similarity("Who won the lottery?", self.data)[0][0]
+ self.assertEqual(self.data[uid], self.data[4])
+
+ def testCrossEncoderBatch(self):
+ """
+ Test cross-encoder similarity model with multiple inputs
+ """
+
+ similarity = Similarity("cross-encoder/ms-marco-MiniLM-L-2-v2", crossencode=True)
+ results = [r[0][0] for r in similarity(["Who won the lottery?", "Where did an iceberg collapse?"], self.data)]
+ self.assertEqual(results, [4, 1])
+
+ def testLateEncoder(self):
+ """
+ Test late-encoder similarity model
+ """
+
+ similarity = Similarity("neuml/pylate-bert-tiny", lateencode=True)
+ uid = similarity("Who won the lottery?", self.data)[0][0]
+ self.assertEqual(self.data[uid], self.data[4])
+
+ def testLateEncoderBatch(self):
+ """
+ Test late-encoder similarity model with multiple inputs
+ """
+
+ similarity = Similarity("neuml/colbert-bert-tiny", lateencode=True)
+ results = [r[0][0] for r in similarity(["Who won the lottery?", "Where did an iceberg collapse?"], self.data)]
+ self.assertEqual(results, [4, 1])
+
+ def testSimilarity(self):
+ """
+ Test similarity with single query
+ """
+
+ uid = self.similarity("feel good story", self.data)[0][0]
+ self.assertEqual(self.data[uid], self.data[4])
+
+ def testSimilarityBatch(self):
+ """
+ Test similarity with multiple queries
+ """
+
+ results = [r[0][0] for r in self.similarity(["feel good story", "climate change"], self.data)]
+ self.assertEqual(results, [4, 1])
+
+ def testSimilarityFixed(self):
+ """
+ Test similarity with a fixed label text classification model
+ """
+
+ similarity = Similarity(dynamic=False)
+
+ # Test with query as label text and label id
+ self.assertLessEqual(similarity("negative", ["This is the best sentence ever"])[0][1], 0.1)
+ self.assertLessEqual(similarity("0", ["This is the best sentence ever"])[0][1], 0.1)
+
+ def testSimilarityLong(self):
+ """
+ Test similarity with long text
+ """
+
+ uid = self.similarity("other", ["Very long text " * 1000, "other text"])[0][0]
+ self.assertEqual(uid, 1)