Evaluation Frameworks
From the course AI Testing & Quality Assurance
Systematic AI Evaluation
LLM-as-Judge
Use a powerful model to evaluate outputs:
async function evaluateResponse(query: string, response: string): Promise<number> {
const evaluation = await openai.chat.completions.create({
model: "gpt-4o",
messages: [{
role: "user",
content: `Rate the following response to the query on a scale of 1-5.
Query: ${query}
Response: ${response}
Criteria:
- Accuracy (is it factually correct?)
- Completeness (does it address the full query?)
- Clarity (is it well-written and easy to understand?)
Return JSON: { "score": number, "reasoning": string }`
}],
response_format: { type: "json_object" },
});
return JSON.parse(evaluation.choices[0].message.content).score;
}
Evaluation Metrics
| Metric | What It Measures | When to Use |
|---|---|---|
| Accuracy | Factual correctness | Q&A, research |
| Relevance | Query-response alignment | Search, RAG |
| Coherence | Logical flow | Long-form content |
| Toxicity | Harmful content | All applications |
| Faithfulness | Grounded in context (RAG) | RAG systems |
Building an Eval Dataset
[
{
"input": "What is the capital of France?",
"expected": "Paris",
"category": "factual",
"difficulty": "easy"
},
{
"input": "Explain quantum entanglement",
"criteria": ["mentions particle pairs", "non-local correlations", "measurement"],
"category": "explanation",
"difficulty": "medium"
}
]
Eval Best Practices
- Use 50-200 diverse test cases per evaluation
- Include edge cases and adversarial inputs
- Version your eval dataset alongside code
- Run evals on every prompt or model change
Common Pitfall: LLM Judge Bias Toward Longer Responses
When using an LLM as a judge, studies consistently show that larger models rate longer responses higher regardless of quality — a form of verbosity bias. Counteract this by instructing the judge to penalize unnecessary length and by evaluating conciseness as a separate dimension:
const prompt = `Rate the response (1-5) for:
1. Accuracy: Does it correctly answer the question?
2. Conciseness: Does it avoid unnecessary words?
3. Completeness: Does it cover all aspects of the question?
IMPORTANT: A shorter, accurate answer should score higher than a longer, verbose answer.
Best Practice: Track Evaluation Score Distributions, Not Just Averages
An average score of 4.0 can hide bimodal quality — some responses scoring 5.0 and others scoring 3.0. Track score distributions to identify systematic failure modes:
const scores = await runEvalSuite(prompt, testCases);
const distribution = {
"5.0": scores.filter(s => s === 5).length,
"4.0-4.9": scores.filter(s => s >= 4 && s < 5).length,
"3.0-3.9": scores.filter(s => s >= 3 && s < 4).length,
"below_3.0": scores.filter(s => s < 3).length,
};
console.log("Score distribution:", distribution);
// "below_3.0": 12 reveals a systematic failure affecting 12 test cases
Named Product Reference: Braintrust for AI Evaluation
Braintrust is an enterprise AI evaluation platform that provides a web UI for running and comparing evaluations, a SDK for defining evals in code, and a prompt playground. It stores eval results across model versions so you can compare GPT-4o vs. Claude vs. Gemini on the same dataset with one command. For teams running regular evaluations, Braintrust eliminates the need to build custom eval infrastructure.
Key Takeaways
- •LLM-as-Judge uses a powerful model to score outputs
- •Evaluation metrics include accuracy, relevance, coherence, and toxicity
- •Build eval datasets with 50-200 diverse, categorized test cases
- •Run evaluations on every prompt or model change
Progress is kept in this browser only.