Direct answer
Retrieval-augmented generation, usually shortened to RAG, means finding relevant material in an approved knowledge base and giving that material to a language model before it answers. The model is still generating language; retrieval simply gives it better evidence. A basic RAG system has two phases: prepare the knowledge base, then answer a query. During preparation, you clean documents, split them into useful chunks, create numerical representations called embeddings, and store those chunks in a searchable index. During a query, you search that index, select the best evidence, and ask the model to answer within that evidence.
RAG does not make a model truthful by itself. Bad source files, broken access rules, weak chunks, stale versions, or poor retrieval can still produce a confident wrong answer. The professional skill is therefore not “connect a PDF to a chatbot.” It is designing and testing the full evidence path from source document to final claim.
Four terms without the fog
An embedding is a list of numbers that represents patterns in a piece of content. Pieces with related meaning tend to be closer in the embedding space. This lets semantic search find “fee refund deadline” when a user asks “How long do I have to get my money back?” even though the wording differs. An embedding is not a fact database, a confidence score, or an explanation of meaning.
A chunk is the unit you retrieve. A 60-page handbook is usually too broad to send as one item, while a single sentence may lack the heading, exception, or table row needed to interpret it. Good chunks preserve one coherent idea and enough local context to stand alone. Headings, sections, FAQ entries, policy clauses, and table rows are often better boundaries than blindly cutting every fixed number of characters.
Retrieval is the search step. Semantic retrieval compares query and chunk embeddings. Keyword retrieval rewards literal overlap. Hybrid retrieval combines both, which is useful when exact codes, product names, dates, or Urdu/English spellings matter alongside meaning. Retrieval returns candidates; it does not prove that a candidate answers the question.
Generation is the final language step. The model receives the question plus selected chunks and produces an answer. A dependable application also passes source identifiers, constrains the answer format, checks that citations point to retrieved material, and permits abstention.
OpenAI's managed vector stores automatically chunk, embed, and index attached files. The current retrieval guide documents default chunk settings, but treat any default as a starting hypothesis rather than a universal optimum. Your document structure and evaluation set should determine the final choice.
Worked build: an admissions policy knowledge base
Imagine a training institute with three approved files:
admissions-2026.md, covering eligibility and deadlines;fees-2026.md, covering payments and refunds;campus-hours.md, covering Karachi and Lahore office times.
A learner asks, “Can an evening-program applicant pay the admission fee in two parts?” A weak system searches the whole corpus and returns a paragraph about general tuition installments. A better system retrieves the admissions-fee clause, preserves its heading and effective date, and notices whether “evening program” is included or excluded.
Prepare each chunk with metadata, not only text:
{
"chunk_id": "fees-2026#installments",
"text": "Admission fee ...",
"document": "fees-2026.md",
"section": "Installments",
"effective_from": "2026-01-01",
"campus": "all",
"audience": "public",
"version": 3
}
Then make the retrieval flow explicit. This provider-neutral pseudocode shows the responsibilities without pretending that a vector database is just a JavaScript array:
async function retrieve(question, user) {
const queryVector = await embed(question);
const filters = {
audience: user.isStaff ? ["public", "staff"] : ["public"],
activeOn: new Date().toISOString().slice(0, 10),
};
const hits = await index.hybridSearch({
query: question,
vector: queryVector,
filters,
limit: 6,
});
return hits.filter((hit) => hit.score >= QUALITY_FLOOR);
}
The important decisions are visible: identity becomes a server-side permission filter; freshness is considered before generation; the result count is bounded; and low-quality hits can be rejected. QUALITY_FLOOR must come from tests, not intuition.
For this corpus, start by chunking each policy section under its heading. Repeat the document title, section name, and effective date in chunk metadata. Keep a table with its header. If a rule and its exception are separated by a page break, combine them. If chunks repeatedly retrieve irrelevant neighbouring clauses, reduce their scope. If answers miss exceptions because the chunks are too narrow, add overlap or restructure by meaning.
A practical design sequence
- Inventory sources. Record owner, authority, intended audience, update cadence, format, and deletion rule for every file.
- Normalize carefully. Remove navigation noise and duplicate footers, but preserve headings, table labels, dates, warnings, and exception language.
- Choose semantic boundaries. Build chunks around complete ideas. Give every chunk a stable ID that survives re-indexing when its meaning has not changed.
- Attach control metadata. Include tenant, role, region, document version, effective dates, and source URL or file reference.
- Index and retrieve. Start with a modest number of candidates. Inspect the actual returned text and scores rather than judging only the final prose.
- Generate from evidence. Require citations and an explicit “insufficient evidence” path.
- Evaluate separately. First ask whether the right chunk appeared. Only then judge whether the answer used it correctly.
Failure cases to recognize
- Garbage ingestion: a scanned PDF produces scrambled text, so retrieval finds nonsense. Add OCR review and reject files that fail extraction checks.
- Chunks without context: “Applications close on 15 August” is retrieved without a program name or year. Carry the heading and effective date.
- Old and new versions collide: both policies rank well, so the model blends them. Mark versions and filter to the active one.
- Exact identifiers disappear: an embedding search misses
FORM-17B. Add keyword or hybrid search. - Permission leakage: a staff salary policy enters a public answer. Enforce access before retrieval; a prompt saying “do not reveal staff files” is not authorization.
- Top result worship: the highest-scoring chunk is related but not sufficient. Test multiple candidates and permit abstention.
- Multilingual mismatch: Roman Urdu queries retrieve weaker English results. Add representative queries to evaluation and consider synonyms or query rewriting that you can test.
🇵🇰 Pakistan Angle
Pakistani knowledge bases often begin as mixed-quality PDFs, photographed notices, WhatsApp exports, spreadsheets, and bilingual text. Design ingestion for that reality. A university circular may contain English policy language, an Urdu heading, and a scanned signature on the same page. A small business may have one official return policy in Google Docs and five contradictory voice notes in a group chat. Decide which source is authoritative before indexing anything.
Keep the learner experience usable on modest connections: retrieve small text chunks, avoid repeatedly uploading large files, and cache safe public content. Test common Roman Urdu phrasing, local abbreviations, and alternate spellings, but do not silently translate a rule and claim the translation is official. Show the original source beside any translation. For regulated, financial, medical, employment, or admissions decisions, route uncertainty to the responsible human instead of turning a related paragraph into a decision.
Hands-on deliverable
Create a small RAG preparation pack for one real or synthetic domain. Use at least five approved documents and produce:
- a source inventory with owner, audience, version, and freshness fields;
- 15–30 meaningful chunks with stable IDs and metadata;
- a one-page chunking rationale explaining boundaries and overlap;
- ten test queries, including an exact identifier, a paraphrase, an unanswerable question, a stale-policy trap, and a Roman Urdu variation;
- a retrieval log showing the top candidates for every query;
- three changes you made after inspecting failures.
Do not include real private customer, student, employee, or patient records. Synthetic examples are enough to prove the engineering skill.
Completion rubric
- I can explain embeddings as similarity representations without calling them facts or understanding.
- Each chunk preserves a coherent idea and traceable source context.
- My metadata supports access, version, and freshness filtering.
- My retrieval tests include keyword, semantic, multilingual, stale, and unanswerable cases.
- I inspected retrieved chunks separately from generated answers.
- I documented at least three failure-driven improvements.
- No secret, personal record, or unauthorized document entered the knowledge base.
Key takeaway: RAG quality is the quality of the evidence pipeline—source authority, chunk design, retrieval controls, and evaluation—not the fluency of the final answer.