Language models, retrieval, prompting, and language adaptation · Long-context and retrieval-augmented language · LNG051 · Model
Retrieval-Augmented Generation (RAG)
Couples a dense retriever with a sequence generator for knowledge-intensive language tasks. The original named RAG models have specific latent-document formulations;
In CodeSplash
A CodeSplash feature runs part of this entry inside the app; some variants aren't offered. There's no Set up button; open it where the label says. It uses the AI model you set up in Settings › AI, and the project's AI privacy settings decide what it may send.
Do it in CodeSplash Run it on your own data in the app: Data › Import › Documents… makes a collection; its Ask documents button (or an @-mention in the AI panel) answers with the AI model you set up in Settings › AI from at most 8 retrieved passages (BM25, fused with meaning-based search when an embedder is installed), and the app checks every sentence's citations, quotes and numbers. It is a frozen retrieve-then-answer pipeline: RAG-Sequence and RAG-Token marginalization and joint retriever and generator training aren't offered.
- Availability
- Partly runs in CodeSplash (AI)
- Code example
- Component demo, not a trained model
- Who ran it
- CodeSplash's own run
- CodeSplash's run
- The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: The chapter's authors report running it; there is no record for this entry alone. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Couples a dense retriever with a sequence generator for knowledge-intensive language tasks. The original named RAG models have specific latent-document formulations; the broader industry phrase also includes many pipelines that do not optimize that objective.
Question / objective
How should a generator combine uncertainty over several retrieved evidence documents?
Architecture / computational structure
A retriever and conditional sequence generator form a latent-document model. Learned retriever/generator weights are parameters; chunking, candidate count, corpus version, and sequence-versus-token marginalization are system/model choices.
Assumptions
Retrieval does not guarantee correctness, source quality, or faithful use of evidence.
Formula / computational definition
RAG-Sequence uses sum_z p_eta(z|x) product_t p_theta(y_t|x,z,y_<t); RAG-Token permits the document mixture inside the token product. Candidate retrieval approximates the otherwise large document sum.
Parameters and notation
Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Chunking, retriever, top-k, index corpus, generator, evidence budget, document probability normalization, and joint versus frozen training.
Fitting, fine-tuning, and adaptation
Fit conditional likelihood with the intended marginalization, or clearly label a simpler frozen retrieval pipeline. Adapt the generator via supported LoRA/full updates and retriever through relevance losses; reindex after encoder changes. Validate answer correctness, citation support, retrieval quality, and forgetting independently.
How to use and calibrate
Separate retriever recall, answer correctness, evidence attribution, citation validity, faithfulness, latency, and cost. Use development data for settings and an independent test set for the final estimate. Report uncertainty across examples or seeds when the evaluation design supports it; model scores and self-assessments are not automatically calibrated probabilities of correctness.
Example application
Answer internal technical questions with passages from a curated, versioned manual collection.
Interpretation / pitfalls
Watch out
Irrelevant or adversarial documents can influence answers; unsupported claims may persist even when sources are supplied. The code below isolates one stated component; successful execution does not validate model quality or reproduce the cited experiment.
Python and R code examples
Full implementation route: Transformers includes implementations of the original RAG models; custom pipelines need a retriever, generator, and explicit evaluation. R can bridge Python with reticulate. The example demonstrates document-level marginal likelihood rather than claiming that a context-concatenation pipeline is the original model.
Example scope: RAG-Sequence versus RAG-Token marginalization on two toy documents and two output tokens.
Dependencies: Python 3 standard library; base R; shared setup above. No model downloads or external services.
Validation: Python component executed in the combined example script; R counterpart supplied but not runtime-tested here. All numbers are synthetic.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Component demo, not a trained model — RAG-Sequence versus RAG-Token marginalization on two toy documents and two output tokens.
Shared setup for this chapter
import math
def softmax(xs):
m = max(xs)
es = [math.exp(x-m) for x in xs]
return [e/sum(es) for e in es]
def dot(a, b):
return sum(x*y for x, y in zip(a, b))
def matvec(matrix, vector):
return [dot(row, vector) for row in matrix]
def meanvec(rows):
return [sum(row[j] for row in rows)/len(rows) for j in range(len(rows[0]))]
def sigmoid(x):
return 1/(1+math.exp(-x))
def nll(probabilities):
return -sum(math.log(p) for p in probabilities)
pz = [0.6,0.4]; token_p = [[0.9,0.2],[0.3,0.8]]
sequence = sum(w*math.prod(p) for w,p in zip(pz,token_p))
token = math.prod(sum(pz[z]*token_p[z][t] for z in range(2)) for t in range(2))
print(sequence,token)
Component demo, not a trained model — RAG-Sequence versus RAG-Token marginalization on two toy documents and two output tokens.
Shared setup for this chapter
softmax <- function(x) {
shifted <- exp(x-max(x))
shifted/sum(shifted)
}
pz <- c(0.6,0.4); token_p <- rbind(c(0.9,0.2),c(0.3,0.8))
sequence <- sum(pz*apply(token_p,1,prod))
token <- prod(colSums(token_p*pz))
print(c(sequence=sequence,token=token))
Sources
- Source 1arxiv.org
From the CodeSplash reference, entry LNG051.