Natural Language Processing & Generative AI

Chapter 10
Retrieval-Augmented Generation and Knowledge Systems

Advance Diploma in Artificial Intelligence and Machine Learning
Natural Language Processing & Generative AI   •   Advance Diploma

Retrieval-Augmented Generation and Knowledge Systems

Introduction

Large language models (LLMs) generate fluent text by predicting likely continuations from patterns learned during training. Their knowledge, however, may be incomplete, outdated, difficult to verify, or unsuitable for private organizational data. Retrieval-augmented generation (RAG) addresses this limitation by connecting a generative model to an external knowledge source. Instead of relying only on model parameters, a RAG system retrieves relevant evidence and supplies it to the model at generation time.

A typical RAG pipeline performs five broad activities:

  1. ingest and prepare source documents;
  2. divide documents into searchable chunks;
  3. represent chunks as embeddings and store them in a retrieval system;
  4. retrieve and optionally rerank relevant evidence;
  5. generate a grounded answer with citations and appropriate safeguards.

RAG is therefore not merely a prompt-engineering technique. It is an information-retrieval and knowledge-engineering system whose quality depends on data preparation, indexing, search, ranking, context construction, generation, evaluation, and governance.

Learning Objectives

By the end of this chapter, a learner should be able to:

  • explain the architecture and purpose of a RAG system;
  • design document-ingestion and chunking strategies;
  • distinguish sparse, dense, hybrid, and knowledge-graph retrieval;
  • explain embeddings, vector databases, metadata filtering, and reranking;
  • construct prompts that ground an LLM in retrieved evidence;
  • design citation and provenance mechanisms;
  • evaluate retrieval and generation quality;
  • identify security, privacy, ethical, and operational risks;
  • propose a practical RAG solution for an organizational use case.

1. The RAG Paradigm

In a conventional LLM application, a user question is sent directly to a model. In RAG, the question is first used to search an external corpus. The retrieved passages are then included in the model input. The model is instructed to answer using those passages and to acknowledge insufficient evidence.

A simplified pipeline is:

Retrieval augmented generation architecture DocumentsPDF, web, database Ingestionclean, parse, chunk Indexembeddings + metadata User queryquestion + filters Retrieversearch + rerank LLMgrounded response Answerwith citations retrieved context
Retrieval augmented generation architecture
Figure: Retrieval augmented generation architecture

Figure: A RAG system combines an indexed knowledge base with query-time retrieval and grounded generation.

The central design principle is separation of responsibilities. The retrieval component finds evidence; the language model interprets and communicates it. A powerful generator cannot compensate for consistently irrelevant retrieval, and a strong index cannot guarantee that a model will use evidence correctly.

2. Document Ingestion and Knowledge Preparation

2.1 Source acquisition

Sources may include policy manuals, technical documentation, websites, emails, databases, tickets, research papers, and structured records. Before indexing, each source should be assigned metadata such as title, author, publication date, version, department, access classification, URL, and document identifier.

Source quality matters. A duplicated, obsolete, or unauthorized document can produce a technically fluent but operationally dangerous answer. In regulated settings, the ingestion process should preserve the original file, checksum, extraction timestamp, and transformation history.

2.2 Parsing and normalization

Ingestion commonly includes:

  • optical character recognition for scanned pages;
  • extraction of paragraphs, headings, tables, lists, and captions;
  • removal of navigation menus, repeated headers, and boilerplate;
  • normalization of encoding, whitespace, and quotation marks;
  • preservation of page numbers and section locations;
  • detection of language and document version.

Tables require special treatment. Flattening a table into arbitrary text may destroy relationships between headers and values. A useful representation keeps the table title, column headings, row labels, units, and source location together.

2.3 Chunking

An embedding is usually created for a chunk rather than an entire document. A chunk should be large enough to express a coherent idea but small enough to be retrieved precisely and fit within the model context window.

Common strategies include:

  • fixed-length chunking: split by a character or token limit;
  • recursive chunking: split first at headings, then paragraphs, then sentences;
  • semantic chunking: group text whose meaning is sufficiently coherent;
  • structure-aware chunking: retain document sections, table rows, code blocks, or legal clauses;
  • parent-child chunking: retrieve a small child passage but provide its larger parent section to the generator.

Overlap can reduce boundary loss. If a chunk has length L and adjacent chunks overlap by O tokens, the next chunk begins approximately at position L − O. Excessive overlap increases storage, redundancy, and the probability that search results contain repeated information.

Chunk size should be selected experimentally. Short chunks improve precision but may omit definitions or conditions. Long chunks provide context but may dilute similarity and consume the generation budget. A practical system often begins with structure-aware chunks and evaluates several sizes using representative questions.

3. Embeddings and Vector Search

An embedding model maps text to a numerical vector. Semantically related texts tend to occupy nearby regions of the embedding space. Let a document chunk be represented by vector d and a query by vector q, both in an n-dimensional space.

Cosine similarity is commonly defined as:

cos(q,d) = (q · d) / (||q|| ||d||)

Here, q · d is the dot product and ||q|| and ||d|| are the Euclidean norms. The measure is dimensionless and is defined for non-zero vectors. If vectors are normalized to unit length, cosine similarity equals their dot product.

Euclidean distance is:

d(q,d) = √(∑ᵢ (qᵢ − dᵢ)²)

where qᵢ and dᵢ are corresponding coordinates. Distance and similarity must not be confused: lower Euclidean distance indicates greater proximity, whereas higher cosine similarity generally indicates greater alignment.

Embedding models have limitations. They may underrepresent rare terminology, confuse negation, encode social bias, or perform differently across languages and domains. Domain-specific evaluation is therefore more reliable than assuming that a general embedding model will work equally well for legal, biomedical, financial, and technical material.

Vector databases

A vector database stores embeddings and associated payloads such as text, document identifiers, timestamps, permissions, and source locations. It normally supports approximate nearest-neighbor search. Approximate methods trade a small amount of exactness for lower latency and greater scale. Important operational considerations include index type, memory use, update frequency, filtering support, replication, backup, and access control.

A retrieval record should contain enough provenance to reconstruct the evidence shown to the user. At minimum, this commonly includes chunk text, source identifier, page or section, version, and access labels.

4. Retrieval Models

4.1 Sparse retrieval

Sparse retrieval represents documents using terms and their weights. BM25 is a widely used ranking function. One common form is:

BM25(q,d) = ∑ₜ∈q IDF(t) × [ f(t,d)(k₁ + 1) / (f(t,d) + k₁(1 − b + b |d|/avgdl)) ]

Here, f(t,d) is the frequency of term t in document d, |d| is document length, avgdl is average document length, k₁ and b are tuning parameters, and IDF(t) is the inverse-document-frequency weight. Exact implementation details vary. BM25 is especially useful for product codes, names, legal phrases, error messages, and other terms for which exact matching matters.

4.2 Dense retrieval

Dense retrieval compares query and document embeddings. It can identify paraphrases and conceptually related passages even when they do not share exact words. However, it may miss a precise identifier or rare acronym. Dense search can also retrieve passages that are conceptually similar but factually unsuitable.

4.3 Hybrid retrieval

Hybrid retrieval combines sparse and dense results. One method is reciprocal rank fusion (RRF):

RRF(d) = ∑ᵣ∈R 1 / (k + rankᵣ(d))

Here, R is the set of retrieval systems, rankᵣ(d) is the rank of document d in system r, and k is a positive constant that reduces the influence of very high ranks. RRF combines rankings without requiring the scores from different systems to be calibrated to the same scale.

Hybrid retrieval is often appropriate for enterprise search because employee questions may combine natural language with exact identifiers, policy names, dates, or technical codes.

4.4 Metadata and permission filtering

Retrieval should apply access controls before evidence reaches the model. Filters may include tenant, role, region, confidentiality level, document status, and effective date. Authorization must not be implemented only in the prompt; prompt instructions cannot reliably prevent a retriever from exposing unauthorized text.

5. Reranking and Context Construction

Initial retrieval commonly produces a broad candidate set. A reranker then examines each query–passage pair using a cross-encoder or another scoring model. Rerankers are often more accurate than embedding similarity because they jointly attend to the query and passage, but they are computationally more expensive.

A typical sequence is:

  1. retrieve perhaps 20–100 candidates using sparse, dense, or hybrid search;
  2. remove duplicates and unauthorized records;
  3. rerank candidates;
  4. select a small set that covers different relevant sources;
  5. assemble context in a clear, labeled format.

Context construction should preserve evidence boundaries. Each passage can be labeled with a citation key, document title, version, page, and access date. The system should avoid silently merging contradictory versions. If sources conflict, the model should be instructed to report the conflict and identify the relevant dates or authorities.

The context window is a constrained resource. Adding more passages does not necessarily improve accuracy; irrelevant material can distract the model. Diversity-aware selection can prevent five nearly identical chunks from displacing a distinct but important source.

6. Grounded Generation and Citations

Grounding means constraining the answer to retrieved evidence and making unsupported claims less likely. A grounding instruction should state that the model must:

  • answer from the supplied sources;
  • distinguish evidence from inference;
  • avoid inventing facts or citations;
  • state when the evidence is insufficient;
  • preserve uncertainty and conditions;
  • cite the passage supporting each substantive claim.

A citation system should be generated from structured retrieval metadata rather than fabricated by the model. For example, a citation key such as [S1] can map to a source title, version, page, and stable link in the user interface.

Grounding does not guarantee truth. A retrieved source may be wrong, obsolete, malicious, or internally contradictory. Consequently, high-risk applications may require human approval, source-authority ranking, answer validation, and explicit refusal rules.

A useful conceptual distinction is:

  • faithfulness: the answer is supported by the supplied evidence;
  • relevance: the answer addresses the user’s question;
  • retrieval quality: the evidence set contains the information needed to answer;
  • citation correctness: citations actually support the associated claims.

These properties should be evaluated separately.

7. Knowledge Graphs and Graph-Augmented RAG

A knowledge graph represents entities as nodes and relationships as edges. For example, a graph may represent:

  • a product as being manufactured at a facility;
  • a policy as applying to a department;
  • a medication as having a contraindication;
  • a machine as being connected to a sensor.

Graph retrieval is valuable when questions require multi-hop reasoning, explicit relationships, or consistent entity resolution. A query such as “Which suppliers support facilities affected by the revised safety policy?” may require traversing suppliers, facilities, incidents, and policies.

Graph construction includes entity extraction, relation extraction, normalization, disambiguation, provenance assignment, and temporal versioning. Errors in entity resolution can connect facts to the wrong person, product, or organization.

Graph RAG may combine:

  1. vector search for descriptive passages;
  2. keyword search for exact identifiers;
  3. graph traversal for relationships;
  4. a generator that receives both textual evidence and structured triples.

Graphs are not automatically more truthful than documents. Every node and edge should retain provenance, confidence, and validity dates. A graph without source traceability can make errors appear authoritative.

8. Evaluation of RAG Systems

Evaluation should examine the complete pipeline rather than only the final prose.

Retrieval metrics

  • Recall@k: proportion of questions for which at least one relevant item appears in the top k results.
  • Precision@k: proportion of the top k results judged relevant.
  • MRR: mean reciprocal rank of the first relevant result.
  • nDCG: a ranking metric that accounts for graded relevance and position.

For a set of queries Q, recall at k can be expressed as:

Recall@k = (number of queries with a relevant result in top k) / |Q|

This formula assumes binary relevance and a defined evaluation set. If several relevant documents exist, the denominator and judgment protocol must be specified carefully.

Generation and system metrics

  • faithfulness or groundedness: whether claims follow from retrieved evidence;
  • answer relevance: whether the response satisfies the question;
  • citation precision: proportion of citations that support their claims;
  • citation completeness: proportion of important claims that are cited;
  • abstention quality: whether the system declines when evidence is inadequate;
  • latency, cost, throughput, and availability.

Human evaluation remains important for ambiguity, conflicting sources, nuanced reasoning, and harmful outcomes. Test sets should include ordinary questions, adversarial prompts, misspellings, outdated documents, permission boundaries, and questions whose answer is absent from the corpus.

9. Worked Design Example

Suppose an organization wants an assistant for internal equipment-maintenance procedures. Its corpus contains 1,200 manuals and service bulletins. A user asks: “What inspection is required after replacing the hydraulic pump on Model X7?”

Ingestion. Manuals are parsed, headings and page numbers preserved, and service bulletins assigned effective dates. Obsolete manuals are retained for audit but marked inactive for default retrieval.

Chunking. Procedure sections are split at headings and numbered steps. Each chunk stores model, component, revision, page, and effective date. A step is not separated from its safety warning.

Indexing. The system uses dense embeddings for paraphrased questions and BM25 for “X7” and part numbers. Both indexes retain the same permission and revision metadata.

Retrieval. The query is searched through both methods. Results are filtered to maintenance documents visible to the user and to the current model revision. A reranker prioritizes passages mentioning pump replacement and post-repair inspection.

Generation. The model receives three passages labeled [S1]–[S3]. It is instructed to list the required inspection, mention safety conditions, and cite every procedure step. If the sources disagree, it must report the discrepancy instead of selecting a passage silently.

Validation. An answer validator checks that cited page numbers exist and that the cited passages contain the relevant terms. A maintenance supervisor reviews high-risk recommendations before deployment.

The important lesson is that the model is only one component. Accurate answers depend on revision control, metadata, retrieval, evidence selection, and procedural governance.

10. Implementation Blueprint

A production implementation can be organized into the following layers:

LayerMain responsibilityTypical controls
Source layerAcquire authoritative dataownership, versioning, checksums
Transformation layerParse, clean, and chunkextraction tests, table handling
Index layerStore searchable representationssparse/dense indexes, metadata
Retrieval layerFind and rank evidencehybrid search, filters, reranking
Generation layerProduce grounded responsesprompt policy, citations, abstention
Evaluation layerMeasure quality and riskgolden set, red-team tests, monitoring
Governance layerControl use and accountabilityaudit logs, privacy, human review

A robust development process begins with a small, representative evaluation set. Engineers should inspect failed cases manually and determine whether the cause was missing data, bad parsing, poor chunking, inadequate retrieval, incorrect ranking, or generation failure. Changing the LLM may not solve a retrieval or data-governance problem.

11. Challenges, Ethics, and Governance

Data quality and staleness

RAG can retrieve stale information with high confidence. Effective dates, document status, and scheduled re-indexing are essential. Organizations should define who owns updates and how quickly changes become searchable.

Privacy and security

Documents may contain personal, financial, health, or confidential information. Systems should apply least-privilege access, encryption, tenant isolation, retention policies, and auditable logs. Prompt injection can occur when a retrieved document contains instructions aimed at manipulating the model. Retrieved text must be treated as untrusted data, not as a higher-priority system instruction.

Copyright and licensing

Ingesting and reproducing documents may be subject to copyright, contractual, or database licensing restrictions. Citation and access controls do not automatically resolve those obligations.

Bias and unequal coverage

A corpus may overrepresent certain populations, languages, or institutional viewpoints. Missing evidence can create systematic under-service. Evaluation should examine language coverage and outcomes across relevant user groups.

Automation risk

A fluent answer can encourage overreliance. High-impact domains such as healthcare, credit, employment, law, and safety require risk-based controls, transparent limitations, human oversight, and escalation paths. The appropriate response may be a refusal, a request for clarification, or a referral to a qualified professional.

12. Contemporary Developments and Best Practices

Current RAG systems increasingly use query rewriting, multi-query retrieval, agentic planning, multimodal indexing, structured output, and continual evaluation. Query rewriting can improve recall but may distort user intent, so the original query should be retained for auditing. Multimodal systems can retrieve figures, scanned forms, tables, and diagrams, but visual extraction must be evaluated separately from text extraction.

Recommended practices include:

  1. start with authoritative, well-governed sources;
  2. preserve provenance at every transformation;
  3. use hybrid retrieval when exact terms and semantic similarity both matter;
  4. apply authorization before context assembly;
  5. rerank a manageable candidate set;
  6. cite claims using system-generated source metadata;
  7. design explicit abstention and conflict-handling behavior;
  8. evaluate retrieval and generation independently;
  9. monitor latency, cost, drift, and failure categories;
  10. maintain human review for high-consequence decisions.

Chapter Summary

Retrieval-augmented generation connects language models to external knowledge through document preparation, chunking, embeddings, search, reranking, context construction, and grounded generation. Sparse retrieval is strong for exact terms, dense retrieval captures semantic similarity, and hybrid retrieval combines their advantages. Vector databases support scalable similarity search, while metadata filters enforce relevance, time, and authorization constraints. Reranking improves candidate ordering, and citations provide provenance when generated from reliable retrieval records. Knowledge graphs extend RAG to entities, relationships, and multi-hop questions. Effective evaluation separates retrieval recall and ranking from answer faithfulness, relevance, citation quality, and abstention behavior. Because RAG can expose private data, stale information, biased sources, and prompt-injection content, responsible deployment requires governance, monitoring, human oversight, and explicit limitations.

Key Terms

Embedding: A numerical vector representation of text or another information object.

Chunk: A retrievable segment of a source document.

Chunk overlap: Shared content between adjacent chunks used to reduce boundary loss.

Vector database: A system that stores and searches vector representations with associated metadata.

Sparse retrieval: Search based primarily on weighted terms and lexical matches.

Dense retrieval: Search based on similarity between learned vector representations.

Hybrid retrieval: A combination of sparse and dense retrieval methods.

BM25: A term-based ranking function using frequency, document length, and inverse document frequency.

Cosine similarity: The normalized dot product used to compare vector direction.

Reranker: A model or algorithm that reorders an initial retrieval candidate set.

Metadata filter: A condition restricting results by fields such as date, role, or document status.

Grounding: Constraining a generated response to supplied evidence.

Provenance: Information describing the origin, version, and transformation history of evidence.

Citation completeness: The extent to which important claims have supporting references.

Knowledge graph: A structured network of entities and typed relationships.

Entity resolution: The process of determining when records refer to the same real-world entity.

Abstention: Declining to answer or qualifying an answer when evidence is insufficient.

Prompt injection: Malicious or unintended instructions embedded in input or retrieved content.

Recall@k: The proportion of evaluation queries with relevant evidence among the top k results.

Review Questions

Short Answer Questions

  1. What problem does RAG address in conventional LLM applications?
  2. Why should document metadata include version and effective-date information?
  3. Distinguish sparse retrieval from dense retrieval.
  4. What is the purpose of a reranker?
  5. Define grounding and explain why it does not guarantee truth.

Long Answer Questions

  1. Explain the complete lifecycle of a RAG pipeline from source acquisition to answer evaluation.
  2. Compare fixed-length, recursive, semantic, and structure-aware chunking strategies.
  3. Explain cosine similarity, vector databases, and approximate nearest-neighbor search in an enterprise RAG system.
  4. Discuss how knowledge graphs complement text retrieval in graph-augmented RAG.
  5. Analyze the ethical, privacy, security, and governance requirements of a RAG assistant used in a high-impact domain.

Analytical / Application Questions

  1. An organization’s assistant retrieves relevant passages but frequently cites obsolete policy versions. Diagnose the likely causes and propose a redesigned ingestion, metadata, retrieval, and evaluation process.
  2. Design a hybrid RAG architecture for a technical-support corpus containing manuals, error codes, diagrams, and service bulletins. Justify the retrieval, chunking, reranking, and citation choices.
  3. A user asks a question whose answer is absent from the indexed corpus, but the LLM produces a confident response. Develop an evaluation test and operational safeguards that would improve abstention and reduce unsupported generation.

Multiple Choice Questions with Answers

Question 1

Which component of a RAG pipeline normally finds external evidence before generation?

A. Tokenizer

B. Retriever

C. Decoder loss

D. Grammar parser

Correct Answer: B. Retriever

Question 2

What does cosine similarity compare most directly?

A. The storage size of two documents

B. The publication dates of two documents

C. The direction of two non-zero vectors

D. The number of pages in two documents

Correct Answer: C. The direction of two non-zero vectors

Question 3

Which retrieval method is particularly useful for exact product codes and error messages?

A. Sparse retrieval

B. Random retrieval

C. Image generation

D. Audio retrieval

Correct Answer: A. Sparse retrieval

Question 4

Why is hybrid retrieval often useful in enterprise search?

A. It eliminates the need for metadata

B. It combines exact lexical matching with semantic similarity

C. It guarantees that every answer is correct

D. It prevents all prompt injection attacks

Correct Answer: B. It combines exact lexical matching with semantic similarity

Question 5

What is the main role of a reranker?

A. To convert documents into PDFs

B. To reorder an initial candidate set using more detailed query-passage analysis

C. To delete all duplicate users

D. To train the original language model from scratch

Correct Answer: B. To reorder an initial candidate set using more detailed query-passage analysis

Question 6

Which information is most important for producing reliable source citations?

A. The model’s temperature only

B. The color of the user interface

C. Structured provenance such as document, version, and page

D. The number of neurons in the embedding model

Correct Answer: C. Structured provenance such as document, version, and page

Question 7

What does Recall@k measure in retrieval evaluation?

A. Whether a relevant result appears among the top k results

B. The grammatical quality of the final answer

C. The average length of a document

D. The number of tokens generated per second

Correct Answer: A. Whether a relevant result appears among the top k results

Question 8

Which feature most distinguishes a knowledge graph from a plain document index?

A. It stores only unstructured paragraphs

B. It represents entities and typed relationships

C. It cannot retain provenance

D. It always uses image embeddings

Correct Answer: B. It represents entities and typed relationships

Question 9

What should a RAG system do when authoritative retrieved evidence is insufficient?

A. Invent a likely answer

B. Ignore access controls

C. Abstain or clearly state the limitation

D. Select the oldest document automatically

Correct Answer: C. Abstain or clearly state the limitation

Question 10

Why must authorization be applied before context is sent to the language model?

A. Prompt instructions alone cannot reliably prevent exposure of unauthorized retrieved text

B. Authorization increases embedding dimensions

C. It makes all documents semantically identical

D. It removes the need for citations

Correct Answer: A. Prompt instructions alone cannot reliably prevent exposure of unauthorized retrieved text

Natural Language Processing & Generative AI
Chapter 10 of 15