LLM and RAG foundations: context, knowledge and retrieval

LLM and RAG foundations: context, knowledge and retrieval

Learning materials

Model, context and external knowledge

A Large Language Model (LLM) predicts the next token from the available context.

ComponentContentsHow it changes
Model weightsNumerical parameters learned during trainingTraining, fine-tuning or loading different weights
Call contextSupplied instructions, messages and tool resultsThe application builds a new request
Search indexRepresentations of external documentsIndexing a new document version

A chat database is invisible to the model unless the application supplies its contents. A new instruction in a request does not update weights. Inference means applying an already trained model.

LLM and RAG foundations: context, knowledge and retrieval: mechanism diagram with an example

Tokens and model adjustments

  • A token is a processing unit: a word, word piece, punctuation or another fragment. Word counts cannot enforce token limits.
  • A context window limits the tokens available to one call; input and output constraints depend on the model and API (Application Programming Interface).
  • Few-shot prompting supplies examples in the input. Fine-tuning changes trainable parameters using examples.
  • Parameter-efficient fine-tuning (PEFT) updates a limited parameter set instead of all model weights. LoRA (Low-Rank Adaptation) is one PEFT method.
  • LoRA trains a low-rank update to a weight matrix, usually with frozen base weights. It fine-tunes a smaller parameter set.
  • Quantization uses lower numerical precision. It saves memory but can reduce quality.
  • Temperature changes the next-token sampling distribution. Less randomness does not establish truth.

RAG: answering with retrieved documents

Retrieval-Augmented Generation (RAG) supplies retrieved sources to a generator. A chunk is a document passage; its boundaries should preserve conditions and exceptions. An embedding is a numerical vector representing text. Nearby vectors indicate similarity under the chosen model, not verified truth.

MethodRoleExample
BM25 (Best Match 25)Word-based retrievalExact part number AB-42
Dense retrievalNearby-vector retrieval“Send a purchase back” → “product returns”
Hybrid searchCombines lexical and vector retrievalA part number and a problem description
RRF — Reciprocal Rank FusionCombines ranks rather than incomparable scoresA document near the top of both lists gets larger contributions
RerankerReorders retrieved candidates more closelySelects 5 passages from 100
HNSW (Hierarchical Navigable Small World)Accelerates approximate nearest-neighbour retrievalTrades recall against time and memory

When queries and documents use different words

  • HyDE (Hypothetical Document Embeddings) generates a hypothetical document, then retrieves real documents using its embedding. Invented details in that draft are not evidence.
  • Multi-query creates several query formulations. Merge their results and remove duplicate documents.

Example: a product-return deadline

  1. The user asks: “Can I return a purchase after 10 days?”
  2. Retrieval finds the current passage: “Returns within 14 days; custom-made products are excluded.”
  3. The context includes the question and the whole passage, including the exception.
  4. The answer states: “The deadline fits an ordinary product; the custom-made exception still applies.”

If chunking removes the exception, even a good generator receives incomplete evidence. If retrieval returns an old policy, inspect its version and index. RAG does not guarantee correctness or require a vector database: retrieval can use other methods. A hallucination is a plausible but false or unsupported claim; today's price without a current source requires verification.