Skip to main content
Albero Academy

Embeddings & RAG

Give a model your own knowledge — embeddings, vector search, and retrieval-augmented generation to ground answers in real data.

Chapter 3Generative AI16 min readBeginner friendly
  • #GenAI
  • #RAG
  • #Embeddings

A base model doesn't know your company's documents, your product docs, or anything after its training cutoff. RAG — retrieval-augmented generation — is the standard way to fix that: find the relevant text and hand it to the model as context. It's how most "chat with your docs" products work.

The knowledge problem

You can't retrain a giant model every time your docs change, and you can't paste your entire knowledge base into every prompt (it won't fit the context window, and it's expensive). RAG retrieves only the few passages relevant to each question.

Embeddings

An embedding turns a piece of text into a vector — a list of numbers — that captures its meaning. Texts with similar meaning get similar vectors, even with no words in common ("laptop" and "notebook computer" land close together):

text → vector
# Each text becomes a fixed-length vector (e.g. 1024 numbers)
embed("How much is the data course?") -> [0.02, -0.11, 0.34, ...]
embed("What's the price of the analytics programme?") -> [0.03, -0.09, 0.31, ...]
# ↑ different words, similar meaning → nearby vectors

Vector search

Store all your document chunks' embeddings in a vector database. To answer a question, embed the question and find the nearest chunks by cosine similarity — that's semantic search, matching on meaning rather than keywords:

the retrieval step
# 1. Offline: chunk docs, embed each, store in a vector DB
for chunk in documents:
db.add(embedding=embed(chunk), text=chunk)
# 2. At query time: embed the question, get the closest chunks
question = "What is the refund policy?"
top_chunks = db.search(embed(question), k=3)

RAG — putting it together

Retrieve the relevant chunks, then paste them into the prompt as context and ask the model to answer from them:

retrieval-augmented generation
context = "\n\n".join(top_chunks)
prompt = f'''Answer the question using ONLY the context below.
If the answer isn't there, say you don't know.
Context:
{context}
Question: {question}'''
answer = llm(prompt)

Practice

Sketch a RAG pipeline for an FAQ bot: (1) split the FAQ into chunks, (2) embed and store them, (3) for a user question, retrieve the top 3 chunks, (4) build a grounded prompt. Write the prompt template that forces answers to come only from the retrieved context.

Key takeaways

  1. 01Base models don't know your private or post-cutoff data — RAG supplies it at query time.
  2. 02Embeddings turn text into vectors that capture meaning; similar meaning → nearby vectors.
  3. 03Store chunk embeddings in a vector DB and retrieve by cosine similarity (semantic search).
  4. 04RAG = retrieve relevant chunks, then ask the model to answer from that context.
  5. 05Grounding in real data reduces hallucination and lets you cite sources.