方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Implement a judge calibration pipeline

Implement a Judge Calibration Pipeline

You have an LLM-as-judge that scores RAG responses for faithfulness, but you don't know if it agrees with humans. Build a calibration pipeline that measures how well the judge aligns with human labels and flags when calibration degrades.

Requirements

  1. JudgeCalibrationRun dataclass: stores case_id, judge_score, human_score, dimension (faithfulness/relevance/helpfulness), and timestamp.

  2. compute_calibration_metrics(runs: list[JudgeCalibrationRun]) -> dict that returns:

    • agreement_rate: fraction of cases where abs(judge_score - human_score) <= 0.2
    • mean_absolute_error: average abs(judge_score - human_score)
    • bias: mean of judge_score - human_score (positive = judge rates higher than humans)
    • cohens_kappa: Cohen's Kappa treating scores >= 0.7 as "pass" and < 0.7 as "fail"
    • by_dimension: the above metrics broken down per dimension
  3. is_calibration_healthy(metrics: dict) -> tuple[bool, list[str]] that returns (True, []) when calibration is acceptable, or (False, [reason, ...]) when it fails. Flag when: agreement_rate < 0.75, mean_absolute_error > 0.25, or abs(bias) > 0.15.

  4. A standalone cohens_kappa(predicted: list[bool], actual: list[bool]) -> float function using the formula (p_o - p_e) / (1 - p_e) where p_o is observed agreement and p_e is expected agreement by chance.

Constraints

  • No external ML libraries. Pure Python only.
  • compute_calibration_metrics must handle the case where a dimension has no data (skip it in by_dimension).
  • All scores are floats in [0.0, 1.0].

Evaluation / hard / Step 35 of 36

Practice stage

Evaluation and review loops

Hint

Separate the scoring logic from the interpretation logic. Your goal is not just a number; it is a useful next action.

Success criteria
  • - Produces a useful signal, not decorative output
  • - Makes regression review easier
  • - Would support a benchmark or observability loop
Review checklist
  • - Would this output help decide what to fix next?
  • - Are important failure modes visible?
  • - Does the score hide any ambiguity I should record?

Practice

Generate a variation

Generate a new exercise variation to deepen understanding or practice a related concept.

Attempt history

Recent submissions

Before you submit, decide what a strong answer should make obvious to the reviewer.

No attempts yet.