Sentence Embeddings

Beyond Single Words

Sentence embeddings encode an entire sentence or paragraph into a single fixed-length vector. The key advance over word embeddings is context-awareness: the representation of each word is influenced by all the other words in the sentence.

Context-aware encoding

"I went to the bank to deposit money" and "I sat on the bank of the river" produce different sentence embeddings that correctly capture their different meanings — even though they share the word "bank."

How They Are Produced

Sentence embedding models use transformer architectures. Transformers process all words simultaneously using attention mechanisms — each word attends to all other words and builds a contextual representation. The per-token representations are then pooled (typically by averaging) into a single fixed-length vector.

SBERT — The Key Innovation

SBERT (Sentence-BERT, 2019) is the most influential sentence embedding model. Its innovation was the training objective: a siamese network trained with contrastive loss to pull similar sentence pairs together and push dissimilar pairs apart in vector space.

Original BERT for similarity

Required one forward pass per sentence pair. Comparing n sentences required n×(n-1)/2 passes. Impractical for large-scale retrieval.

SBERT for similarity

Requires one forward pass per sentence. Embeddings are precomputed and compared with a dot product. Scales to millions of documents.

Choosing an Embedding Model

1

Local models (all-MiniLM, all-mpnet) — Run on CPU, no API cost, full control. Best for privacy-sensitive data or budget constraints.

2

API models (OpenAI text-embedding-3-small/large) — No setup, high quality, per-token cost. Best for quick deployment.

3

Domain-specific models — Fine-tuned on medical, legal, or scientific text. Outperform general models for specialized domains.

Critical rule

Always embed queries and documents with the same model. Vectors from different models live in incompatible spaces — comparing them produces meaningless results.

Need Help?

Ask the AI assistant about sentence embeddings, SBERT, or how to choose an embedding model.