Score responses with EPR#
Score two committed responses, one certain and one hallucinated, with a published EPR detector, per answer and per token.
EPR (Entropy Production Rate) computes the entropy at each token and averages it over the sequence.
The two questions are “What is the capital city of France?”, answered with certainty, and “Who is Charles Moslonka?” (the first author of our paper), answered with a fabricated biography.
Needs / cost: no API key, no GPU; one detector download from the Hugging Face Hub (~KBs, cached), then seconds.
Section |
What it does |
Cost |
|---|---|---|
Load example responses |
Read the committed fixture |
none |
Build the EPR detector |
Fetch the published detector’s weights |
one download, ~KBs, cached after the first run |
Sequence-level scoring |
P(hallucination) per response |
seconds |
Token-level scoring |
P(hallucination) per token, highlighted |
seconds |
# The Hugging Face repository holding the published EPR detector for the model that
# produced these responses. A path to your own `.skops` file works too.
DETECTOR = "artefactory/epr-ministral"
DATA_PATH = "open_ai_responses_top15.json"
# Candidates per token the detector was trained at, which the fixture also carries. A detector refuses
# arrays of a different width rather than producing a mis-shaped score, and a response narrower
# than K is refused rather than padded: the missing candidates are unfetched, not absent, so
# filling them would understate the entropy.
K = 15
# Where the highlighting below changes colour.
THRESHOLD_LOW = 0.35 # at or below -> green, the model was confident here
THRESHOLD_HIGH = 0.70 # above -> red, it was not
Load example responses#
The fixture contains two responses, to illustrate the contrast between a certain and an uncertain answer.
with Path(DATA_PATH).open(encoding="utf-8") as f:
data = json.load(f)
responses = data["responses"]
print(f"Loaded {len(responses)} responses")
Loaded 2 responses
Build the EPR detector#
The detector is fetched from the Hub on first use, then cached. EPR.from_pretrained
always requires a repository id or a path. make_detector puts a LogProbParser in front: one pipeline from the raw OpenAI Responses API dicts
to scores.
detector = make_detector(k=K, classifier=EPR.from_pretrained(DETECTOR, k=K))
Sequence-level scoring#
predict_proba(response) returns an array of shape (n_sequences, 2). Column 1 is the
hallucination probability: higher means the model was more uncertain while generating the
answer. Nothing here was checked against any source.
for resp in responses:
prompt = resp["metadata"]["prompt"]
text = resp["output"][0]["content"][0]["text"]
score = detector.predict_proba(resp)[0, 1]
print(f"Prompt : {prompt}")
print(f"Answer : {text}")
print(f"Score : {score:.2f}")
print()
Prompt : What is the capital city of France? Please answer briefly.
Answer : Paris.
Score : 0.05
Prompt : Who is Charles Moslonka ? Where was he born ? Please answer in two sentences.
Answer : Charles Moslonka is a French singer born in Lyon in 1985.
Score : 1.00
Token-level scoring#
predict_token_proba returns (n_sequences, max_tokens, 1): the same
probability, per token, which is what says where in the answer the model was uncertain.
n_sequences: response index.max_tokens: token index within the sequence.1: the per-token scalar hallucination probability.
for resp in responses:
prompt = resp["metadata"]["prompt"]
tokens = [t["token"] for t in resp["output"][0]["content"][0]["logprobs"]]
scores = detector.predict_token_proba(resp)[0, :, 0]
print(f"Prompt: {prompt}")
display(highlight(tokens, scores))
Prompt: What is the capital city of France? Please answer briefly.
Prompt: Who is Charles Moslonka ? Where was he born ? Please answer in two sentences.
Where to go next#
Your model is not one of the published detectors? A detector reads one model’s confidence, so a published one only scores the model it was trained on. Fit your own with
EPR(k=15).fit(LogProbParser(k=15).transform(responses), y): see Train a WEPR detector from answers that are already judged.Score live traces. Score Langfuse traces with a detector attaches a detector’s score to Langfuse traces.
The other detector. Score responses with WEPR runs the same fixture through WEPR.