Chapter 4: Evaluate AI Systems (Practical Tooling)
+-----------------------------------------------------------------------------+
| LLM-AS-A-JUDGE EVALUATION |
+-----------------------------------------------------------------------------+
| Question + Context + Candidate Output ---> Frontier Model (GPT-4o) |
| | |
| v |
| Output: Structured JSON Score (1-5) + Rigorous Step-by-Step Justification |
+-----------------------------------------------------------------------------+
The LLM-as-a-Judge Paradigm
Instead of relying solely on expensive human annotators, production teams use a larger, more capable model (the "Judge") to evaluate smaller operational models:
1. Single-Answer Grading
The judge evaluates one output against a strict rubric.
2. Pairwise Comparison (Side-by-Side Arena)
The judge is given two outputs (Model A vs. Model B) blindly and selects the winner to compute Elo ratings.
Implementation: Production Faithfulness Evaluator
from pydantic import BaseModel, Field
from openai import OpenAI
class FaithfulnessVerdict(BaseModel):
is_faithful: bool = Field(description="True if all claims are backed by context")
hallucinated_claims: list[str] = Field(description="List of statements unsupported by context")
confidence_score: float = Field(ge=0.0, le=1.0)
reasoning: str
def evaluate_faithfulness(question: str, context: str, answer: str) -> FaithfulnessVerdict:
client = OpenAI()
prompt = f"""
You are an impartial evaluator auditing an AI assistant for hallucinations.
Verify if the ANSWER contains any factual claims not directly supported by the CONTEXT.
QUESTION: {question}
CONTEXT: {context}
ANSWER: {answer}
"""
res = client.beta.chat.completions.parse(
model="gpt-4o",
response_format=FaithfulnessVerdict,
messages=[{"role": "user", "content": prompt}],
temperature=0.0
)
return res.choices[0].message.parsed
Avoiding Judge Biases
Chip warns of four prominent biases when using LLM judges:
- Position Bias: Models tend to favor the first answer in pairwise evaluations (Mitigation: swap positions and average).
- Verbosity Bias: Models favor longer, wordier answers even if they contain fluff (Mitigation: penalize unnecessary word length).
- Self-Enhancement Bias: A model (e.g. GPT-4) tends to score its own outputs higher than Claude or Gemini outputs.