Upload 5 files
Browse filesA Comprehensive Rag Score Calculation Metric Added
- app.py +1 -1
- src/.DS_Store +0 -0
- src/display/about.py +15 -5
- src/utils.py +63 -7
- utils/rag_score_calculator.py +171 -0
app.py
CHANGED
|
@@ -267,7 +267,7 @@ def create_demo():
|
|
| 267 |
value=rag_details_df,
|
| 268 |
label="Retrieval Detailed Results",
|
| 269 |
interactive=False,
|
| 270 |
-
column_widths=["
|
| 271 |
|
| 272 |
)
|
| 273 |
|
|
|
|
| 267 |
value=rag_details_df,
|
| 268 |
label="Retrieval Detailed Results",
|
| 269 |
interactive=False,
|
| 270 |
+
column_widths=["280px", "120px", "140px", "140px", "140px", "120px", "160px", "100px", "120px"]
|
| 271 |
|
| 272 |
)
|
| 273 |
|
src/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
src/display/about.py
CHANGED
|
@@ -199,11 +199,21 @@ An evaluation system designed to assess Retrieval-Augmented Generation (RAG) cap
|
|
| 199 |
- **Content Safety**: Evaluates content safety and appropriateness
|
| 200 |
|
| 201 |
**Judge Model**: nvidia/Llama-3_1-Nemotron-Ultra-253B-v1
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
### 6. 👥 Human Arena
|
| 209 |
Human Arena is a community-driven evaluation platform where language models are compared through human preferences and voting. This evaluation method captures real-world user preferences and provides insights into model performance from a human perspective.
|
|
|
|
| 199 |
- **Content Safety**: Evaluates content safety and appropriateness
|
| 200 |
|
| 201 |
**Judge Model**: nvidia/Llama-3_1-Nemotron-Ultra-253B-v1
|
| 202 |
+
|
| 203 |
+
**RAG Score Calculation**
|
| 204 |
+
The RAG Score is a comprehensive metric that combines multiple performance indicators using dynamic normalization across all models. The formula weights different aspects of retrieval performance:
|
| 205 |
+
|
| 206 |
+
**Formula Components:**
|
| 207 |
+
- **RAG Success Rate** (0.9 weight): Direct percentage of successful retrievals (higher is better)
|
| 208 |
+
- **Normalized False Positives** (0.9 weight): Hallucinated references, min-max normalized (lower is better)
|
| 209 |
+
- **Normalized Max Correct References** (0.1 weight): Maximum correct retrievals, min-max normalized (higher is better)
|
| 210 |
+
- **Normalized Missed References** (0.1 weight): Relevant documents not retrieved, min-max normalized (lower is better)
|
| 211 |
+
|
| 212 |
+
**Final Score Formula:**
|
| 213 |
+
```
|
| 214 |
+
RAG Score = (0.9 × RAG_success_rate + 0.9 × norm_false_positives +
|
| 215 |
+
0.1 × norm_max_correct + 0.1 × norm_missed_refs) ÷ 2.0
|
| 216 |
+
```
|
| 217 |
|
| 218 |
### 6. 👥 Human Arena
|
| 219 |
Human Arena is a community-driven evaluation platform where language models are compared through human preferences and voting. This evaluation method captures real-world user preferences and provides insights into model performance from a human perspective.
|
src/utils.py
CHANGED
|
@@ -12,6 +12,7 @@ import requests
|
|
| 12 |
import logging
|
| 13 |
from datetime import datetime
|
| 14 |
from dotenv import load_dotenv
|
|
|
|
| 15 |
|
| 16 |
# Logger setup
|
| 17 |
logger = logging.getLogger("mezura.utils")
|
|
@@ -144,6 +145,26 @@ def load_benchmark_results():
|
|
| 144 |
# Define benchmark types to look for
|
| 145 |
benchmark_types = ["evalmix", "light_eval", "snake", "retrieval", "arena", "human_arena"] # "lm_harness" removed
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
# Load raw JSON files (detailed results)
|
| 148 |
for benchmark_type in benchmark_types:
|
| 149 |
dir_path = f"result/{benchmark_type}"
|
|
@@ -202,6 +223,15 @@ def load_benchmark_results():
|
|
| 202 |
if "model_name" in data:
|
| 203 |
data["model_name"] = format_model_name(data["model_name"])
|
| 204 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
results["avg"][benchmark_type].append(data)
|
| 206 |
except Exception as e:
|
| 207 |
print(f"Error loading {benchmark_type} avg file: {file} - {e}")
|
|
@@ -254,6 +284,15 @@ def load_benchmark_results():
|
|
| 254 |
if "model_name" in data:
|
| 255 |
data["model_name"] = format_model_name(data["model_name"])
|
| 256 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
results["raw"][benchmark_type].append(data)
|
| 258 |
|
| 259 |
# Also add to default results to ensure we have all models in the leaderboard
|
|
@@ -263,7 +302,10 @@ def load_benchmark_results():
|
|
| 263 |
|
| 264 |
# Extract key metrics based on benchmark type
|
| 265 |
if benchmark_type == "retrieval":
|
| 266 |
-
# For RAG Judge, extract RAG_success_rate and average_judge_score if available
|
|
|
|
|
|
|
|
|
|
| 267 |
if "RAG_success_rate" in data:
|
| 268 |
simplified_data["RAG_success_rate"] = data["RAG_success_rate"]
|
| 269 |
if "average_judge_score" in data:
|
|
@@ -655,8 +697,8 @@ def create_combined_leaderboard_table(benchmark_data):
|
|
| 655 |
|
| 656 |
# Process each benchmark type - exclude snake
|
| 657 |
for benchmark_type in benchmark_types:
|
| 658 |
-
# For human_arena, use raw data since
|
| 659 |
-
if benchmark_type
|
| 660 |
data_source = benchmark_data["raw"][benchmark_type]
|
| 661 |
else:
|
| 662 |
data_source = benchmark_data["avg"][benchmark_type]
|
|
@@ -730,8 +772,12 @@ def create_combined_leaderboard_table(benchmark_data):
|
|
| 730 |
all_models[formatted_model_name]["Light Eval"] = item["overall_average"]
|
| 731 |
# Remove dtype and license from JSON - use only lookup table values
|
| 732 |
elif benchmark_type == "retrieval":
|
| 733 |
-
if
|
| 734 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 735 |
all_models[formatted_model_name]["Retrieval"] = round(avg_value, 2)
|
| 736 |
# Remove dtype and license from JSON - use only lookup table values
|
| 737 |
elif benchmark_type == "arena":
|
|
@@ -911,6 +957,7 @@ def create_raw_details_table(benchmark_data, benchmark_type):
|
|
| 911 |
elif benchmark_type == "retrieval":
|
| 912 |
# RAG benchmark column mappings
|
| 913 |
custom_columns = {
|
|
|
|
| 914 |
"RAG_success_rate": "Rag Success Rate",
|
| 915 |
"max_correct_references": "Max Correct Ref.",
|
| 916 |
"total_false_positives": "Hallucinate Ref.",
|
|
@@ -1023,12 +1070,21 @@ def create_raw_details_table(benchmark_data, benchmark_type):
|
|
| 1023 |
# Set the new column order
|
| 1024 |
df = df[final_cols]
|
| 1025 |
|
| 1026 |
-
elif benchmark_type == "retrieval"
|
| 1027 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1028 |
|
| 1029 |
# Define desired column order for Retrieval - metadata columns at the end
|
| 1030 |
desired_cols = [
|
| 1031 |
"Model Name",
|
|
|
|
| 1032 |
"Rag Success Rate",
|
| 1033 |
"Max Correct Ref.",
|
| 1034 |
"Hallucinate Ref.",
|
|
|
|
| 12 |
import logging
|
| 13 |
from datetime import datetime
|
| 14 |
from dotenv import load_dotenv
|
| 15 |
+
from utils.rag_score_calculator import RAGScoreCalculator
|
| 16 |
|
| 17 |
# Logger setup
|
| 18 |
logger = logging.getLogger("mezura.utils")
|
|
|
|
| 145 |
# Define benchmark types to look for
|
| 146 |
benchmark_types = ["evalmix", "light_eval", "snake", "retrieval", "arena", "human_arena"] # "lm_harness" removed
|
| 147 |
|
| 148 |
+
# Initialize RAG Score calculator for runtime calculation
|
| 149 |
+
rag_calculator = None
|
| 150 |
+
rag_scores_cache = {} # Cache for RAG scores by run_id
|
| 151 |
+
try:
|
| 152 |
+
rag_calculator = RAGScoreCalculator()
|
| 153 |
+
if rag_calculator.stats:
|
| 154 |
+
logger.info("RAG Score calculator initialized successfully")
|
| 155 |
+
# Pre-calculate RAG scores from detail files
|
| 156 |
+
for data in rag_calculator.all_data:
|
| 157 |
+
run_id = data.get('run_id')
|
| 158 |
+
if run_id:
|
| 159 |
+
rag_score = rag_calculator.calculate_rag_score(data)
|
| 160 |
+
rag_scores_cache[run_id] = rag_score
|
| 161 |
+
logger.info(f"Pre-calculated {len(rag_scores_cache)} RAG scores")
|
| 162 |
+
else:
|
| 163 |
+
logger.warning("No RAG statistics available for score calculation")
|
| 164 |
+
except Exception as e:
|
| 165 |
+
logger.warning(f"Could not initialize RAG Score calculator: {e}")
|
| 166 |
+
rag_calculator = None
|
| 167 |
+
|
| 168 |
# Load raw JSON files (detailed results)
|
| 169 |
for benchmark_type in benchmark_types:
|
| 170 |
dir_path = f"result/{benchmark_type}"
|
|
|
|
| 223 |
if "model_name" in data:
|
| 224 |
data["model_name"] = format_model_name(data["model_name"])
|
| 225 |
|
| 226 |
+
# Add pre-calculated RAG Score for retrieval data (from detail files cache)
|
| 227 |
+
if benchmark_type == "retrieval" and rag_scores_cache:
|
| 228 |
+
run_id = data.get('run_id')
|
| 229 |
+
if run_id and run_id in rag_scores_cache:
|
| 230 |
+
data["RAG_score"] = rag_scores_cache[run_id]
|
| 231 |
+
logger.debug(f"Added cached RAG_score {rag_scores_cache[run_id]} for avg file {data.get('model_name', 'unknown')}")
|
| 232 |
+
else:
|
| 233 |
+
logger.debug(f"No cached RAG_score found for run_id: {run_id}")
|
| 234 |
+
|
| 235 |
results["avg"][benchmark_type].append(data)
|
| 236 |
except Exception as e:
|
| 237 |
print(f"Error loading {benchmark_type} avg file: {file} - {e}")
|
|
|
|
| 284 |
if "model_name" in data:
|
| 285 |
data["model_name"] = format_model_name(data["model_name"])
|
| 286 |
|
| 287 |
+
# Add pre-calculated RAG Score for retrieval data (from cache)
|
| 288 |
+
if benchmark_type == "retrieval" and rag_scores_cache:
|
| 289 |
+
run_id = data.get('run_id')
|
| 290 |
+
if run_id and run_id in rag_scores_cache:
|
| 291 |
+
data["RAG_score"] = rag_scores_cache[run_id]
|
| 292 |
+
logger.debug(f"Added cached RAG_score {rag_scores_cache[run_id]} for detail file {data.get('model_name', 'unknown')}")
|
| 293 |
+
else:
|
| 294 |
+
logger.debug(f"No cached RAG_score found for detail run_id: {run_id}")
|
| 295 |
+
|
| 296 |
results["raw"][benchmark_type].append(data)
|
| 297 |
|
| 298 |
# Also add to default results to ensure we have all models in the leaderboard
|
|
|
|
| 302 |
|
| 303 |
# Extract key metrics based on benchmark type
|
| 304 |
if benchmark_type == "retrieval":
|
| 305 |
+
# For RAG Judge, extract RAG_score, RAG_success_rate and average_judge_score if available
|
| 306 |
+
# RAG_score should be available since we just calculated it above
|
| 307 |
+
if "RAG_score" in data:
|
| 308 |
+
simplified_data["RAG_score"] = data["RAG_score"]
|
| 309 |
if "RAG_success_rate" in data:
|
| 310 |
simplified_data["RAG_success_rate"] = data["RAG_success_rate"]
|
| 311 |
if "average_judge_score" in data:
|
|
|
|
| 697 |
|
| 698 |
# Process each benchmark type - exclude snake
|
| 699 |
for benchmark_type in benchmark_types:
|
| 700 |
+
# For human_arena and retrieval, use raw data since avg files don't have complete info
|
| 701 |
+
if benchmark_type in ["human_arena", "retrieval"]:
|
| 702 |
data_source = benchmark_data["raw"][benchmark_type]
|
| 703 |
else:
|
| 704 |
data_source = benchmark_data["avg"][benchmark_type]
|
|
|
|
| 772 |
all_models[formatted_model_name]["Light Eval"] = item["overall_average"]
|
| 773 |
# Remove dtype and license from JSON - use only lookup table values
|
| 774 |
elif benchmark_type == "retrieval":
|
| 775 |
+
# Prefer RAG_score if available, otherwise use RAG_success_rate
|
| 776 |
+
if "RAG_score" in item:
|
| 777 |
+
avg_value = item["RAG_score"]
|
| 778 |
+
all_models[formatted_model_name]["Retrieval"] = round(avg_value, 4) # Higher precision for RAG Score
|
| 779 |
+
elif "RAG_success_rate" in item:
|
| 780 |
+
avg_value = item["RAG_success_rate"]
|
| 781 |
all_models[formatted_model_name]["Retrieval"] = round(avg_value, 2)
|
| 782 |
# Remove dtype and license from JSON - use only lookup table values
|
| 783 |
elif benchmark_type == "arena":
|
|
|
|
| 957 |
elif benchmark_type == "retrieval":
|
| 958 |
# RAG benchmark column mappings
|
| 959 |
custom_columns = {
|
| 960 |
+
"RAG_score": "RAG Score",
|
| 961 |
"RAG_success_rate": "Rag Success Rate",
|
| 962 |
"max_correct_references": "Max Correct Ref.",
|
| 963 |
"total_false_positives": "Hallucinate Ref.",
|
|
|
|
| 1070 |
# Set the new column order
|
| 1071 |
df = df[final_cols]
|
| 1072 |
|
| 1073 |
+
elif benchmark_type == "retrieval":
|
| 1074 |
+
# Sort by RAG Score if available, otherwise by Rag Success Rate
|
| 1075 |
+
if "RAG Score" in df.columns:
|
| 1076 |
+
df = df.sort_values(by="RAG Score", ascending=False)
|
| 1077 |
+
primary_metric = "RAG Score"
|
| 1078 |
+
elif "Rag Success Rate" in df.columns:
|
| 1079 |
+
df = df.sort_values(by="Rag Success Rate", ascending=False)
|
| 1080 |
+
primary_metric = "Rag Success Rate"
|
| 1081 |
+
else:
|
| 1082 |
+
primary_metric = None
|
| 1083 |
|
| 1084 |
# Define desired column order for Retrieval - metadata columns at the end
|
| 1085 |
desired_cols = [
|
| 1086 |
"Model Name",
|
| 1087 |
+
"RAG Score",
|
| 1088 |
"Rag Success Rate",
|
| 1089 |
"Max Correct Ref.",
|
| 1090 |
"Hallucinate Ref.",
|
utils/rag_score_calculator.py
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
from typing import Dict, List, Tuple
|
| 4 |
+
|
| 5 |
+
class RAGScoreCalculator:
|
| 6 |
+
"""
|
| 7 |
+
Dynamic RAG Score calculator that calculates scores at runtime
|
| 8 |
+
without modifying the original JSON files.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
def __init__(self, retrieval_dir: str = "result/retrieval"):
|
| 12 |
+
self.retrieval_dir = retrieval_dir
|
| 13 |
+
self.stats = None
|
| 14 |
+
self.all_data = None
|
| 15 |
+
self._load_and_analyze()
|
| 16 |
+
|
| 17 |
+
def _load_and_analyze(self):
|
| 18 |
+
"""Load all retrieval detail files and calculate normalization statistics."""
|
| 19 |
+
self.all_data = []
|
| 20 |
+
detail_files = [f for f in os.listdir(self.retrieval_dir) if f.startswith('detail_')]
|
| 21 |
+
|
| 22 |
+
if not detail_files:
|
| 23 |
+
print("Warning: No detail files found in retrieval directory")
|
| 24 |
+
return
|
| 25 |
+
|
| 26 |
+
for filename in detail_files:
|
| 27 |
+
filepath = os.path.join(self.retrieval_dir, filename)
|
| 28 |
+
try:
|
| 29 |
+
with open(filepath, 'r') as f:
|
| 30 |
+
data = json.load(f)
|
| 31 |
+
self.all_data.append(data)
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"Error loading {filename}: {e}")
|
| 34 |
+
continue
|
| 35 |
+
|
| 36 |
+
if not self.all_data:
|
| 37 |
+
print("Warning: No valid data loaded from detail files")
|
| 38 |
+
return
|
| 39 |
+
|
| 40 |
+
# Calculate normalization statistics
|
| 41 |
+
self._calculate_stats()
|
| 42 |
+
|
| 43 |
+
def _calculate_stats(self):
|
| 44 |
+
"""Calculate min/max statistics for normalization."""
|
| 45 |
+
if not self.all_data:
|
| 46 |
+
return
|
| 47 |
+
|
| 48 |
+
# Extract values for analysis
|
| 49 |
+
rag_success_rates = [d.get('RAG_success_rate', 0) for d in self.all_data]
|
| 50 |
+
max_correct_refs = [d.get('max_correct_references', 0) for d in self.all_data]
|
| 51 |
+
false_positives = [d.get('total_false_positives', 0) for d in self.all_data]
|
| 52 |
+
missed_refs = [d.get('total_missed_references', 0) for d in self.all_data]
|
| 53 |
+
|
| 54 |
+
# Calculate min/max for normalization
|
| 55 |
+
self.stats = {
|
| 56 |
+
'rag_success_rate': {
|
| 57 |
+
'min': min(rag_success_rates),
|
| 58 |
+
'max': max(rag_success_rates)
|
| 59 |
+
},
|
| 60 |
+
'max_correct_references': {
|
| 61 |
+
'min': min(max_correct_refs),
|
| 62 |
+
'max': max(max_correct_refs)
|
| 63 |
+
},
|
| 64 |
+
'total_false_positives': {
|
| 65 |
+
'min': min(false_positives),
|
| 66 |
+
'max': max(false_positives)
|
| 67 |
+
},
|
| 68 |
+
'total_missed_references': {
|
| 69 |
+
'min': 0, # Fixed minimum value
|
| 70 |
+
'max': 7114 # Fixed maximum value
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
def normalize_value(self, value, min_val, max_val, higher_is_better=True):
|
| 75 |
+
"""Normalize a value to 0-1 range."""
|
| 76 |
+
if max_val == min_val:
|
| 77 |
+
return 1.0 # If all values are the same, return 1
|
| 78 |
+
|
| 79 |
+
normalized = (value - min_val) / (max_val - min_val)
|
| 80 |
+
|
| 81 |
+
if not higher_is_better:
|
| 82 |
+
normalized = 1 - normalized # Flip for "lower is better" metrics
|
| 83 |
+
|
| 84 |
+
return normalized
|
| 85 |
+
|
| 86 |
+
def calculate_rag_score(self, data: Dict) -> float:
|
| 87 |
+
"""Calculate the RAG score for a single model's data."""
|
| 88 |
+
if not self.stats:
|
| 89 |
+
print("Warning: No statistics available for normalization")
|
| 90 |
+
return 0.0
|
| 91 |
+
|
| 92 |
+
# Extract values with defaults
|
| 93 |
+
rag_success_rate = data.get('RAG_success_rate', 0)
|
| 94 |
+
max_correct_refs = data.get('max_correct_references', 0)
|
| 95 |
+
false_positives = data.get('total_false_positives', 0)
|
| 96 |
+
missed_refs = data.get('total_missed_references', 0)
|
| 97 |
+
|
| 98 |
+
# Normalize values (0-1)
|
| 99 |
+
norm_max_correct = self.normalize_value(
|
| 100 |
+
max_correct_refs,
|
| 101 |
+
self.stats['max_correct_references']['min'],
|
| 102 |
+
self.stats['max_correct_references']['max'],
|
| 103 |
+
higher_is_better=True
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
norm_false_positives = self.normalize_value(
|
| 107 |
+
false_positives,
|
| 108 |
+
self.stats['total_false_positives']['min'],
|
| 109 |
+
self.stats['total_false_positives']['max'],
|
| 110 |
+
higher_is_better=False # Lower is better
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
norm_missed_refs = self.normalize_value(
|
| 114 |
+
missed_refs,
|
| 115 |
+
self.stats['total_missed_references']['min'],
|
| 116 |
+
self.stats['total_missed_references']['max'],
|
| 117 |
+
higher_is_better=False # Lower is better
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
# Calculate weighted score
|
| 121 |
+
# Weights: rag_success_rate=0.9, false_positives=0.9, max_correct=0.1, missed_refs=0.1
|
| 122 |
+
rag_score = (
|
| 123 |
+
0.9 * rag_success_rate +
|
| 124 |
+
0.9 * norm_false_positives +
|
| 125 |
+
0.1 * norm_max_correct +
|
| 126 |
+
0.1 * norm_missed_refs
|
| 127 |
+
) / 2.0 # Divide by 2 since total weights = 2.0
|
| 128 |
+
|
| 129 |
+
return round(rag_score, 4)
|
| 130 |
+
|
| 131 |
+
def get_normalization_info(self) -> Dict:
|
| 132 |
+
"""Get current normalization statistics for debugging."""
|
| 133 |
+
return {
|
| 134 |
+
'stats': self.stats,
|
| 135 |
+
'total_files': len(self.all_data) if self.all_data else 0,
|
| 136 |
+
'retrieval_dir': self.retrieval_dir
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
def refresh_stats(self):
|
| 140 |
+
"""Refresh statistics by reloading data - call this when new data is added."""
|
| 141 |
+
print("Refreshing RAG Score normalization statistics...")
|
| 142 |
+
self._load_and_analyze()
|
| 143 |
+
return self.stats is not None
|
| 144 |
+
|
| 145 |
+
def main():
|
| 146 |
+
"""Main function for testing RAG score calculations."""
|
| 147 |
+
calculator = RAGScoreCalculator()
|
| 148 |
+
|
| 149 |
+
print("RAG Score Calculator (Runtime Only)")
|
| 150 |
+
print("===================================")
|
| 151 |
+
|
| 152 |
+
# Show normalization info
|
| 153 |
+
info = calculator.get_normalization_info()
|
| 154 |
+
print(f"Total files: {info['total_files']}")
|
| 155 |
+
print(f"Retrieval directory: {info['retrieval_dir']}")
|
| 156 |
+
|
| 157 |
+
if info['stats']:
|
| 158 |
+
print("\nNormalization ranges:")
|
| 159 |
+
for metric, data in info['stats'].items():
|
| 160 |
+
print(f" {metric}: {data['min']} - {data['max']}")
|
| 161 |
+
|
| 162 |
+
print("\nSample RAG Score calculations:")
|
| 163 |
+
for i, data in enumerate(calculator.all_data[:5]): # Show first 5
|
| 164 |
+
rag_score = calculator.calculate_rag_score(data)
|
| 165 |
+
model_name = data.get('model_name', 'Unknown')
|
| 166 |
+
print(f" {model_name}: {rag_score}")
|
| 167 |
+
else:
|
| 168 |
+
print("\n❌ No statistics available for normalization")
|
| 169 |
+
|
| 170 |
+
if __name__ == "__main__":
|
| 171 |
+
main()
|