Lesson 5 · Module 3: Your AI Operating System on a Private Server
Knowledge bases with RAG: letting agents answer from your own documents
- Why RAG makes an agent stop guessing
- How your documents get split into pieces
- How to run it on your own server with n8n
Why this matters
A customer asks about refunds late at night and an AI agent confidently gives an answer that contradicts the shop's real policy. The agent was never shown the policy, so it guessed. This lesson shows how to let an agent look up answers in your own documents before it replies, so it stops guessing and answers from facts you control.
Key ideas
- RAG (retrieval-augmented generation). Before the model writes a reply, the system searches your documents and passes the few most relevant passages to the model as notes. The model itself is never retrained, so when a policy changes you only update the documents.
- Chunks. A chunk is a small piece of a document, usually a few paragraphs. Chunks are used because the model should receive the one paragraph about refunds, not a forty-page handbook.
- Embeddings. An embedding is a list of numbers that represents the meaning of a text. Texts with similar meaning get numbers that are close together, so "money back" can match "refund policy" even without a shared word.
- Search by closeness. To answer a question, the system turns the question into numbers too, then finds the chunks whose numbers are closest. A tiny example with four chunks and a question:
| Chunk | Score for "How many days do I have to ask for a refund?" |
|---|---|
| Refunds: full refund within 14 days | 0.52 |
| Bookings: cancel 24 hours before | 0.00 |
| Shipping: we ship to the US, UK, Canada | 0.00 |
The refund chunk wins, so it is the one sent to the model.
- Keep the index fresh. Indexing is the one-time preparation of chunks and embeddings. When a document changes, its old chunks must be replaced, or the agent keeps quoting outdated facts.
Try it before you watch
A shop changes its refund period from 14 days to 30 days. Its old refund chunk is still in the database. What will the agent most likely answer, and what should you do?
Show answerHide answer
The agent will most likely still answer "14 days", because the old chunk still wins the search. Replace that document's old chunks with new ones when the file changes. This shows that an index must be refreshed, not just added to.
Words to know
- Vector database: a database that stores embeddings and can quickly find the ones closest to a given vector.
- Embedding model: a model that turns a piece of text into a list of numbers that capture its meaning.
- Cosine similarity: a score from 0 to 1 that measures how closely two lists of numbers point the same way, used to rank chunks.
- Indexing: the one-time preparation of splitting documents into chunks, turning them into embeddings and saving them.
- Qdrant and pgvector: Qdrant is a standalone vector database; pgvector is an add-on that lets the Postgres database store and search embeddings.
What to watch for
- Watch how the shop owner's real refund policy is compared with the agent's confident answer, and where the gap comes from.
- Notice how the chunks are split and how the question finds the refund chunk, even when the words differ.
- Pay attention to the rule that the agent must answer only from the notes and say "I don't know" when the answer is missing.
Learn it with your tutor on the live board
Key points from the video
The agent that made up a policy
- A customer asks about refunds at 11 pm
- The AI agent answers fast and sounds sure
- The answer is wrong: it never saw the policy
layout: wide
parts:
shop: {window: refunds.py, holds: [], code: ["REFUND_DAYS = 14", "# real policy: fourteen days"]}
ask: {chat: "Can I still get a refund after ten days?", in: shop, reply: "Yes, you have thirty days."}
steps:
- at: "skincare shop"
type: shop
- at: "messages her shop"
send: ask
- at: "sounding confident"
highlight: ask
- at: "real policy is fourteen days"
highlight: shop
- at: "a promise she never made"
note: "A promise she never made"
- at: "wasn't lying on purpose"
note: "Not lying, just filling a gap"
- at: "typical answer from the internet"
highlight: ask
- at: "read Sara's own documents"
highlight: shop What you will learn
- Why RAG makes an agent stop guessing
- How your documents get split into pieces
- How to run it on your own server with n8n
layout: side
parts:
lesson: {chip: Lesson, sub: "Three things"}
steps:
- at: "three things"
highlight: lesson
- at: "stop guessing"
spokes: {of: lesson, items: [RAG, Stop guessing]}
- at: "small pieces"
spokes: {of: lesson, items: [Documents, Pieces]}
- at: "using n8n"
spokes: {of: lesson, items: [Server, n8n]}
- at: "No maths background"
note: "No maths needed"
- at: "tiny working example"
note: "A small Python example" RAG is an open-book exam
- RAG: Retrieval-Augmented Generation
- Retrieval: search your documents first
- Generation: the model writes the answer from them
- The model's memory stays the same; its notes change
layout: wide
parts:
model: {chip: Model, sub: "Writes the answer"}
book: {chip: Book, sub: "Your documents"}
desk: {box: EXAM DESK, holds: [model, book]}
steps:
- at: "stands for"
spokes: {of: model, items: [Retrieval, Generation]}
- at: "think of an open-book exam"
zoom_out: desk
- at: "misremember a detail"
note: "A student might misremember"
- at: "Give them the right book"
highlight: book
- at: "look it up before writing"
note: "Look it up before writing"
- at: "does the same for your agent"
highlight: model
- at: "search your documents"
spokes: {of: desk, items: [Search, Passages]}
- at: "writes the reply"
highlight: model
- at: "never retrain the model"
note: "The model never changes"
- at: "change the book on the desk"
highlight: book Step one: prepare the book
- Collect FAQs, price lists, policies, past emails
- Split each document into chunks of a few paragraphs
- Turn every chunk into an embedding
- Store chunks and embeddings in a vector database
layout: side
parts:
book: {chip: Book, sub: "Your own documents"}
steps:
- at: "prepare the book once"
highlight: book
- at: "Gather the documents"
vortex: {into: book, words: [FAQ, price list, policies, email]}
- at: "split them into chunks"
spokes: {of: book, items: [Chunks, Few paragraphs]}
- at: "Why chunks"
highlight: book
- at: "forty-page handbook"
vortex: {into: book, words: [paragraph, handbook, refunds, booking, answer, customer]}
- at: "turned into an embedding"
spokes: {of: book, items: [Embedding, Vector database]}
- at: "called indexing"
spokes: {of: book, items: [Indexing, Rerun]}
- at: "whenever a document changes"
highlight: book Embeddings: an address for meaning
- An embedding is a list of numbers for a text
- Similar meaning gives nearby numbers
- "Money back" lands near "refund policy"
- Search = find the chunks closest to the question
kind: scatter
title: Texts placed by meaning
x_label: Meaning, first part
y_label: Meaning, second part
data: [[1, 8], [2, 9], [1.5, 7.2], [2.5, 8.1], [7, 2], [8, 3], [7.5, 1.5], [8.5, 2.6], [5, 5]]
steps:
- at: "list of numbers"
show: all
note: "Each dot is one text"
- at: "Books about refunds"
note: "Refund texts sit together"
- at: "books about shipping"
note: "Shipping texts sit apart"
- at: "Texts with similar meaning"
note: "Similar meaning, close dots"
- at: "lands right next to"
data: [[1, 8], [2, 9], [1.5, 7.2], [2.5, 8.1], [7, 2], [8, 3], [7.5, 1.5], [8.5, 2.6], [1.8, 8.4]]
note: "Money back lands near refunds"
- at: "never uses the word refund"
note: "No shared word needed"
- at: "how does the search work"
note: "Search finds the nearest dots"
- at: "closest to the question"
note: "Closest chunks win"
- at: "hands those to the model"
note: "Top chunks go to the model" A tiny retrieval in Python
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
chunks = [
"Refunds: full refund within 14 days of purchase.",
"Shipping: we ship to the US, UK and Canada.",
"Bookings: cancel at least 24 hours before.",
"Invoices are emailed on the 1st of each month.",
]
question = "How many days do I have to ask for a refund?"
vec = TfidfVectorizer(stop_words="english")
matrix = vec.fit_transform(chunks + [question])
scores = cosine_similarity(matrix[-1], matrix[:-1])[0]
for score, text in sorted(zip(scores, chunks), reverse=True):
print(f"{score:.2f} {text}") The answering flow on your server
layout: wide
direction: LR
components:
user: {kind: user, label: User}
wf: {kind: worker, label: n8n workflow}
emb: {kind: model, label: Embedding model}
vdb: {kind: database, label: Qdrant}
llm: {kind: llm, label: Chat model}
zones:
- {label: Your VPS, contains: [wf, emb, vdb]}
links: [user->wf, wf->emb, wf->vdb, wf->llm]
steps:
- at: "the full answering flow"
show: all
- at: "A customer sends a refund question"
flow: ["user>wf: question"]
- at: "passes the question to an embedding model"
flow: ["wf>emb: embedding", "emb>wf: vector"]
- at: "searches Qdrant with that vector"
flow: ["wf>vdb: search", "vdb>wf: top three chunks"]
- at: "sends those chunks plus the question"
flow: ["wf>llm: chunks"]
- at: "answer only from these notes"
highlight: llm
- at: "stops the agent from guessing"
flow: ["llm>wf", "wf>user"] Qdrant: the vector database
- Open-source vector database, one Docker container
- Runs on the same VPS as n8n
- Already on Postgres? pgvector works too
- n8n has ready-made vector store nodes
layout: side
parts:
qd: {chip: Qdrant, sub: "Vector database"}
pg: {chip: Postgres, sub: "pgvector adds search"}
vps: {box: YOUR SERVER, holds: [qd, pg]}
steps:
- at: "a good default is Qdrant"
highlight: qd
- at: "single Docker container"
spokes: {of: qd, items: [Docker, Same server]}
- at: "never leave your server"
note: "Documents stay on your server"
- at: "If you use Postgres"
zoom_out: vps
highlight: pg
- at: "pgvector extension adds vector search"
spokes: {of: pg, items: [pgvector, Vector search]}
- at: "one less service"
note: "One less service to run"
- at: "n8n ships with vector store nodes"
highlight: vps
spokes: {of: vps, items: [n8n nodes, Dragging nodes]}
- at: "pasting connection details"
note: "Paste connection details" Example: a hair salon's knowledge base
- Price list: one chunk per service
- Policy binder: one chunk per section
- Customer asks: find the cancellation chunk, answer, hand over
layout: wide
parts:
wall: {chip: Price list, sub: "One chunk per service"}
binder: {chip: Binder, sub: "One chunk per section"}
phone: {chip: Phone, sub: "Customer questions"}
salon: {box: SALON, holds: [wall, binder, phone]}
steps:
- at: "hair salon"
highlight: salon
- at: "price list on the wall"
highlight: wall
- at: "each service and its price"
note: "One chunk per service"
- at: "colour treatment"
note: "Finds the colour line"
- at: "policy binder on the front desk"
highlight: binder
- at: "each section a chunk"
note: "One chunk per section"
- at: "where customers ask things"
highlight: phone
- at: "move my Friday booking"
note: "Can I move my Friday booking?"
- at: "retrieves the cancellation chunk"
spokes: {of: salon, items: [Cancellation, Booking]}
- at: "twenty-four-hour rule"
note: "The real twenty-four-hour rule"
- at: "hands over to the booking workflow"
note: "Hands over to booking" Common mistake: a stale book
- You change a price but the old chunks stay
- Old chunks stay in the database and win searches
- Fix: delete the file's old chunks, then index the new version
- Store a source name and date with each chunk
layout: side
parts:
old: {chip: Old chunks, sub: "Last year's prices"}
fresh: {chip: New chunks, sub: "This year's prices"}
db: {box: VECTOR DB, holds: [old, fresh]}
steps:
- at: "the mistake that bites almost everyone"
highlight: db
- at: "raises its prices"
note: "The price list changes"
- at: "still sitting in the vector database"
highlight: old
- at: "quoting last year's prices"
spokes: {of: db, items: [Last year, Full confidence]}
- at: "The fix is to replace"
highlight: fresh
- at: "Save the source file name"
note: "Source name and date"
- at: "delete that file's old chunks"
spokes: {of: db, items: [Delete old, Index new]}
- at: "whenever the file changes"
highlight: fresh
- at: "shows you where an answer came from"
note: "Source shows where it came from" Recap
- RAG = search your documents, then answer from them
- Chunks plus embeddings make search by meaning work
- Keep the index fresh and allow "I don't know"
layout: wide
parts:
docs: {chip: Documents, sub: "Your business facts"}
idx: {chip: Index, sub: "Chunks and embeddings"}
ag: {chip: Agent, sub: "Answers from notes"}
srv: {box: YOUR SERVER, holds: [docs, idx, ag]}
steps:
- at: "Let's pull it together"
highlight: srv
- at: "open-book exam"
highlight: ag
- at: "searches your documents first"
highlight: docs
- at: "answers from what it found"
highlight: ag
- at: "Chunks and embeddings"
spokes: {of: idx, items: [Chunks, Embeddings]}
- at: "money back finds your refund policy"
note: "Money back finds refund policy"
- at: "the index stays fresh"
highlight: idx
- at: "say I don't know"
note: "I don't know, not guessing"
- at: "real facts"
note: "Real facts from your documents"
- at: "on a server you control"
highlight: srv Your exercise
- Pick one real business: yours or a client's
- Write 10 questions customers really ask
- Gather the documents that answer them
- Split them into chunks and mark which chunk answers each question
layout: side
parts:
ex: {chip: Exercise, sub: "Your first knowledge base"}
steps:
- at: "raw material"
highlight: ex
- at: "Pick one real business"
note: "One real business"
- at: "Write down ten questions"
spokes: {of: ex, items: [Ten questions, Messages]}
- at: "gather the documents"
spokes: {of: ex, items: [Documents, Chunks]}
- at: "note which chunk holds the answer"
note: "Note which chunk answers each question"
- at: "a gap in the documents"
note: "A gap is valuable too"
- at: "keep this server healthy"
note: "Backups, updates, monitoring" Transcript
The agent that made up a policy. Picture Sara, a shop owner who runs a small online skincare shop. At eleven at night a customer messages her shop: can I still get a refund after ten days? Sara's AI agent replies in seconds, sounding confident: yes, you have thirty days. But Sara's real policy is fourteen days. Now she either honours a promise she never made or she upsets a customer. The model wasn't lying on purpose. It simply never saw her policy, so it filled the gap with a typical answer from the internet. Today we fix that by letting the agent read Sara's own documents before it replies.
What you will learn. By the end of this lesson you'll be able to do three things. First, explain what RAG is and why it makes an agent stop guessing. Second, describe how your business documents get split into small pieces and turned into something a computer can search by meaning. Third, wire the whole thing together on your own private server, using n8n and a vector database running in Docker. No maths background is needed, and you'll see a tiny working example in Python along the way.
RAG is an open-book exam. RAG stands for retrieval-augmented generation. That's a mouthful, so think of an open-book exam. A student who studied years ago might misremember a detail. Give them the right book and they look it up before writing. RAG does the same for your agent. Retrieval means: before answering, search your documents and pull out the few passages that relate to the question. Generation means: the language model writes the reply using those passages as its notes. You never retrain the model. When your policy changes, you just change the book on the desk.
Step one: prepare the book. Before your agent can look anything up, you prepare the book once. Gather the documents customers actually ask about: your FAQ, price list, refund and booking policies, even good past email replies. Then split them into chunks, which are small pieces of a few paragraphs each. Why chunks? Because you want to hand the model the one paragraph about refunds, not your whole forty-page handbook. Each chunk is then turned into an embedding, which we'll explain next, and saved in a vector database. This preparation step is called indexing, and you rerun it whenever a document changes.
Embeddings: an address for meaning. An embedding is a list of numbers that an embedding model produces for a piece of text. Think of it as a shelf address in a library. Books about refunds sit together on one shelf, books about shipping on another. Texts with similar meaning get addresses close together. When a customer asks, can I get my money back, the question gets its own address, and it lands right next to the refunds shelf, even though it never uses the word refund. So how does the search work? It simply finds the chunks whose addresses are closest to the question's address, and hands those to the model.
A tiny retrieval in Python. Here's the retrieval step in miniature. You'll never write this code yourself, because n8n does it for you, but it shows what happens inside. At the top are four chunks from Sara's shop, and below them one customer question. To keep it runnable anywhere, we use a simple word-counting method called TF-IDF as a stand-in for embeddings. Then cosine similarity gives each chunk a score from zero to one for how closely it matches the question. Look at the output: the refund chunk scores highest and the others score zero. A real embedding model does the same job but matches meaning, not just words, so it would also catch money back. The top chunks are what we send to the model.
The answering flow on your server. Now let's see the full answering flow running on your own server. A customer sends a refund question through your website chat. Your n8n workflow passes the question to an embedding model, which returns its vector. The workflow searches Qdrant with that vector and gets back the top three chunks. Then it sends those chunks plus the question to the chat model, with one firm instruction: answer only from these notes, and if the answer isn't there, say you don't know and offer a human. That single sentence is what stops the agent from guessing.
Qdrant: the vector database. For the vector database, a good default is Qdrant. It's open source, it installs as a single Docker container on the same server where n8n runs, and your documents never leave your server. If you use Postgres, a common database, there's a shortcut: the pgvector extension adds vector search right inside that same database, so you run one less service. Either way, n8n ships with vector store nodes for both, so building the indexing workflow and the answering workflow is mostly dragging nodes and pasting connection details.
Example: a hair salon's knowledge base. Let's apply this to a client, a small hair salon that wants an assistant for bookings. Start with the price list on the wall: each service and its price becomes its own chunk, so a question about colour treatment finds the colour line. Next, the policy binder on the front desk: cancellation rules, deposits, opening hours, each section a chunk. Finally, the phone, where customers ask things like, can I move my Friday booking? The agent retrieves the cancellation chunk, answers with the real twenty-four-hour rule, and hands over to the booking workflow.
Common mistake: a stale book. Here's the mistake that bites almost everyone. The salon raises its prices and someone edits the price list document. The old chunks are still sitting in the vector database, and simply indexing the file again usually adds new chunks next to them, so the agent can keep quoting last year's prices with full confidence. The fix is to replace, not add. Save the source file name and date with each chunk. Then an n8n workflow can delete that file's old chunks and index the new version, whenever the file changes or simply every night. The source name also shows you where an answer came from.
Recap. Let's pull it together. RAG is an open-book exam for your agent: it searches your documents first, then answers from what it found. Chunks and embeddings are what make search by meaning work, so a question about money back finds your refund policy. And the system stays trustworthy only if the index stays fresh and the agent is allowed to say I don't know instead of guessing. With this in place, your agents answer from the business's real facts, on a server you control.
Your exercise. Before the next lesson, build the raw material for your first knowledge base. Pick one real business, your own or a client's. Write down ten questions customers genuinely ask, from messages or emails. Then gather the documents that answer them and split those documents into chunks of a few paragraphs. For each question, note which chunk holds the answer. If a question has no matching chunk, you've just found a gap in the documents, and that's valuable too. Next time we'll keep this server healthy with backups, updates and monitoring.
Explore the animations
The animations from the video, exact and live: play them, pause, drag the slider or jump to a step at your own pace.
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Answer every question, then check. 70% passes; you can retry as often as you like.
Lesson summary
- Reading
- Video watched
- Quiz passed (70%)
- RAG = search your documents, then answer from them
- Chunks plus embeddings make search by meaning work
- Keep the index fresh and allow "I don't know"