Thanh Linh Nguyen

PhD candidate at Trinity College Dublin, The University of Dublin


Thanh Linh Nguyen | PhD candidate at Trinity College Dublin, The University of Dublin

Here are my notes on Retrieval-Augmented Generation (RAG), covering core concepts from embeddings and semantic search to the full RAG pipeline.

Table of Contents

  1. What Are Embeddings?

    1.1. Meaning as Geometry

    1.2. Measuring Similarity

  2. Semantic Representation

    2.1. Why Semantic Search Beats Keyword Search

    2.2. Key Properties of Good Embeddings

  3. The RAG Pipeline

    3.1. Indexing

    3.2. Retrieval

    3.3. Generation

  4. Embedding Models

  5. References


1. What Are Embeddings? [1]

Embeddings are dense numerical vectors that represent text — words, sentences, or entire documents — in a high-dimensional space. Rather than treating words as arbitrary symbols, embeddings encode meaning: semantically similar texts end up geometrically close to each other in that space.

For example, the sentence "The dog chased the ball" might be represented as a vector like:

\[\mathbf{v} = [0.23, -0.81, 0.54, \ldots] \in \mathbb{R}^d\]

where $d$ is typically 384, 768, or 1536 dimensions depending on the model.

The critical property is that distance in vector space corresponds to semantic distance in meaning. Two texts that mean the same thing (even if worded differently) will have vectors that are close together, while unrelated texts will be far apart.

1.1. Meaning as Geometry

The core insight behind embeddings is that semantic similarity = geometric proximity. In the vector space:

  • "car" and "automobile" are near neighbors
  • "king", "queen", "man", and "woman" satisfy: $\text{king} - \text{man} + \text{woman} \approx \text{queen}$
  • Unrelated concepts like "pizza" and "quantum physics" are geometrically distant

This transforms a fuzzy linguistic question — “are these texts similar?” — into a precise mathematical one: “how close are these vectors?”

1.2. Measuring Similarity

The standard metric is cosine similarity, which measures the angle between two vectors rather than their absolute distance:

\[\text{similarity}(\mathbf{A}, \mathbf{B}) = \frac{\mathbf{A} \cdot \mathbf{B}}{|\mathbf{A}| \times |\mathbf{B}|}\]
Score Interpretation
1.0 Identical meaning
0.5 – 0.9 Highly related
0.0 Completely unrelated
-1.0 Opposite meaning

Cosine similarity is preferred over Euclidean distance because it is scale-invariant — it doesn’t matter how long the vectors are, only the direction they point. This makes it robust to variable-length texts.


2. Semantic Representation