See how retrieval becomes an answer.

Follow information through a Basic RAG system, inspect every transformation, and connect each visual step to code.

See → Understand → Interact → Code → Connect

Basic RAG mental model

One connected system, six visible stages.

Use this map first. The guided sections below unpack each stage without losing the full flow.

  1. 01DocumentExternal knowledge enters the system.
  2. 02ChunksThe source becomes retrievable units.
  3. 03EmbeddingsMeaning is represented as vectors.
  4. 04SearchThe closest evidence is ranked.
  5. 05ContextSelected evidence forms the prompt.
  6. 06AnswerThe model responds from evidence.
Phase 01FoundationsWhy external knowledge is needed
02

The problem RAG solves

What happens when an LLM needs information it was never trained on?

Without RAG
User

“How much does a consultation at DentCare cost?”

LLM (Knowledge Cutoff)

“I don't know.” (or presents an invented hallucinated price)

VS
With RAG
Evidence A Consultation price · External evidence provided

“The initial dental consultation costs R$ 250.”

User

“How much does a consultation at DentCare cost?”

LLM (Augmented with Evidence)

“The initial dental consultation costs R$ 250.”

Core Takeaway: RAG (Retrieval-Augmented Generation) gives the model relevant external information before generation.

03

Documents: The External Knowledge Source

RAG starts with unstructured external information. In this interactive guide, we use a Dental Clinic sample document.

Sample Document: DentCare Clinic Information Plain Text Source

DentCare Clinic is open from Monday to Friday, from 8 AM to 6 PM.

Evidence AThe initial dental consultation costs R$ 250.

Dr. Ana is an orthodontics specialist and works on Tuesdays and Thursdays.

Dr. Carlos performs root canal treatments and endodontics procedures.

Cancellations must be made at least 24 hours in advance.

The clinic accepts payments via PIX, credit card, and debit card.

In Python Conceptual equivalent python
document = """
DentCare Clinic is open from Monday to Friday, from 8 AM to 6 PM.
The initial dental consultation costs R$ 250.
Dr. Ana is an orthodontics specialist and works on Tuesdays and Thursdays.
Dr. Carlos performs root canal treatments and endodontics procedures.
Cancellations must be made at least 24 hours in advance.
The clinic accepts payments via PIX, credit card, and debit card.
"""
Phase 02IndexingPrepare knowledge for retrieval
04

Chunking: Splitting Large Documents

Large documents need smaller, manageable pieces to fit into vector search and context windows.

Quick Presets:
140
30

Generated Chunks (0)

Stable evidence identity Evidence A — Consultation price

“The initial dental consultation costs R$ 250.”

Resolving current chunk and retrieval status…
In Python Updated from the controls above python
chunks = split_text(
    document,
    chunk_size=140,
    overlap=30,
)
05

Embeddings: Converting Meaning into Vectors

This browser lesson uses simulated educational feature vectors to demonstrate the retrieval mechanism. It does not run a real embedding model.

Input units Chunks 01–04 Evidence A travels with its chunk
Simulated output [[0.82, …], [0.34, …], …] One vector per chunk

Illustrative 2D Projection

Illustrative: simulated educational feature vectors. Real embeddings normally have hundreds or thousands of dimensions; this visualization teaches the mechanism and does not reproduce a real embedding model.

Scroll horizontally to inspect the full projection →

Select any point above to inspect its simulated vector and illustrative placement

In Python Conceptual equivalent—not executed here python
chunk_embeddings = embed(chunks)
Phase 03RetrievalCompare the question with indexed knowledge
06

Query Embedding: Vectorizing the Question

The user's question must be converted into the exact same vector space so we can compare it to document chunks.

Preset Questions:
Query Text “How much does a consultation cost?”
↓
Simulated Query Feature Vector (Normalized) [0.78, 0.12, -0.45, 0.88, ...]
In Python Updated when the query is embeddedpython
query_embedding = embed(query)
07

Similarity Search: Comparing Vectors

Which chunks point in a similar direction to the query in the simulated educational vector space?

Cosine Similarity

Cosine similarity measures the cosine of the angle θ between two vectors:

-1.0Opposite 0.0Orthogonal 1.0Same direction
Simulated educational vector engine

Ranked Chunks by Dense Cosine Similarity

Select a row to compare its geometry

Geometric cosine comparison

Query ↔ top-ranked chunk

Explore cos(θ) = (u · v) / (||u|| ||v||)
Unit-circle projection
Angle θ 24.5°
Cosine Similarity cos(θ) 0.910
Directional Alignment Strong Alignment
24.5°

Geometry sandbox only — this control does not change retrieval ranking.

cos(θ) = (q · c) / (||q|| ||c||)
In Python Dense cosine retrieval python
scores = cosine_similarity(
    query_embedding,
    chunk_embeddings
)
ranked_chunks = sort_by_score(scores)
08

Top-K Retrieval

Top-K selects the highest-ranked dense results that will enter the context.

3
Selected Chunks 3 of 4
Selected Text 0 chars
Estimated Tokens (≈ chars ÷ 4) ≈0 tokens

Retrieved Chunks Selection (Top K = 3)

In Python Updated from Top K python
results = similarity_search(
    query_embedding,
    chunk_embeddings,
    k=3,
)
Phase 04Augment & GenerateTurn retrieved knowledge into grounded output
09

Context: Assembly of Retrieved Knowledge

Retrieved chunks become context.

Crucial Distinction: The LLM does not search the vector database itself. The Retriever performs the search and places the facts into the Context buffer.

Selected evidence units
Context Buffer Retriever Output
In Python Collect the selected chunk textpython
context = "\n\n".join(
    chunk.text for chunk in results
)
10

Augmented Prompt: Constructing the Payload

Now we augment the prompt by uniting system instructions, retrieved context, and the user's question.

1. SYSTEM INSTRUCTIONS

Answer using only the provided context below. If you don't know, state clearly that information is unavailable.

2. RETRIEVED CONTEXT
3. USER QUESTION

“How much does a consultation cost?”

Single payload Augmented Prompt Instructions + evidence + question
In Python Compose one model input python
prompt = build_prompt(
    context=context,
    query=query,
)
11

Simulated Generation & Grounding

Only now is a deterministic simulated answer produced. It is marked grounded only when its complete canonical evidence is present in the active context.

Prompt→Simulated LLM→Answer
Simulated LLM Output

“The initial dental consultation costs R$ 250.”

Evidence AConsultation price
“The initial dental consultation costs R$ 250.”
Currently in Chunk 02 · Included in context
Source details:
Waiting for complete evidence in the active context.
In Python Generate from the assembled promptpython
answer = llm.generate(prompt)
Phase 05ConnectSee the complete Basic RAG system
12

Complete Basic RAG Pipeline

Now that every concept has been learned individually, see how the Basic RAG components connect.

Scroll horizontally inside a pipeline row when needed →

1. INDEXING (Offline Preparation)
Document Evidence A
Chunking Evidence A · Chunk --
Simulated Chunk Vectors Evidence A
Vector Store Evidence A indexed
Vector Store supplies indexed chunk vectors↓
2. RETRIEVAL & GENERATION (Online Query Flow)
User Query Question targets Evidence A
Simulated Query Vector
Context Buffer Evidence A collected
Prompt Builder Evidence A in prompt
LLM Generation Answer cites Evidence A
Status: Ready to run pipeline simulation.

You now know the components.

Continue in the Lab to experiment, adjust parameters, break assumptions, and inspect the complete pipeline as a debugger.

Future modules will cover Hybrid Retrieval, Reranking, and Evaluation as separate advanced topics.

Build it in Python · Coming next