Learn RAG from the inside out.
See how documents become context, and how context becomes an answer.
The problem RAG solves
What happens when an LLM needs information it was never trained on?
âQuanto custa a consulta na DentCare?â
âI don't know.â (or presents an invented hallucinated price)
âA consulta odontolĂłgica inicial custa R$ 250.â
âQuanto custa a consulta na DentCare?â
âA consulta odontolĂłgica inicial custa R$ 250.â
Documents: The External Knowledge Source
RAG starts with unstructured external information. In this interactive guide, we use a Dental Clinic sample document.
document = """
A clĂnica DentCare funciona de segunda a sexta-feira, das 8h Ă s 18h.
A consulta odontolĂłgica inicial custa R$ 250.
A Dra. Ana é especialista em ortodontia e atende às terças e quintas.
O Dr. Carlos realiza tratamentos de canal e procedimentos de endodontia.
Cancelamentos devem ser feitos com pelo menos 24 horas de antecedĂȘncia.
A clĂnica aceita pagamentos via PIX, cartĂŁo de crĂ©dito e cartĂŁo de dĂ©bito.
"""
Chunking: Splitting Large Documents
Large documents need smaller, manageable pieces to fit into vector search and context windows.
Generated Chunks (0)
âA consulta odontolĂłgica inicial custa R$ 250.â
Resolving current chunk and retrieval statusâŠchunks = split_text(
document,
chunk_size=140,
overlap=30
)
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.
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.
Similarity Search: Comparing Vectors
Which chunks point in a similar direction to the query in the simulated educational vector space?
Ranked Chunks by Dense Cosine Similarity
đ Interactive Geometric Cosine Angle Visualizer
Explore cos(Ξ) = (u · v) / (||u|| ||v||)scores = cosine_similarity(
query_embedding,
chunk_embeddings
)
ranked_chunks = sort_by_score(scores)
Top-K Retrieval
Top-K selects the highest-ranked dense results that will enter the context.
Retrieved Chunks Selection (Top K = 3)
# Keep the K highest cosine-similarity results
retrieved_chunks = top_k(ranked_chunks, k=3)
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.
Augmented Prompt: Constructing the Payload
Now we augment the prompt by uniting system instructions, retrieved context, and the user's question.
Answer using only the provided context below. If you don't know, state clearly that information is unavailable.
âQuanto custa uma consulta?â
prompt = f"""
Answer using only the context below.
Context:
{context}
Question:
{query}
"""
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.
âA consulta odontolĂłgica inicial custa R$ 250.â
Complete Basic RAG Pipeline
Now that every concept has been learned individually, see how the Basic RAG components connect.
You now know the components.
Now break them, tune them, and inspect the complete system in the interactive debugger lab.
Future modules will cover Hybrid Retrieval, Reranking, and Evaluation as separate advanced topics.