Module 1 · Beyond naive RAG

Choosing embeddings: dimensions, multilingual, domain

5 min read

The embedding model is the foundation of dense search: it turns each text into a vector, and the proximity between vectors is supposed to reflect the proximity of meaning. Its choice is the most structural one in the pipeline — changing it means re-embedding everything — and yet it is often made by default. This lesson gives the criteria that matter: dimensions, language, domain, prefixes, length and cost.

What an embedding model does

An embedding model (a bi-encoder) reads a text and produces a fixed-size vector — 384, 768, 1,024 dimensions or more. Two texts with close meanings give close vectors, measured by cosine similarity. Prism normalizes all vectors, so the dot product equals the cosine:

def normalize(vectors: np.ndarray) -> np.ndarray:
    norms = np.linalg.norm(vectors, axis=1, keepdims=True)
    return vectors / np.maximum(norms, 1e-12)

With the course's multilingual model, « délai de retour » (return period) and "return window" have a similarity of 0.46, versus 0.15 for « délai de retour » and « recette de cuisine » (cooking recipe): the model links the two languages, but the gap is not huge. This is typical: absolute similarities vary a lot from one model to another; only the ranking matters.

Criterion #1: language

A model trained mostly on English gives poor results on a French corpus, and does not link an English question to a French document. For a non-English corpus, choose a multilingual model. The test bench shows it: dense search retrieves 92% of the English questions on a French corpus, lexical search 0%.

Dimensions: precision versus cost

DimensionsMemory per vector (float32)For 10 million passages
3841.5 KB15 GB
7683 KB31 GB
1,0244 KB41 GB
3,07212 KB123 GB

More dimensions generally mean better quality, but cost memory, storage and distance computation time. Two techniques reduce that cost without changing models:

  • Quantization stores each dimension on fewer bits (lesson 2.4).
  • "Matryoshka" embeddings: some models are trained so that the first dimensions concentrate most of the information. You can then truncate the vector (keep 256 dimensions out of 1,024) and lose little quality. Only truncate models designed for it: on a regular model, truncation destroys quality.

Query and document prefixes

Some models are trained with prefixes that distinguish questions from documents (for example query: and passage: for the E5 family, or an instruction at the start of the query for others). Omitting them silently degrades results. Read the model card: this is the kind of detail that costs several points without anything flagging the error.

Maximum length

Each model has a maximum length (often 256 or 512 tokens, sometimes 8,192). Beyond it, the text is truncated without warning: the end of a long passage is simply not represented in its vector. Check that your passages, title and metadata included, stay under that limit.

The domain

A general-purpose model knows little about the vocabulary of a specialized domain (legal, medical, industrial) and internal references (product codes, project names). Three answers, by increasing cost:

  1. Hybrid search (lesson 3.1): the lexical part catches the exact terms that embeddings handle poorly. This is the first thing to do.
  2. Context added to passages (lesson 3.4): a passage that carries its context is better represented.
  3. Adapting the model (fine-tuning) on question/passage pairs from your domain: effective, but it requires data, expertise, and a full re-embedding with every version.

Local or API?

Local model (FastEmbed, Sentence Transformers, Ollama)API (Voyage AI, Cohere, OpenAI, Mistral…)
CostFree, apart from computeBilled per token
PrivacyTexts stay with youTexts leave your system
QualityGood, often a notch below the best API modelsOften the best
LatencyDepends on your hardware (CPU or GPU)Network, but fast compute
DependencyNoneThe provider can change or retire a model

The last point is often forgotten: if the provider retires an embedding model, your whole index must be recomputed with another one. Always store the model name and version with each vector, and keep the source texts so you can re-embed.

Caching embeddings

Computing embeddings costs (time or money), and the same text is often re-embedded: a rerun of an ingestion, a repeated benchmark. Prism caches them on disk, per model and per text hash:

def embed(self, texts: list[str]) -> np.ndarray:
    keys = [hashlib.md5(t.encode()).hexdigest() for t in texts]
    missing = [t for t, k in zip(texts, keys) if k not in self._cache]
    if missing:
        for text, vector in zip(missing, self._model.embed(missing)):
            self._cache[hashlib.md5(text.encode()).hexdigest()] = np.asarray(vector, dtype=np.float32)
        ...

The cache key includes the model (one file per model): mixing vectors from two models in the same index is a silent and serious error.

Key takeaways

  • A bi-encoder turns each text into a vector; only the ranking of similarities matters.
  • Non-English corpus: a multilingual model, without exception.
  • Dimensions = quality versus cost; quantization and Matryoshka truncation (on models built for it) cut the bill.
  • Respect the model's prefixes and maximum length: their errors are silent.
  • Specialized domain: hybrid first, context next, fine-tuning last; keep model, version and texts so you can re-embed.

Practice

  1. Compute the similarity of « délai de retour » with "return window", then with « délai de livraison » (delivery time): does the model separate language or meaning better?
  2. Run the test bench with PRISM_EMBED_MODEL=BAAI/bge-small-en-v1.5 (an English model): what happens to the English and rephrased questions?
  3. Compute the memory needed for your own data (number of passages × dimensions × 4 bytes).

Resources