Skip to content

Language models, retrieval, prompting, and language adaptation · Long-context and retrieval-augmented language · LNG059 · Training method

Self-RAG

Trains a language model to decide when retrieval is useful and to produce reflection tokens about evidence and answer quality.

Explained here Component demo, not a trained model CodeSplash's own run

In CodeSplash

Documented in the Academy only. There's no Set up button.

Availability
Explained here
Code example
Component demo, not a trained model
Who ran it
CodeSplash's own run
CodeSplash's run
The reference's example ran here in one language.
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

Trains a language model to decide when retrieval is useful and to produce reflection tokens about evidence and answer quality. Retrieval is integrated into generation control rather than always being applied once before answering.

Question / objective

Can a model learn when to retrieve and whether evidence supports its generated answer?

Architecture / computational structure

A generator emits ordinary and reflection tokens; retrieval and segment selection interact with those learned token predictions. Learned generator weights are parameters; critique thresholds, candidate budgets, and score weights are inference controls.

Assumptions

Self-assessed support is learned behavior, not independent verification.

Formula / computational definition

S(y)=log⁡pθ(y∣x,z)+∑jλjcj(x,z,y)(generic critique-weighted selection illustration).S(y)=\log p_\theta(y\mid x,z)+\sum_j\lambda_jc_j(x,z,y)\qquad\text{(generic critique-weighted selection illustration).}

Training augments answer sequences with retrieval and critique tokens. During inference, segment candidates can be scored using weighted relevance, support, and utility signals, alongside language-model likelihood, according to the specified decoding configuration.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Reflection-token training data, retrieval thresholds, candidate count, critique-score weights, evidence corpus, and generation length.

Fitting, fine-tuning, and adaptation

Train on the published reflection-augmented format using task/retrieval/critique supervision; generic prompt additions alone do not reproduce training. Use supported adapters or full updates while preserving special-token handling. Validate critique calibration against independent evidence judgments and monitor retrieval cost and base-task retention.

How to use and calibrate

Measure retrieval frequency, factual support, citation quality, answer correctness, and extra token/latency cost; compare with fixed-retrieval baselines. 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

Generate cited answers while explicitly evaluating when retrieval was needed and whether claims are supported.

Interpretation / pitfalls

Watch out

Reflection tokens can be confidently wrong; retrieval policy and source quality determine whether additional evidence actually improves reliability. 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: Use the authors' released training/inference implementation and models; R bridges Python using reticulate. The native weighted-candidate example illustrates one selection mechanism, not the full learned reflection model or a universal Self-RAG scoring formula.

Example scope: Illustrative Self-RAG candidate selection from supplied critique components; not a universal formula.

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.

Python

Component demo, not a trained model — Illustrative Self-RAG candidate selection from supplied critique components; not a universal formula.

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)
components = [[0.8,0.9,0.6],[0.9,0.3,0.8]]
weights = [0.3,0.5,0.2]
scores = [dot(weights,row) for row in components]
print(scores,max(range(2),key=scores.__getitem__))
R

Component demo, not a trained model — Illustrative Self-RAG candidate selection from supplied critique components; not a universal formula.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
components <- rbind(c(0.8,0.9,0.6),c(0.9,0.3,0.8)); weights <- c(0.3,0.5,0.2)
scores <- drop(components %*% weights)
print(list(scores=scores,selected=which.max(scores)))

Sources

From the CodeSplash reference, entry LNG059.