Search queries can match individual parts of a passage that a single pooled vector may obscure. Sentence Transformers generates multi-vector embeddings that retain a separate vector for each encoded token, allowing a query to match several parts of a document.

ColBERT compares these matrices through MaxSim. For each query token, it takes the highest similarity to a document token, then sums those matches. The resulting scores rank documents for the same query; they are not probabilities.

The small English Answer.AI ColBERT checkpoint includes the trained projection needed for token embeddings and runs on a CPU. Its query and document encoders apply different prefixes automatically, so neither manual prefix insertion nor sentence-level pooling is needed.

Steps to generate multi-vector embeddings with Sentence Transformers:

  1. Activate a fresh Python 3.14 virtual environment in your project directory.
    $ source .venv/bin/activate

    If the project has no virtual environment, create one first. On Ubuntu, the matching package is python3-venv.
    Related: Create a Python virtual environment.
    Related: Activate a Python virtual environment.

  2. Install the CPU build of PyTorch into the active environment.
    $ python -m pip install --upgrade torch \
        --index-url https://download.pytorch.org/whl/cpu

    The CPU installation path was tested on Ubuntu 26.04 arm64 with Python 3.14.
    Related: Install Sentence Transformers with pip

  3. Install Sentence Transformers version 6 into the same environment.

    Sentence Transformers 6 changes APIs from version 5. The fresh environment keeps the upgrade separate from an existing application's dependencies.

    $ python -m pip install --upgrade "sentence-transformers>=6,<7"

    MultiVectorEncoder was introduced in version 6.0. Tested with Sentence Transformers 6.3.0 and PyTorch 2.15.0.
    Related: Multi-vector encoder usage

  4. Create multi_vector.py in your text editor with two queries and their matching documents.
    multi_vector.py
    import torch
    from sentence_transformers import MultiVectorEncoder
     
     
    queries = [
        "What is the capital of France?",
        "How do you bake bread?",
    ]
    documents = [
        "Paris is the capital of France.",
        "Bread dough is baked in a hot oven.",
    ]

    The lists deliberately use the same order. The first query should match the first document, and the second query should match the second document.

  5. Append the checkpoint loading and token encoding calls below the document list.
    multi_vector.py
    model = MultiVectorEncoder(
        "answerdotai/answerai-colbert-small-v1",
        device="cpu",
    )
    query_embeddings = model.encode_query(queries)
    document_embeddings = model.encode_document(documents)

    The first model load downloads the public checkpoint from Hugging Face without requiring a token. Each encoding result is a list of two-dimensional tensors shaped as token count by embedding dimension. This checkpoint expands queries to 32 positions; document lengths vary with tokenization and punctuation filtering. A BertModel load report can mark linear.weight as unexpected because MultiVectorEncoder loads that ColBERT projection separately.
    Related: Answer.AI ColBERT model card

  6. Append the MaxSim scoring and relevance checks below the encoding calls.
    multi_vector.py
    scores = model.similarity(
        query_embeddings, document_embeddings
    )
    for i, embedding in enumerate(query_embeddings):
        print("query", i, tuple(embedding.shape))
    for i, embedding in enumerate(document_embeddings):
        print("doc", i, tuple(embedding.shape))
    print("score shape:", tuple(scores.shape))
    print("MaxSim rows:")
    for row in scores:
        print([round(float(value), 4) for value in row])
     
    assert scores.shape == (len(queries), len(documents))
    assert torch.isfinite(scores).all()
    assert scores[0, 0] > scores[0, 1]
    assert scores[1, 1] > scores[1, 0]
    print("top docs:", scores.argmax(dim=1).tolist())

    model.similarity() uses MaxSim by default. Rows follow the query order and columns follow the document order. The assertions reject a wrong matrix shape, nonfinite scores, or a ranking that favors the unrelated document for either query.

  7. Save multi_vector.py in your editor.
  8. Run multi_vector.py to calculate the MaxSim scores. Example output:
    $ python multi_vector.py
    ##### snipped #####
    query 0 (32, 96)
    query 1 (32, 96)
    doc 0 (9, 96)
    doc 1 (11, 96)
    score shape: (2, 2)
    MaxSim rows:
    [31.4515, 30.2343]
    [30.0433, 31.2685]
    top docs: [0, 1]

    The 96 columns are the checkpoint's embedding dimension. The 2-by-2 score matrix compares both queries with both documents, and indices [0, 1] identify the expected top matches. Scores can differ slightly across library versions. A traceback from an assertion means the shape, scores, or ranking did not pass the corresponding check.

Share this guide