Training a detector#
Any model that returns top_logprobs can have a detector fitted for it, published weights
or not. It takes answers that model generated and a verdict on each. The pipeline is the one
How it works describes, unchanged — only the classifier’s coefficients are new.
from artefactual import make_detector
K = 15 # the k the published detectors were trained with; see the scoring guide
# responses: what the model answered, generated with top_logprobs=K
# y: one 0/1 label per answer, 1 marking a hallucination
detector = make_detector(k=K).fit(responses, y)
A fit needs two things, both in the OpenAI Batch output shape and joined on custom_id:
the answers, and a verdict on each. Three notebooks fit a detector from them, each starting
from a different amount of that already in hand — none depends on the others, and each one
ends in a saved .skops file.
You already have |
Notebook |
|---|---|
an endpoint, and nothing else |
Train a WEPR detector from an endpoint alone: generate, judge, fit |
the answers, but nothing judged yet, and no budget to judge them |
Train a WEPR detector from unjudged answers, judged on your machine |
the answers, and a verdict on each |
Each runs top to bottom as it ships — the first on a hundred questions, half TriviaQA and half SimpleQA, the other two on committed sample files — so the detector is fitted before anything is edited. Pointing one at your own data is the question list in one cell for the first, or two file paths for the other two.
How much data#
A hundred judged answers is enough to see whether the detector separates the two classes,
which is the scale the notebooks run at. The paper’s own runs use TriviaQA at around 500
questions per model — the procedure is in
scripts/ecir. EPR
fits one coefficient rather than 2k, so it is the one to reach for when labels are scarcer
than that.
How hard the questions are matters as much as how many. A model that answers nearly all
of them correctly produces almost no hallucinations to learn from, and a detector fitted
there separates a problem that was already nearly separable — the ROC-AUC then describes
the question set. TriviaQA on its own is that easy for a current model. The shipped pack and
build_questions.sh mixed draw half from SimpleQA, a set selected for questions strong
models get wrong.
The label a judge’s verdict becomes#
read_judgment returns True when the answer was correct, and the class a detector
predicts is the hallucination. The caller negates it, and that is the whole of the
conversion:
The judge said |
|
|
Means |
|---|---|---|---|
the answer was correct |
|
|
grounded |
the answer was wrong |
|
|
hallucination — the class |
something unreadable |
|
— |
drop the row and count it |
Whether the fit worked#
Each notebook cross-validates the fit and reports a ROC-AUC with a per-class breakdown. What the fit weighs in Train a WEPR detector from answers that are already judged plots the fitted coefficient per rank, and Audit the labels the judge produced in Train a WEPR detector from unjudged answers, judged on your machine lists, question by question, where the judge and the fitted detector disagree — the labels are the part worth doubting first.
To choose k or the classifier’s regularisation, give the unfitted detector to
GridSearchCV with the responses and the labels; scikit-learn compatibility has the recipe, including
sample_weight.