Repository navigation
Expand file tree
/
Copy patheval_dashboard.py
More file actions
194 lines (161 loc) · 6.76 KB
/
Copy patheval_dashboard.py
File metadata and controls
194 lines (161 loc) · 6.76 KB
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
import gradio as gr
import pandas as pd
from collections import defaultdict
from enum import Enum
from dotenv import load_dotenv
from evaluation.evaluate import evaluate_retrieval_all, evaluate_answer_all
from pipelines import DEFAULT_PIPELINE, PIPELINE_CHOICES
load_dotenv(override=True)
class MetricType(Enum):
MRR = "mrr"
NDCG = "ndcg"
KEYWORD_COVERAGE = "keyword-coverage"
ACCURACY = "accuracy"
COMPLETENESS = "completeness"
RELEVANCE = "relevance"
# Color coding thresholds: (green, amber). Below amber is red.
THRESHOLDS: dict[MetricType, tuple[float, float]] = {
MetricType.MRR: (0.9, 0.75),
MetricType.NDCG: (0.9, 0.75),
MetricType.KEYWORD_COVERAGE: (90.0, 75.0),
# Answer metrics use a 1-5 scale
MetricType.ACCURACY: (4.5, 4.0),
MetricType.COMPLETENESS: (4.5, 4.0),
MetricType.RELEVANCE: (4.5, 4.0),
}
PLACEHOLDER_HTML = (
"<div style='padding: 20px; text-align: center; color: #999;'>"
"Click 'Run Evaluation' to start</div>"
)
def metric_color(value: float, metric_type: MetricType) -> str:
green, amber = THRESHOLDS[metric_type]
if value >= green:
return "green"
if value >= amber:
return "orange"
return "red"
def format_metric_html(
label: str,
value: float,
metric_type: MetricType,
is_percentage: bool = False,
is_score: bool = False,
) -> str:
color = metric_color(value, metric_type)
if is_percentage:
value_str = f"{value:.1f}%"
elif is_score:
value_str = f"{value:.2f}/5"
else:
value_str = f"{value:.4f}"
return f"""
<div style="margin: 10px 0; padding: 15px; background-color: #f5f5f5; border-radius: 8px; border-left: 5px solid {color};">
<div style="font-size: 14px; color: #666; margin-bottom: 5px;">{label}</div>
<div style="font-size: 28px; font-weight: bold; color: {color};">{value_str}</div>
</div>
"""
def format_completion_html(test_case_count: int) -> str:
return f"""
<div style="margin-top: 20px; padding: 10px; background-color: #d4edda; border-radius: 5px; text-align: center; border: 1px solid #c3e6cb;">
<span style="font-size: 14px; color: #155724; font-weight: bold;">✓ Evaluation Complete: {test_case_count} test cases</span>
</div>
"""
def run_retrieval_evaluation(pipeline_name, progress=gr.Progress()):
total_mrr = 0.0
total_mean_ndcg = 0.0
total_keyword_coverage_percent = 0.0
category_mrr = defaultdict(list)
test_case_count = 0
for test_case, retrieval_eval, progress_value in evaluate_retrieval_all(pipeline_name):
test_case_count += 1
total_mrr += retrieval_eval.mrr
total_mean_ndcg += retrieval_eval.mean_ndcg
total_keyword_coverage_percent += retrieval_eval.keyword_coverage_percent
category_mrr[test_case.category].append(retrieval_eval.mrr)
progress(progress_value, desc=f"Evaluating test case {test_case_count}...")
metrics_html = f"""
<div style="padding: 0;">
{format_metric_html("Mean Reciprocal Rank (MRR)", total_mrr / test_case_count, MetricType.MRR)}
{format_metric_html("Normalized DCG (nDCG)", total_mean_ndcg / test_case_count, MetricType.NDCG)}
{format_metric_html("Keyword Coverage", total_keyword_coverage_percent / test_case_count, MetricType.KEYWORD_COVERAGE, is_percentage=True)}
{format_completion_html(test_case_count)}
</div>
"""
category_df = pd.DataFrame(
[
{"Category": category, "Average MRR": sum(scores) / len(scores)}
for category, scores in category_mrr.items()
]
)
return metrics_html, category_df
def run_answer_evaluation(pipeline_name, progress=gr.Progress()):
total_accuracy = 0.0
total_completeness = 0.0
total_relevance = 0.0
category_accuracy = defaultdict(list)
test_case_count = 0
for test_case, answer_eval, progress_value in evaluate_answer_all(pipeline_name):
test_case_count += 1
total_accuracy += answer_eval.accuracy
total_completeness += answer_eval.completeness
total_relevance += answer_eval.relevance
category_accuracy[test_case.category].append(answer_eval.accuracy)
progress(progress_value, desc=f"Evaluating test case {test_case_count}...")
metrics_html = f"""
<div style="padding: 0;">
{format_metric_html("Accuracy", total_accuracy / test_case_count, MetricType.ACCURACY, is_score=True)}
{format_metric_html("Completeness", total_completeness / test_case_count, MetricType.COMPLETENESS, is_score=True)}
{format_metric_html("Relevance", total_relevance / test_case_count, MetricType.RELEVANCE, is_score=True)}
{format_completion_html(test_case_count)}
</div>
"""
category_df = pd.DataFrame(
[
{"Category": category, "Average Accuracy": sum(scores) / len(scores)}
for category, scores in category_accuracy.items()
]
)
return metrics_html, category_df
def main():
theme = gr.themes.Soft(font=["Inter", "system-ui", "sans-serif"])
with gr.Blocks(title="RAG Evaluation Dashboard") as ui:
gr.Markdown("# RAG Evaluation Dashboard")
gr.Markdown("Evaluate retrieval and answer quality for the Insurellm RAG system")
pipeline_selector = gr.Radio(
choices=PIPELINE_CHOICES,
value=DEFAULT_PIPELINE,
label="RAG Pipeline",
)
gr.Markdown("## Retrieval Evaluation")
retrieval_button = gr.Button("Run Evaluation", variant="primary", size="lg")
with gr.Row():
with gr.Column(scale=1):
retrieval_metrics = gr.HTML(PLACEHOLDER_HTML)
with gr.Column(scale=1):
retrieval_chart = gr.BarPlot(
x="Category",
y="Average MRR",
title="Average MRR by Category",
y_lim=[0, 1],
height=400,
)
gr.Markdown("## Answer Evaluation")
answer_button = gr.Button("Run Evaluation", variant="primary", size="lg")
with gr.Row():
with gr.Column(scale=1):
answer_metrics = gr.HTML(PLACEHOLDER_HTML)
with gr.Column(scale=1):
answer_chart = gr.BarPlot(
x="Category",
y="Average Accuracy",
title="Average Accuracy by Category",
y_lim=[1, 5],
height=400,
)
retrieval_button.click(
fn=run_retrieval_evaluation, inputs=pipeline_selector, outputs=[retrieval_metrics, retrieval_chart]
)
answer_button.click(fn=run_answer_evaluation, inputs=pipeline_selector, outputs=[answer_metrics, answer_chart])
ui.launch(inbrowser=True, theme=theme)
if __name__ == "__main__":
main()