Learn/Generative AI, RAG & Agents/MODULE 2: RETRIEVAL AUGMENTED GENERATION (RAG)
Lesson 14 of 37 · 2 min read

2.3 Vector Embeddings: Teaching Computers Meaning

To understand how the "retrieval" half of RAG actually works, we need to revisit and deepen our understanding of embeddings from Module 1.

Embeddings, Revisited

Recall: an embedding is a list of numbers (a vector) that represents the meaning of a piece of text in mathematical form.

Pieces of text with similar meanings have similar embeddings; pieces with different meanings have different embeddings.

In Module 1, we used embeddings to represent individual tokens inside an LLM.

But you can also use the same technique to embed whole sentences, paragraphs, or entire documents.

Specialized models, called embedding models, do exactly this.

You give them a chunk of text and they give you back a vector — typically 384, 768, 1536, or 3072 numbers long.

For example, the embedding model might convert:

"The weather in Paris is rainy today."

into a list of 1,536 numbers like:

[0.012, -0.084, 0.331, 0.005, -0.218, ...]

And it might convert:

"It's pouring in Paris."

into:

[0.018, -0.080, 0.328, 0.011, -0.220, ...]

Notice that the two embeddings look very similar, even though the sentences use almost completely different words.

That's the magic: embedding models learn to capture meaning, not just words.

Measuring Similarity

Once you have embeddings, how do you check if two pieces of text are similar?

The most common method is called cosine similarity, which essentially measures the angle between two vectors.

Two vectors pointing in exactly the same direction get a similarity score of 1.

Vectors pointing in opposite directions get -1.

Random vectors get something near 0.

You don't need to do this math yourself — software libraries handle it — but it's helpful to know what's going on.

So the workflow looks like this:

  1. Embed the user's question into a vector.
  2. Find documents whose embeddings are most similar (highest cosine similarity) to the question's embedding.
  3. Those are the most relevant documents.

This is called semantic search: searching by meaning, not by keywords.

Traditional keyword search would miss "It's pouring in Paris" if you searched for "Paris rainy weather." Semantic search finds it.

A Concrete Example

Suppose you have a knowledge base of three documents:

  • Doc A: "Our company's vacation policy allows 20 days of paid time off per year for full-time employees."
  • Doc B: "Expense reports must be submitted within 30 days of incurring the expense."
  • Doc C: "New hires receive two weeks of paid vacation in their first calendar year."

Each gets converted to an embedding vector when added to the system.

Now a user asks: "How much vacation do new employees get?"

This question gets embedded as a vector.

The system compares this vector against the three document vectors.

Doc C is most similar (it's about new hires and vacation).

Doc A is somewhat similar (it's also about vacation, but for established employees).

Doc B is unrelated.

The top match (Doc C, and probably also Doc A) gets pulled out and passed to the LLM along with the question.

The LLM produces an answer like: "New employees receive two weeks of paid vacation in their first calendar year, after which they receive 20 days per year as full-time employees."

That's semantic retrieval in action.

Keep going with a free account

A free account saves your progress across lessons and lets you pick up where you left off.