-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathevaluation.py
More file actions
259 lines (223 loc) Β· 8.95 KB
/
evaluation.py
File metadata and controls
259 lines (223 loc) Β· 8.95 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
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
"""LightSpeed Evaluation Framework - Main Evaluation Runner."""
import argparse
import shutil
import sys
import traceback
from pathlib import Path
from typing import Any, Optional
from lightspeed_evaluation.core.models.system import LLMPoolConfig, SystemConfig
# Import only lightweight modules at top level
from lightspeed_evaluation.core.storage import FileBackendConfig, get_file_config
from lightspeed_evaluation.core.system import ConfigLoader
from lightspeed_evaluation.core.system.exceptions import (
ConfigurationError,
DataValidationError,
)
def _clear_caches(system_config: SystemConfig) -> None:
"""Clear all cache directories for warmup mode.
Args:
system_config: System configuration containing cache directory paths
"""
cache_dirs: list[tuple[str, str]] = []
# Collect all enabled cache directories
pool = system_config.llm_pool
if isinstance(pool, LLMPoolConfig) and pool.defaults.cache_enabled:
cache_dirs.append(("LLM Judge (pool)", pool.defaults.cache_dir))
if system_config.llm.cache_enabled:
cache_dirs.append(("LLM Judge", system_config.llm.cache_dir))
# We clear the api cache even if the Lightspeed core api is disabled
if system_config.api.cache_enabled:
cache_dirs.append(("API", system_config.api.cache_dir))
if system_config.embedding.cache_enabled:
cache_dirs.append(("Embedding", system_config.embedding.cache_dir))
if not cache_dirs:
print(" No caches enabled to clear")
return
# Clear each cache directory
for cache_name, cache_dir in cache_dirs:
path = Path(cache_dir)
resolved_path = path.resolve()
if resolved_path in {Path("/"), Path.cwd()}:
raise DataValidationError(
f"Refusing to delete unsafe cache directory: '{resolved_path}'"
)
if path.exists():
shutil.rmtree(path)
print(f" Cleared {cache_name} cache: {cache_dir}")
# Recreate empty directory
path.mkdir(parents=True, exist_ok=True)
def _print_summary(
summary: dict[str, Any],
api_tokens: Optional[dict[str, int]] = None,
) -> None:
"""Print evaluation summary and token usage."""
print(
f"β
Pass: {summary['PASS']}, β Fail: {summary['FAIL']}, "
f"β οΈ Error: {summary['ERROR']}, βοΈ Skipped: {summary['SKIPPED']}"
)
if summary["ERROR"] > 0:
print(f"β οΈ {summary['ERROR']} evaluations had errors - check detailed report")
print("\nπ Token Usage Summary:")
print(
f"Judge LLM: {summary['total_judge_llm_tokens']:,} tokens "
f"(Input: {summary['total_judge_llm_input_tokens']:,}, "
f"Output: {summary['total_judge_llm_output_tokens']:,})"
)
if api_tokens:
print(
f"API Calls: {api_tokens['total_api_tokens']:,} tokens "
f"(Input: {api_tokens['total_api_input_tokens']:,}, "
f"Output: {api_tokens['total_api_output_tokens']:,})"
)
total = summary["total_judge_llm_tokens"] + api_tokens["total_api_tokens"]
print(f"Total: {total:,} tokens")
def run_evaluation( # pylint: disable=too-many-locals
eval_args: argparse.Namespace,
) -> Optional[dict[str, int]]:
"""Run the complete evaluation pipeline.
Args:
eval_args: Parsed command line arguments
Returns:
dict: Summary statistics with keys TOTAL, PASS, FAIL, ERROR, SKIPPED
"""
print("π LightSpeed Evaluation Framework")
print("=" * 50)
try:
print("π§ Loading Configuration & Setting up environment...")
loader = ConfigLoader()
system_config = loader.load_system_config(eval_args.system_config)
# Clear caches if cache warmup mode is enabled
if eval_args.cache_warmup:
print("\nπ₯ Cache warmup mode: Clearing existing caches...")
_clear_caches(system_config)
# Import heavy modules after environment is configured
print("\nπ Loading Heavy Modules...")
# pylint: disable=import-outside-toplevel
from lightspeed_evaluation.api import evaluate
from lightspeed_evaluation.core.output import OutputHandler
from lightspeed_evaluation.core.output.statistics import (
calculate_api_token_usage,
calculate_basic_stats,
)
from lightspeed_evaluation.core.system import DataValidator
# pylint: enable=import-outside-toplevel
print("β
Configuration loaded & Setup is done !")
# Load, filter, and validate evaluation data
evaluation_data = DataValidator(
api_enabled=system_config.api.enabled,
fail_on_invalid_data=system_config.core.fail_on_invalid_data,
system_config=system_config,
).load_evaluation_data(
eval_args.eval_data,
tags=eval_args.tags,
conv_ids=eval_args.conv_ids,
metrics=eval_args.metrics,
)
print(
f"β
System config: {system_config.llm.provider}/{system_config.llm.model}"
)
# Handle case where no conversations match the filter
if len(evaluation_data) == 0:
print("\nβ οΈ No conversation groups matched the filter criteria")
print(" Nothing to evaluate - returning empty results")
return {"TOTAL": 0, "PASS": 0, "FAIL": 0, "ERROR": 0, "SKIPPED": 0}
# Run evaluation pipeline
print("\nβοΈ Initializing Evaluation Pipeline...")
print("\nπ Running Evaluation...")
results = evaluate(
system_config, evaluation_data, output_dir=eval_args.output_dir
)
file_entries = [
c for c in system_config.storage if isinstance(c, FileBackendConfig)
]
if not file_entries:
# No file storage in config: use legacy default file settings (same as get_file_config).
print("\nπ Generating Reports...")
file_config = get_file_config(system_config.storage)
output_handler = OutputHandler(
output_dir=eval_args.output_dir or file_config.output_dir,
base_filename=file_config.base_filename,
system_config=system_config,
file_config=file_config,
)
output_handler.generate_reports(results, evaluation_data)
print("\nπ Evaluation Complete!")
print(f"π {len(results)} evaluations completed")
for fc in file_entries:
report_dir = Path(eval_args.output_dir or fc.output_dir).resolve()
print(f"π Reports generated in: {report_dir}")
if not file_entries:
out_dir = Path(
eval_args.output_dir
or get_file_config(system_config.storage).output_dir
).resolve()
print(f"π Reports generated in: {out_dir}")
# Final Summary
summary = calculate_basic_stats(results)
api_tokens = (
calculate_api_token_usage(evaluation_data)
if system_config.api.enabled
else None
)
_print_summary(summary, api_tokens)
return {
"TOTAL": summary["TOTAL"],
"PASS": summary["PASS"],
"FAIL": summary["FAIL"],
"ERROR": summary["ERROR"],
"SKIPPED": summary["SKIPPED"],
}
except (
FileNotFoundError,
ValueError,
RuntimeError,
ConfigurationError,
DataValidationError,
) as e:
print(f"\nβ Evaluation failed: {e}")
traceback.print_exc()
return None
def main() -> int:
"""Command line interface."""
parser = argparse.ArgumentParser(
description="LightSpeed Evaluation Framework / Tool",
)
parser.add_argument(
"--system-config",
default="config/system.yaml",
help="Path to system configuration file (default: config/system.yaml)",
)
parser.add_argument(
"--eval-data",
default="config/evaluation_data.yaml",
help="Path to evaluation data file (default: config/evaluation_data.yaml)",
)
parser.add_argument("--output-dir", help="Override output directory (optional)")
parser.add_argument(
"--tags",
nargs="+",
default=None,
help="Filter by tags (run conversation groups with matching tags)",
)
parser.add_argument(
"--conv-ids",
nargs="+",
default=None,
help="Filter by conversation group IDs (run only specified conversations)",
)
parser.add_argument(
"--metrics",
nargs="+",
default=None,
help="Filter to only run specified metrics (e.g. custom:answer_correctness)",
)
parser.add_argument(
"--cache-warmup",
action="store_true",
help="Enable cache warmup mode - rebuild caches without reading existing entries",
)
eval_args = parser.parse_args()
summary = run_evaluation(eval_args)
return 0 if summary is not None else 1
if __name__ == "__main__":
sys.exit(main())