Chapter 3 · Part 2
Build your knowledge base
From here we build one real program. By the end of Chapter 6 the code from these
chapters stacks into a single rag.py you run as a chatbot. This chapter builds what the
chatbot answers from: the knowledge base.
A knowledge base is your documents, split into small chunks and embedded so they're searchable. Chunking matters: retrieve too much and you bury the answer in noise (and blow the model's context); too little and you cut the answer in half. A short paragraph per chunk is a solid default.
Start your rag.py
Create rag.py. Load the embedder and define the Nimbus Notes knowledge base — each
string is one chunk:
from sentence_transformers import SentenceTransformer
embedder = SentenceTransformer("all-MiniLM-L6-v2")
documents = [
"Nimbus Notes was founded in 2021 by Priya Raman and is headquartered in Lisbon, Portugal.",
"The Nimbus Notes free plan includes 3 notebooks and 500 MB of storage.",
"Nimbus Notes Pro costs $6 per month and adds unlimited notebooks, version history, and offline sync.",
"All paid Nimbus Notes plans come with a 30-day money-back guarantee.",
"Nimbus Notes support is available by email at [email protected] and replies within one business day.",
"The Nimbus Notes iOS app launched in March 2024; an Android app is planned but not yet available.",
"All Nimbus Notes data is encrypted at rest and stored in EU data centers.",
]In a real project these chunks would come from splitting your own files — help articles, a PDF, a wiki export — into paragraphs. The rest of the pipeline doesn't care where they came from; it only sees a list of strings.
Embed the knowledge base
Exactly the move from the semantic-search course: encode every chunk into one matrix,
normalized so similarity is a fast dot product later. Add this to rag.py:
doc_embeddings = embedder.encode(documents, normalize_embeddings=True)
print(doc_embeddings.shape) # (7, 384) — 7 chunks, 384 numbers eachRun it to confirm:
python rag.py(7, 384)That matrix is your searchable index — seven facts, each a point in meaning-space. Next we find the ones that match a question.