方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Build an LLM-as-judge evaluator

Build an LLM-as-Judge Evaluator

Automated evaluation with a language model as the judge is one of the most important patterns in applied AI engineering. Instead of requiring human annotation for every output, you prompt a capable model with a rubric and let it score responses at scale.

What you are building

Implement a LLMJudge class that:

  1. Accepts a rubric -- a plain-English description of what "good" looks like for the task.
  2. Scores a response -- given a question, a generated response, and optional context, returns a score between 0.0 and 1.0 plus a one-sentence rationale.
  3. Batches evaluations -- runs a list of (question, response, context) tuples and returns aggregated results.
  4. Validates output -- if the judge returns malformed JSON or an out-of-range score, log a warning and return a default score of 0.5 rather than crashing.

Rubric for testing

A helpful response directly addresses the user's question, uses only information
from the provided context, and does not introduce unsupported claims.
Score 1.0 if fully helpful and faithful, 0.5 if partially, 0.0 if off-topic or hallucinated.

Constraints

  • No external evaluation libraries.
  • Type-hint every method.
  • The judge prompt must include the rubric, question, context, and response in clearly labeled sections.
  • Return a JudgeResult dataclass with: score: float, rationale: str, raw_output: str.

Evaluation / medium / Step 8 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.