Skip to content

Language models, retrieval, prompting, and language adaptation · Contextual and sentence encoders · LNG019 · Model

E5 text embeddings

A text-embedding family trained using weakly supervised contrastive text pairs, with later supervised stages in relevant releases. It supports query–passage retrieval and general representation tasks without generation.

Runs in CodeSplash Component demo, not a trained model 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
Component demo, not a trained model
Who ran it
CodeSplash's own run
CodeSplash's run
The reference's example ran here in one language.
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

A text-embedding family trained using weakly supervised contrastive text pairs, with later supervised stages in relevant releases. It supports query–passage retrieval and general representation tasks without generation.

Question / objective

Can weakly supervised text pairs produce transferable retrieval embeddings?

Architecture / computational structure

Dual-encoder representations are optimized with contrastive losses over query/document-like pairs. Learned encoder/projection weights are parameters; input prefixes, negative sampling, pooling, and temperature are design controls.

Assumptions

Query/passage formatting matters and must follow the specific model card.

Formula / computational definition

Lq=−log⁡exp⁡(s(q,d+)/τ)∑d∈Dqexp⁡(s(q,d)/τ).\mathcal L_q=-\log\frac{\exp(s(q,d^+)/\tau)}{\sum_{d\in\mathcal D_q}\exp(s(q,d)/\tau)}.

A dual/shared encoder scores a query q and passage d using normalized embedding similarity; contrastive loss increases s(q,d_positive) relative to candidate negatives through a temperature-scaled softmax. The exact stage mixture is model-version-dependent.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Model variant, task-specific text prefixes/instructions, pooling, normalization, sequence limit, negative sampling, and batch size.

Fitting, fine-tuning, and adaptation

Use the documented query/passage formatting and pooling for the selected checkpoint. Adapt with domain-relevance pairs and hard negatives, optionally through compatible adapters. Freeze the encoder while testing a retrieval baseline; after any encoder update rebuild the corpus index and validate domain transfer.

How to use and calibrate

Use multilingual/domain-specific retrieval tests, recall at candidate depth, nDCG, and false-neighbor audits; distinguish retrieval evaluation from classification transfer. 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

Retrieve technical passages from natural-language questions with a compact dense index.

Interpretation / pitfalls

Watch out

Weak text-pair supervision can encode noisy relevance; embeddings do not establish factual entailment or answer correctness. 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: Transformers or SentenceTransformers can load supported E5 checkpoints using documented formatting. R can call the same encoder via reticulate and perform vector indexing/evaluation natively or through a documented backend.

Example scope: E5-style input prefixes and a toy dense ranking after embedding; no encoder invocation.

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

Component demo, not a trained model — E5-style input prefixes and a toy dense ranking after embedding; no encoder invocation.

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_text, passage_text = "query: battery recycling", "passage: lithium recovery methods"
q, docs = [0.8,0.6], [[1,0], [0.6,0.8], [-1,0]]
scores = [dot(q,d) for d in docs]
print(query_text, passage_text, sorted(range(3), key=scores.__getitem__, reverse=True))
R

Component demo, not a trained model — E5-style input prefixes and a toy dense ranking after embedding; no encoder invocation.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
query_text <- "query: battery recycling"; passage_text <- "passage: lithium recovery methods"
q <- c(0.8,0.6); docs <- rbind(c(1,0), c(0.6,0.8), c(-1,0))
scores <- drop(docs %*% q)
print(list(query=query_text, passage=passage_text, ranking=order(scores,decreasing=TRUE)))

Sources

From the CodeSplash reference, entry LNG019.