A passage can answer a question even when only a few of its words carry the relevant meaning. Multi-vector semantic search in Sentence Transformers retains token-level embeddings so a query can return ranked passages rather than rely on a single vector for each document.
The ColBERT-style MaxSim score matches each query token to its most similar document token, then sums those similarities. Query and document encoding use different recipes, so the two encoding methods are not interchangeable. Scores order passages for one query; they are not probabilities.
Exhaustive scoring fits a small in-memory corpus because it compares the query against each passage's token vectors. Those vectors take more storage than one embedding per passage. A CPU and a public English ColBERT checkpoint are enough for four short passages, with two questions checking that the expected answers rank first.
Steps to build multi-vector semantic search with Sentence Transformers:
- Create a Python 3.14 virtual environment in your project directory.
$ python3 -m venv .venv
Tested on Ubuntu 26.04 ARM64 with its default Python 3.14. The Ubuntu packages python3, python3-venv and ca-certificates provide the interpreter, environment support and HTTPS certificates.
Related: Create a Python virtual environment - Activate the project virtual environment.
$ source .venv/bin/activate
Package installation and model execution use this environment.
Related: Activate a Python virtual environment - Install the CPU version of PyTorch in the active environment.
$ python -m pip install --upgrade torch \ --index-url https://download.pytorch.org/whl/cpuThe CPU wheel avoids installing a CUDA runtime for this CPU-only program.
Related: Install Sentence Transformers with pip - Install Sentence Transformers 6.3.0 in the same environment.
$ python -m pip install "sentence-transformers==6.3.0"
MultiVectorEncoder requires Sentence Transformers 6 or later. A fresh environment keeps this major-version install separate from applications using v5.
Related: Migrate Sentence Transformers code to v6 - Create a file named search.py in your project directory using your text editor.
The remaining code blocks build this file from top to bottom.
- Enter the import and the four passages in search.py.
- search.py
from sentence_transformers import MultiVectorEncoder corpus = [ ("venus", "Venus is the second planet from the Sun."), ("mars", "Mars is known as the Red Planet."), ( "jupiter", "Jupiter is the largest planet in the Solar System.", ), ( "saturn", "Saturn is famous for its rings of ice and rock.", ), ]
Each tuple pairs a stable passage identifier with its text. The identifiers make it possible to check the retrieved answer without comparing display text.
- Append the corpus encoding section at the end of search.py.
model = MultiVectorEncoder( "answerdotai/answerai-colbert-small-v1", revision="934fa8bb4ce2284f4c2baa232d81aca4d076fa5e", device="cpu", similarity_fn_name="maxsim", ) corpus_embeddings = model.encode_document( [text for passage_id, text in corpus], show_progress_bar=False, ) print(f"Encoded {len(corpus_embeddings)} passages once.")
The pinned revision identifies the Answer.AI ColBERT checkpoint. The first load downloads public model files; no API key is required. Each encoded passage is a matrix of token vectors. Encoding the corpus once before either query lets both searches reuse the same in-memory corpus.
- Append the query function at the end of search.py.
def search(query): query_embeddings = model.encode_query( [query], show_progress_bar=False, ) scores = model.similarity( query_embeddings, corpus_embeddings, )[0] top_scores, top_indices = scores.topk(3) print(f"\nQuery: {query}") for rank, (score, index) in enumerate( zip(top_scores.tolist(), top_indices.tolist()), start=1, ): passage_id, text = corpus[index] print(f"{rank}. [{passage_id}] MaxSim={score:.4f}") print(f" {text}") return corpus[top_indices[0].item()][0]
The first row of the similarity matrix contains one MaxSim score per corpus passage. The three largest scores determine the ranking; their indices map back to the original passage identifiers and text.
Related: Sentence Transformers multi-vector applications - Append the two searches and expected-top-hit assertions at the end of search.py.
top_hit = search("Which planet is known as the Red Planet?") assert top_hit == "mars", f"Expected mars, got {top_hit}" top_hit = search("Which planet is famous for its rings?") assert top_hit == "saturn", f"Expected saturn, got {top_hit}" print("\nTop-hit checks passed.")
Each assertion compares the retrieved identifier with the intended answer. A different first result raises AssertionError and stops the program. Assertions are enabled by normal Python execution; the -O option disables them.
- Save search.py.
- Run search.py to confirm the ranked passages and both expected top hits. Example output:
$ python search.py ##### snipped ##### Encoded 4 passages once. Query: Which planet is known as the Red Planet? 1. [mars] MaxSim=31.4627 Mars is known as the Red Planet. ##### snipped ##### Query: Which planet is famous for its rings? 1. [saturn] MaxSim=31.0970 Saturn is famous for its rings of ice and rock. ##### snipped ##### Top-hit checks passed.
The omitted lines contain model-loading messages and lower-ranked passages. An unauthenticated Hub warning concerns download rate limits. This ColBERT checkpoint stores its projection separately, so the BERT loading report can list linear.weight as unexpected. The final line appears only after both assertions pass; small floating-point score differences can occur between runtimes.