Language models & knowledge
Understanding retrieval-augmented generation
Retrieval-augmented generation supplies a language model with retrieved information. Quality therefore begins before writing: with source selection, validity and traceability.
The short answer
RAG combines retrieval of external information with subsequent generative processing. This demo shows only retrieval and source-context preparation: actual local TF-IDF search, inspectable matches and detectable conflicts between document versions.
Open interactive exampleFree · in your browser · no sign-up
At a glance
- Retrieval finds relevant context; it does not automatically establish truth.
- Dates, validity and access rights belong in source selection, not just in the final answer.
- Missing evidence or conflicting sources require a separate process path instead of an invented answer.
Retrieval and writing are different tasks
A question about a current approval limit cannot be answered through fluent language alone. The applicable rule must first be found. Retrieval-augmented generation connects access to information with subsequent generative processing. The original RAG approach combines a generative model with a retrievable document collection (Lewis et al. 2020).
Separating these steps has practical value. A retrieval system might return the wrong version even if a language model then summarises its text accurately. Conversely, the correct source might be available but misrepresented. Our interactive example therefore concentrates on the first task: finding documents, showing their metadata and assembling source context. It deliberately contains no generator.
Lexical retrieval with TF-IDF
The small collection contains five fictional documents. A query is split into words, common function words are removed, and each remaining word receives a weight. Words occurring in relatively few documents receive more weight than ubiquitous terms. The smoothed variant used here is
Here is the number of documents and counts documents containing term . Term frequency counts actual occurrences. Document and query vectors are normalised to unit length. Their dot product then equals cosine similarity. This common weighting scheme is described in the official documentation (scikit-learn: TF-IDF).
All weights are calculated from the fixed full collection. The validity filter then restricts which documents can be selected. Changing the number of results therefore does not silently change the word weights. Words absent from the collection cannot produce a match, and documents with zero similarity are omitted.
An example with conflicting rules
Document P-2026, dated 1 October 2026, allows team leads to approve orders up to 5,000 euros. Archived document P-2024 states 2,000 euros. Both contain the words “purchase” and “approval”. A search for those terms can therefore find both versions. A high retrieval score does not decide which rule applies.
The collection also contains notes about hotel costs, IT incidents and purchasing quotes. For each retrieved document, the calculator shows its identifier, date, status, score and unchanged text. Selecting several sources exposes the difference between the approval limits. Conflict detection explicitly uses annotated fact fields with the same category and different values. It is not language understanding or a general contradiction detector.
Selecting “Current sources only” removes the archived rule. That works because the example already knows its status. In a real collection, somebody must establish who assigns validity, how replacement versions are linked and whether a newer rule actually applies to the particular case being considered.
Assemble source context yourself
RAG foundation: retrieve source context with TF-IDF
The demo searches local example documents and displays source context. It does not generate a model answer.
Start with “purchase approval” and allow all five results. Then enable the validity filter. Check which information this selection removes and which ambiguity disappears. Next, search for a term absent from every document: missing evidence is the correct result.
The maximum source count bounds the context. A small value can hide a relevant supporting document or a contradictory passage. More results are not automatically better either: irrelevant text can make downstream processing harder. Selection therefore requires domain evaluation and should be recorded alongside any later answer so the evidence can be inspected.
Limitations and useful checks
TF-IDF here recognises shared words, not synonyms, grammatical variations or meaning. A query in the wrong language may therefore return nothing despite relevant content being present. Retrieval scores are neither calibrated relevance probabilities nor truth values. Even a document marked current could contain incorrect information.
Before practical use, create test questions with expected sources, deliberately missing answers and contradictory versions. Check separately whether the right source was found, selected appropriately and then used accurately. Access permissions must already apply during retrieval; a disclaimer attached to an answer cannot replace an authorisation check.
From evidence to a reviewable decision
A downstream language model could turn the source context into a draft answer. That step does not happen here. For consequential actions, a plausible draft would still be insufficient: Rules and optimisation shows the transition from documented requirements to explicit constraints. Transformer attention explains one calculation used in language processing.
Frequently asked questions
Does the demo generate an AI answer?
No. It calculates local lexical retrieval and displays unchanged document excerpts as source context. No language model runs, and no answer is presented as model output.
Does RAG eliminate hallucinations?
No. Sources can be missing, outdated or incorrect, and a downstream model may use them incorrectly. Retrieval, source checking and answer checking are separate tasks.
Is TF-IDF the same as semantic search?
No. TF-IDF weights words that occur in documents. This demo does not recognise synonyms or conceptually similar expressions without shared words.
Sources and further reading
- PaperLewis, P. et al. (2020): Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. NeurIPS 2020.
- Sourcescikit-learn contributors (2026): Feature extraction: TF-IDF term weighting. Official documentation.
Content last reviewed on 14 November 2026. Please let us know about any errors: impressum@nyxai.com.