LLMJudge#
- class scikitplot.corpus.LLMJudge(llm, *, on_error='raise')[source]#
Judge answers and contexts with a language model.
- Parameters:
- llmLLM or callable
An
LLMfrommake_llm, orfn(prompt, system) -> str, which is wrapped bymake_llm.- on_error{“raise”, “record”}, default=”raise”
What a failed judgement does: raise, or return a
JudgeResultwithscore=NaNanderrorset. Applies to provider errors, cut-off completions and invalid verdicts; never to invalid arguments, which always raise.
- Parameters:
See also
make_llmThe model behind a judge.
evaluate_answersRuns judges over a dataset.
Notes
User note. Each method makes one model call (
preference: two) and costs tokens;identityrecords the model and prompt version so scores from different judges are never mixed unknowingly. To judge the same answers with several models (a panel), pass several judges toevaluate_answers, which reports each judge, their mean and their agreement.Examples
>>> judge = LLMJudge(lambda prompt, system: '{"score": 4, "reason": "direct"}') >>> judge.answer_relevance("Capital of France?", "Paris.").score 0.75
- answer_correctness(question, answer, reference)[source]#
Statement-level F1 of the answer against the reference.
- Parameters:
- question, answer, referencestr
The question, the answer under evaluation and the reference answer.
- Returns:
- JudgeResult
score = TP / (TP + (FP + FN) / 2).details:tp,fp,fn. Undefined when all three are zero.
- Parameters:
- Return type:
- answer_relevance(question, answer)[source]#
How directly the answer addresses the question:
(rating - 1) / 4.- Parameters:
- question, answerstr
The question and the answer under evaluation.
- Returns:
- JudgeResult
details:rating(1-5).
- Parameters:
- Return type:
- context_precision(question, contexts, reference=None)[source]#
Average precision of the relevant contexts, in retrieval order.
- Parameters:
- questionstr
The question the contexts were retrieved for.
- contextsiterable
Retrieved contexts, best first (see
context_texts).- referencestr or None, default=None
A reference answer, which helps the judge decide usefulness.
- Returns:
- JudgeResult
score = sum(precision@k for each relevant rank k) / n_relevant;0.0when no context is relevant.details:relevant(one boolean per context, in order),n_relevant. Undefined without contexts (no call made).
- Parameters:
- Return type:
Notes
The verdict must hold exactly one entry per context index (1-based); a missing, repeated or out-of-range index is an invalid verdict.
- context_recall(question, contexts, reference)[source]#
Fraction of the reference’s statements attributable to the contexts.
- Parameters:
- questionstr
The question the contexts were retrieved for.
- contextsiterable
Retrieved contexts (see
context_texts).- referencestr
The reference answer.
- Returns:
- JudgeResult
details:n_statements,n_attributed. Undefined when the reference yields no statement, or without contexts (no call made).
- Parameters:
- Return type:
- criteria_score(question, answer, criteria, *, scale=5, reference=None, contexts=None)[source]#
Rate the answer against a caller’s rubric:
(rating - 1) / (scale - 1).- Parameters:
- question, answerstr
The question and the answer under evaluation.
- criteriastr
The rubric, in plain language (“cites a source for every number”).
- scaleint, default=5
Highest rating, at least 2.
- referencestr or None, default=None
Optional reference answer shown to the judge.
- contextsiterable or None, default=None
Optional contexts shown to the judge.
- Returns:
- JudgeResult
details:rating,scale.
- Parameters:
- Return type:
- faithfulness(question, answer, contexts)[source]#
Fraction of the answer’s claims that the contexts support.
- Parameters:
- question, answerstr
The question and the answer under evaluation.
- contextsiterable
The contexts the answer was generated from (see
context_texts).
- Returns:
- JudgeResult
details:n_claims,n_supported. Undefined (NaN) when the answer makes no claim, or without contexts (no call made).
- Parameters:
- Return type:
- preference(question, answer_a, answer_b, *, reference=None)[source]#
Which of two answers is better, asked in both orders.
- Parameters:
- question, answer_a, answer_bstr
The question and the two answers; the score is for
answer_a.- referencestr or None, default=None
Optional reference answer shown to the judge.
- Returns:
- JudgeResult
scorefor answer A: 1.0 (A wins), 0.0 (B wins) or 0.5 (tie).details:winner("a","b"or"tie") andposition_consistent: whether both orders named the same winner. Two orders that disagree are a tie.
- Parameters:
- Return type:
Notes
The first call shows A first; the second shows B first. A judge that always picks the first (or second) position therefore yields a tie flagged
position_consistent=False, not a win.