Skip to content

Language models, retrieval, prompting, and language adaptation · Long-context and retrieval-augmented language · LNG055 · Model

ColBERT late-interaction retrieval

Represents queries and documents with multiple contextual token vectors, preserving finer-grained matching than a single pooled vector.

Runs in CodeSplash Complete calculation CodeSplash's own run

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Runs in CodeSplash
Code example
Complete calculation
Who ran it
CodeSplash's own run
CodeSplash's run
The reference's example ran in Python and R here, and they agree.
The reference's record
Python: The chapter's authors report running it; there is no record for this entry alone. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Represents queries and documents with multiple contextual token vectors, preserving finer-grained matching than a single pooled vector. Expensive encoding is largely separated from comparatively inexpensive late interaction during ranking.

Question / objective

Can token-level matching improve retrieval while retaining reusable document encodings?

Architecture / computational structure

Shared or paired contextual encoders produce token vectors; late MaxSim interaction aggregates query-token matches. Learned encoder/projection weights are parameters; compression, pruning, truncation, and candidate depth are index/hyperparameter settings.

Assumptions

Max pooling can reward isolated matches without proving coherent evidence or logical consistency.

Formula / computational definition

S(q,d)=∑i=1∣q∣max⁡1≤j≤∣d∣qi⊤dj.S(q,d)=\sum_{i=1}^{|q|}\max_{1\le j\le|d|}q_i^\top d_j.

S(q,d)=sum_i max_j q_i^T d_j, usually with normalized token vectors and implementation-specific masking. Each query token selects its best matching document token before scores are aggregated.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Token-vector dimension, document truncation, query augmentation, index compression, candidate generation, and retrieval/reranking depth.

Fitting, fine-tuning, and adaptation

Train with documented ranking losses and negatives; fine-tune encoder/projection layers or verified adapters, then rebuild token indexes. Compare frozen encoders before adaptation. Validate negation/phrase behavior, storage and latency, as isolated token matches can rank incoherent evidence highly.

How to use and calibrate

Assess ranking quality, retrieval recall, storage, latency, and phrase/negation sensitivity; compare candidates and reranking under equivalent budgets. Use development data for settings and an independent test set for the final estimate. Report uncertainty across examples or seeds when the evaluation design supports it; model scores and self-assessments are not automatically calibrated probabilities of correctness.

Example application

Rerank candidate scientific passages where specific terminology and multiple concepts matter.

Interpretation / pitfalls

Watch out

Multi-vector indexes require more storage than one-vector retrieval unless compressed carefully. The code below isolates one stated component; successful execution does not validate model quality or reproduce the cited experiment.

Python and R code examples

Full implementation route: Use the official ColBERT implementation for indexing and trained retrieval. R can call Python through reticulate; the native MaxSim example and Lab LNG-B reproduce the scoring operation only, not contextual token encoding or a production index.

Example scope: Exact ColBERT MaxSim aggregation on already normalized toy token vectors.

Dependencies: Python 3 standard library; base R; shared setup above. No model downloads or external services.

Validation: Python component executed in the combined example script; R counterpart supplied but not runtime-tested here. All numbers are synthetic.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Complete calculation — Exact ColBERT MaxSim aggregation on already normalized toy token vectors.

Shared setup for this chapter
import math

def softmax(xs):
    m = max(xs)
    es = [math.exp(x-m) for x in xs]
    return [e/sum(es) for e in es]

def dot(a, b):
    return sum(x*y for x, y in zip(a, b))

def matvec(matrix, vector):
    return [dot(row, vector) for row in matrix]

def meanvec(rows):
    return [sum(row[j] for row in rows)/len(rows) for j in range(len(rows[0]))]

def sigmoid(x):
    return 1/(1+math.exp(-x))

def nll(probabilities):
    return -sum(math.log(p) for p in probabilities)
query = [[1,0],[0,1]]; document = [[1,0],[0.6,0.8]]
similarities = [[dot(q,d) for d in document] for q in query]
score = sum(max(row) for row in similarities)
print(similarities,score)
R

Complete calculation — Exact ColBERT MaxSim aggregation on already normalized toy token vectors.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
query <- rbind(c(1,0),c(0,1)); document <- rbind(c(1,0),c(0.6,0.8))
similarities <- query %*% t(document)
score <- sum(apply(similarities,1,max))
print(list(similarities=similarities,score=score))

Sources

From the CodeSplash reference, entry LNG055.