AI on Kubernetes · GPU/NPULesson 17 of 20
How a RAG Pipeline Answers From Your Documents
Follow one question through retrieval-augmented generation (RAG): documents → chunks → embeddings → vector DB, then question → same embedding model → search → rerank → prompt → LLM. See why the answer is only as good as the chunks that reach the model.
Indexed chunks0
Retrieved—
Prompt tokens—
Answer—
RequestResponse / reportWatchAppears in a later step
What you will learn
- Why a model needs your documents handed to it at question time
- How chunking and embedding turn documents into searchable vectors
- Nearest-neighbor search, hybrid keyword search and reranking
- Bad chunks, stale indexes and mismatched embedding models
- Filtering by permission before retrieval, not after
When answers go wrong
| Failure | Symptom | Fix |
|---|---|---|
| Bad chunking | The fact exists but is never retrieved, or tables and lists come back cut in half | Split along headings, paragraphs and table rows. Tune size and overlap, and test with real questions |
| Stale index | Answers quote an old version of a document | Re-index on change (events or a frequent CronJob). Delete old chunks by document id when a document changes |
| Embedding model mismatch | Similarity scores look random after an upgrade, or the query fails on vector dimensions | Store the model name with each chunk, and rebuild the whole index when the model changes |
| Exact terms missed | Questions about codes, SKUs or names get unrelated chunks | Add keyword search (BM25) and merge results (hybrid search) |
| Context overflow | Request rejected for length, or the answer ignores later chunks | Count tokens, keep fewer and better chunks after reranking, leave room for the answer |
| No permission filter | A user sees content from documents they cannot open | Store allowed groups per chunk and filter in the vector query |
| Model ignores sources | Answer contradicts the retrieved chunks or cites nothing | Tell the model to answer only from the sources and to say when they are not enough. Measure faithfulness |
Try it yourself
# pgvector in a local PostgreSQL (the pgvector image ships the extension)
docker run -d --name pg -e POSTGRES_PASSWORD=pw -p 5432:5432 pgvector/pgvector:pg17
psql postgresql://postgres:pw@localhost:5432/postgres <<'SQL'
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE chunks (id bigserial PRIMARY KEY, doc_id text, groups text[], body text, embedding vector(3));
INSERT INTO chunks (doc_id, groups, body, embedding) VALUES
('refund', '{all}', 'Opened items: 30 days', '[0.9,0.1,0.0]'),
('salary', '{hr}', 'Salary bands 2026', '[0.1,0.9,0.2]'),
('ship', '{all}', 'Shipping takes 3 days', '[0.2,0.1,0.9]');
CREATE INDEX ON chunks USING hnsw (embedding vector_cosine_ops); -- approximate index
-- <=> is cosine distance (smaller is closer). Filter by the user's groups in the same query.
SELECT doc_id, body, embedding <=> '[0.8,0.2,0.1]' AS distance
FROM chunks WHERE groups && '{all,sales}' ORDER BY distance LIMIT 2;
SQL
# Embeddings from an OpenAI-compatible server (many model servers expose this endpoint)
curl -s http://<embedding-server>/v1/embeddings -H 'Content-Type: application/json' \
-d '{"model":"<embedding-model>","input":"How long is the return window?"}' | jq '.data[0].embedding | length' # vector dimension
Real embeddings have hundreds or thousands of dimensions. The three-number vectors above only show how the query works.
Check yourself
Further reading: pgvector · Qdrant docs · Milvus docs · Lewis et al. 2020, Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks · How an Answer Is Built One Token at a Time · From API Request to Response