Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 11,065 Bytes
d15f07c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | ---
sidebar_position: 5
---
# AI Model Evaluation & Comparison
## Overview
When extracting structured data from meeting transcripts, knowing the "right answer" is often impossible. This guide shows how to evaluate and compare AI model outputs without ground truth using industry-standard techniques.
## The Challenge
In 2026, AI development has moved away from "Ground Truth" (knowing the right answer) toward **Comparative & Model-Based Evaluation**. For meeting analysis:
- There's no "correct" extraction of a city council decision
- Different models may extract different (but valid) interpretations
- We need systematic ways to evaluate quality and build consensus
## Evaluation Techniques
### 1. LLM-as-a-Judge Pattern
Instead of checking if an answer matches a known truth, use a more powerful model (the "Judge") to evaluate responses based on a rubric.
**Logic:** Evaluation is easier than generation. A model might not perfectly extract all meeting details, but it can spot if an extraction is missing key information.
**Repository:** [DeepEval](https://github.com/confident-ai/deepeval)
#### Local Code Example
```python
from deepeval.metrics import AnswerRelevancyMetric
from deepeval.test_case import LLMTestCase
# Evaluate decision extraction without needing "expected_output"
test_case = LLMTestCase(
input="What decisions were made about the parks budget?",
actual_output="The council approved $2.5M for parks renovation with a 7-2 vote..."
)
metric = AnswerRelevancyMetric(threshold=0.7)
metric.measure(test_case)
print(f"Relevancy Score: {metric.score}") # 0 to 1
print(f"Reasoning: {metric.reason}") # Why it gave that score
```
#### Using with Bronze Data Model
```python
import psycopg2
from deepeval.metrics import AnswerRelevancyMetric
from deepeval.test_case import LLMTestCase
# Compare decision extractions from bronze_decisions
conn = psycopg2.connect(DATABASE_URL)
cur = conn.cursor()
cur.execute("""
SELECT
source_ai_model,
decision_id,
headline,
decision_statement
FROM bronze_decisions
WHERE source_event_id = %s
ORDER BY source_ai_model
""", (event_id,))
results = cur.fetchall()
for model, decision_id, headline, statement in results:
test_case = LLMTestCase(
input=f"Extract the decision about {headline}",
actual_output=statement
)
metric = AnswerRelevancyMetric(threshold=0.7)
metric.measure(test_case)
print(f"{model}: Relevancy {metric.score}")
```
### 2. N-Way Consensus (Wisdom of the Crowds)
Run the same prompt on multiple models (e.g., Gemini 1.5, GPT-4, Claude 3). If two agree and one is outlier, the outlier is likely wrong.
**Repository:** [Open WebUI](https://github.com/open-webui/open-webui) (has built-in "Multi-Model Chat")
**Framework:** Self-Consistency
#### Judge Prompt for Consensus
```python
# After getting extractions from 3+ models in bronze_decisions
judge_prompt = """
Here are {n} AI model extractions of the same city council decision:
Model 1 (Gemini 1.5 Flash):
{extraction_1}
Model 2 (GPT-4):
{extraction_2}
Model 3 (Claude 3):
{extraction_3}
Instructions:
1. Identify the common points they all agree on (high confidence)
2. Identify points where they contradict (low confidence)
3. Output a final "Consensus Decision" that synthesizes the most accurate information
Format your response as:
- Consensus Points: [list]
- Contradictions: [list]
- Final Synthesis: [unified decision statement]
"""
```
#### Implementation with Bronze Data
```python
def build_consensus_decision(event_id: int, decision_id: str):
"""Build consensus from multiple model extractions."""
query = """
SELECT
source_ai_model,
decision_statement,
headline,
outcome,
primary_theme,
arguments_for,
arguments_against
FROM bronze_decisions
WHERE source_event_id = %s
AND decision_id = %s
ORDER BY source_ai_model
"""
cur.execute(query, (event_id, decision_id))
extractions = cur.fetchall()
if len(extractions) < 2:
return extractions[0] # No consensus needed
# Format extractions for judge
formatted = []
for i, (model, statement, headline, outcome, theme, args_for, args_against) in enumerate(extractions, 1):
formatted.append(f"""
Model {i} ({model}):
- Headline: {headline}
- Statement: {statement}
- Outcome: {outcome}
- Theme: {theme}
- Arguments For: {args_for}
- Arguments Against: {args_against}
""")
# Use judge model (e.g., Gemini 1.5 Pro or GPT-4)
judge_response = call_judge_model(
prompt=judge_prompt.format(
n=len(extractions),
**{f'extraction_{i}': ext for i, ext in enumerate(formatted, 1)}
)
)
return judge_response
```
### 3. Factual Grounding (RAG Evaluation)
If your extraction is based on source text (meeting transcript), check if the AI's answer is actually contained within the source.
**Metric:** Faithfulness (available in [DeepEval](https://github.com/confident-ai/deepeval) or [RAGAS](https://github.com/explodinggradients/ragas))
**Logic:** If the LLM says "The council voted 9-0" but the transcript says "Vote: 7-2," the Faithfulness score will be low.
```python
from deepeval.metrics import FaithfulnessMetric
from deepeval.test_case import LLMTestCase
# Get original transcript from event
cur.execute("""
SELECT es.event_description, et.decision_statement
FROM event es
JOIN events_text_ai et ON et.event_id = es.event_id
JOIN bronze_decisions bd ON bd.source_event_id = es.event_id
WHERE es.event_id = %s
""", (event_id,))
transcript, extracted_decision = cur.fetchone()
test_case = LLMTestCase(
input="Extract the council's decision",
actual_output=extracted_decision,
retrieval_context=[transcript] # The source document
)
metric = FaithfulnessMetric(threshold=0.7)
metric.measure(test_case)
print(f"Faithfulness: {metric.score}") # How grounded in source
```
### 4. Deterministic Guardrails
Check the **shape** of the result, even if you don't know the content:
```python
from pydantic import BaseModel, ValidationError
from typing import List, Optional
class DecisionExtraction(BaseModel):
"""Expected structure of a decision extraction."""
decision_id: str
headline: str
decision_statement: str
outcome: Optional[str]
primary_theme: Optional[str]
ntee_code: Optional[str]
arguments_for: List[dict]
arguments_against: List[dict]
def validate_extraction(extraction_json: dict) -> tuple[bool, str]:
"""Validate extraction structure."""
try:
DecisionExtraction(**extraction_json)
return True, "Valid structure"
except ValidationError as e:
return False, str(e)
# Example: Check all extractions in bronze_decisions
cur.execute("SELECT id, decision_id, structured_data FROM bronze_decisions")
for record_id, decision_id, data in cur.fetchall():
valid, message = validate_extraction(data)
if not valid:
print(f"No {decision_id}: {message}")
```
Additional shape checks:
```python
import re
def check_extraction_quality(extraction: dict) -> dict:
"""Run deterministic quality checks."""
checks = {
'has_decision_id': bool(extraction.get('decision_id')),
'decision_id_format': bool(re.match(r'^D\d+$', extraction.get('decision_id', ''))),
'has_headline': bool(extraction.get('headline')),
'headline_length': 10 <= len(extraction.get('headline', '')) <= 200,
'has_statement': bool(extraction.get('decision_statement')),
'statement_length': len(extraction.get('decision_statement', '')) > 20,
'valid_outcome': extraction.get('outcome') in ['approved', 'rejected', 'tabled', 'amended', None],
'has_ntee_code': bool(extraction.get('ntee_code')),
'ntee_code_format': bool(re.match(r'^[A-Z]\d{2}$', extraction.get('ntee_code', ''))),
}
checks['overall_quality'] = sum(checks.values()) / len(checks)
return checks
```
## Evaluation Metrics Summary
| Technique | Metric Name | What it Scores | Repository |
|-----------|-------------|----------------|------------|
| LLM-as-a-Judge | AnswerRelevancy | Does answer match the intent? | [DeepEval](https://github.com/confident-ai/deepeval) |
| Source Grounding | Faithfulness | Is answer supported by source docs? | [DeepEval](https://github.com/confident-ai/deepeval), [RAGAS](https://github.com/explodinggradients/ragas) |
| Logic/Reasoning | Coherence | Does answer make logical sense? | [DeepEval](https://github.com/confident-ai/deepeval) |
| Safety | Toxicity/Bias | Does output violate safety rubrics? | [DeepEval](https://github.com/confident-ai/deepeval) |
| Structure | Schema Validation | Does output match expected format? | [Pydantic](https://docs.pydantic.dev/) |
## Next Steps
1. **Implement Evaluation Pipeline**: Add these metrics to `scripts/datasources/gemini/evaluate_extractions.py`
2. **Compare Models**: Use `scripts/datasources/gemini/compare_model_extractions.py` with evaluation scores
3. **Build Consensus**: See [AI Model Merging](./ai-model-merging.md) for techniques to synthesize results
4. **Track Quality**: Store evaluation scores in a new `bronze_evaluation_scores` table
## Example: Full Evaluation Pipeline
```python
#!/usr/bin/env python3
"""
Evaluate bronze decision extractions using multiple metrics.
"""
from deepeval.metrics import AnswerRelevancyMetric, FaithfulnessMetric
from deepeval.test_case import LLMTestCase
import psycopg2
def evaluate_decision_extraction(event_id: int, decision_id: str):
"""Evaluate a decision extraction using multiple metrics."""
# Get extraction and source
cur.execute("""
SELECT
bd.source_ai_model,
bd.decision_statement,
bd.headline,
es.description as transcript
FROM bronze_decisions bd
JOIN event es ON es.event_id = bd.source_event_id
WHERE bd.source_event_id = %s
AND bd.decision_id = %s
""", (event_id, decision_id))
model, statement, headline, transcript = cur.fetchone()
# Test case
test_case = LLMTestCase(
input=f"Extract decision about: {headline}",
actual_output=statement,
retrieval_context=[transcript]
)
# Run metrics
relevancy = AnswerRelevancyMetric(threshold=0.7)
relevancy.measure(test_case)
faithfulness = FaithfulnessMetric(threshold=0.7)
faithfulness.measure(test_case)
# Deterministic checks
quality_checks = check_extraction_quality({
'decision_id': decision_id,
'headline': headline,
'decision_statement': statement
})
return {
'model': model,
'relevancy_score': relevancy.score,
'faithfulness_score': faithfulness.score,
'structure_quality': quality_checks['overall_quality'],
'passed_all': all([
relevancy.score >= 0.7,
faithfulness.score >= 0.7,
quality_checks['overall_quality'] >= 0.8
])
}
# Run evaluation
results = evaluate_decision_extraction(event_id=192614, decision_id='D001')
print(f"Model: {results['model']}")
print(f"Relevancy: {results['relevancy_score']:.2f}")
print(f"Faithfulness: {results['faithfulness_score']:.2f}")
print(f"Structure: {results['structure_quality']:.2f}")
print(f"Overall: {'Yes PASS' if results['passed_all'] else 'No FAIL'}")
```
## Resources
- [DeepEval Documentation](https://docs.confident-ai.com/)
- [RAGAS Documentation](https://docs.ragas.io/)
- [Prompt Engineering Guide - Evaluation](https://www.promptingguide.ai/introduction/evaluation)
- [OpenAI Evals Framework](https://github.com/openai/evals)
|