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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574 | class GuardrailEval:
"""Class for running guardrail evaluations."""
def __init__(
self,
config_path: Path,
dataset_path: Path,
stages: Sequence[str] | None = None,
batch_size: int = DEFAULT_BATCH_SIZE,
output_dir: Path = Path("results"),
api_key: str | None = None,
base_url: str | None = None,
azure_endpoint: str | None = None,
azure_api_version: str | None = None,
mode: str = "evaluate",
models: Sequence[str] | None = None,
latency_iterations: int = DEFAULT_LATENCY_ITERATIONS,
) -> None:
"""Initialize the evaluator.
Args:
config_path: Path to pipeline configuration file.
dataset_path: Path to evaluation dataset (JSONL).
stages: Specific stages to evaluate (pre_flight, input, output).
batch_size: Number of samples to process in parallel.
output_dir: Directory to save evaluation results.
api_key: API key for OpenAI, Azure OpenAI, or OpenAI-compatible API.
base_url: Base URL for OpenAI-compatible API (e.g., http://localhost:11434/v1).
azure_endpoint: Azure OpenAI endpoint (e.g., https://your-resource.openai.azure.com).
azure_api_version: Azure OpenAI API version (e.g., 2025-01-01-preview).
mode: Evaluation mode ("evaluate" or "benchmark").
models: Models to test in benchmark mode.
latency_iterations: Number of iterations for latency testing.
"""
self._validate_inputs(config_path, dataset_path, batch_size, mode, latency_iterations)
self.config_path = config_path
self.dataset_path = dataset_path
self.stages = stages
self.batch_size = batch_size
self.output_dir = output_dir
self.api_key = api_key
self.base_url = base_url
self.azure_endpoint = azure_endpoint
self.azure_api_version = azure_api_version or "2025-01-01-preview"
self.mode = mode
self.models = models or DEFAULT_BENCHMARK_MODELS
self.latency_iterations = latency_iterations
# Validate Azure configuration
if azure_endpoint and not AsyncAzureOpenAI:
raise ValueError(
"Azure OpenAI support requires openai>=1.0.0. "
"Please upgrade: pip install --upgrade openai"
)
def _validate_inputs(
self,
config_path: Path,
dataset_path: Path,
batch_size: int,
mode: str,
latency_iterations: int
) -> None:
"""Validate input parameters."""
if not config_path.exists():
raise ValueError(f"Config file not found: {config_path}")
if not dataset_path.exists():
raise ValueError(f"Dataset file not found: {dataset_path}")
if batch_size <= 0:
raise ValueError(f"Batch size must be positive, got: {batch_size}")
if mode not in ("evaluate", "benchmark"):
raise ValueError(f"Invalid mode: {mode}. Must be 'evaluate' or 'benchmark'")
if latency_iterations <= 0:
raise ValueError(f"Latency iterations must be positive, got: {latency_iterations}")
async def run(self) -> None:
"""Run the evaluation pipeline for all specified stages."""
try:
if self.mode == "benchmark":
await self._run_benchmark()
else:
await self._run_evaluation()
except Exception as e:
logger.error("Evaluation failed: %s", e)
raise
async def _run_evaluation(self) -> None:
"""Run standard evaluation mode."""
pipeline_bundles = load_pipeline_bundles(self.config_path)
stages_to_evaluate = self._get_valid_stages(pipeline_bundles)
if not stages_to_evaluate:
raise ValueError("No valid stages found in configuration")
logger.info("Evaluating stages: %s", ", ".join(stages_to_evaluate))
loader = JsonlDatasetLoader()
samples = loader.load(self.dataset_path)
logger.info("Loaded %d samples from dataset", len(samples))
context = self._create_context()
calculator = GuardrailMetricsCalculator()
reporter = JsonResultsReporter()
all_results = {}
all_metrics = {}
for stage in stages_to_evaluate:
logger.info("Starting %s stage evaluation", stage)
try:
stage_results = await self._evaluate_single_stage(
stage, pipeline_bundles, samples, context, calculator
)
if stage_results:
all_results[stage] = stage_results["results"]
all_metrics[stage] = stage_results["metrics"]
logger.info("Completed %s stage evaluation", stage)
else:
logger.warning("Stage '%s' evaluation returned no results", stage)
except Exception as e:
logger.error("Failed to evaluate stage '%s': %s", stage, e)
if not all_results:
raise ValueError("No stages were successfully evaluated")
reporter.save_multi_stage(all_results, all_metrics, self.output_dir)
logger.info("Evaluation completed. Results saved to: %s", self.output_dir)
async def _run_benchmark(self) -> None:
"""Run benchmark mode comparing multiple models."""
logger.info("Running benchmark mode with models: %s", ", ".join(self.models))
pipeline_bundles = load_pipeline_bundles(self.config_path)
stage_to_test, guardrail_name = self._get_benchmark_target(pipeline_bundles)
# Validate guardrail has model configuration
stage_bundle = getattr(pipeline_bundles, stage_to_test)
if not self._has_model_configuration(stage_bundle):
raise ValueError(f"Guardrail '{guardrail_name}' does not have a model configuration. "
"Benchmark mode requires LLM-based guardrails with configurable models.")
logger.info("Benchmarking guardrail '%s' from stage '%s'", guardrail_name, stage_to_test)
loader = JsonlDatasetLoader()
samples = loader.load(self.dataset_path)
logger.info("Loaded %d samples for benchmarking", len(samples))
context = self._create_context()
benchmark_calculator = BenchmarkMetricsCalculator()
basic_calculator = GuardrailMetricsCalculator()
benchmark_reporter = BenchmarkReporter(self.output_dir)
# Run benchmark for all models
results_by_model, metrics_by_model = await self._benchmark_all_models(
stage_to_test, guardrail_name, samples, context, benchmark_calculator, basic_calculator
)
# Run latency testing
logger.info("Running latency tests for all models")
latency_results = await self._run_latency_tests(stage_to_test, samples)
# Save benchmark results
benchmark_dir = benchmark_reporter.save_benchmark_results(
results_by_model,
metrics_by_model,
latency_results,
guardrail_name,
len(samples),
self.latency_iterations
)
# Create visualizations
logger.info("Generating visualizations")
visualizer = BenchmarkVisualizer(benchmark_dir / "graphs")
visualization_files = visualizer.create_all_visualizations(
results_by_model,
metrics_by_model,
latency_results,
guardrail_name,
samples[0].expected_triggers if samples else {}
)
logger.info("Benchmark completed. Results saved to: %s", benchmark_dir)
logger.info("Generated %d visualizations", len(visualization_files))
def _has_model_configuration(self, stage_bundle) -> bool:
"""Check if the guardrail has a model configuration."""
if not stage_bundle.guardrails:
return False
guardrail_config = stage_bundle.guardrails[0].config
if not guardrail_config:
return False
if isinstance(guardrail_config, dict) and 'model' in guardrail_config:
return True
elif hasattr(guardrail_config, 'model'):
return True
return False
async def _run_latency_tests(self, stage_to_test: str, samples: list) -> dict[str, Any]:
"""Run latency tests for all models."""
latency_results = {}
latency_tester = LatencyTester(iterations=self.latency_iterations)
for model in self.models:
model_stage_bundle = self._create_model_specific_stage_bundle(
getattr(load_pipeline_bundles(self.config_path), stage_to_test), model
)
model_context = self._create_context()
latency_results[model] = await latency_tester.test_guardrail_latency_for_model(
model_context,
model_stage_bundle,
samples,
self.latency_iterations,
desc=f"Testing latency: {model}",
)
return latency_results
def _create_context(self) -> Context:
"""Create evaluation context with OpenAI client.
Supports OpenAI, Azure OpenAI, and OpenAI-compatible APIs.
Used for both evaluation and benchmark modes.
Returns:
Context with configured AsyncOpenAI or AsyncAzureOpenAI client.
"""
# Azure OpenAI
if self.azure_endpoint:
if not AsyncAzureOpenAI:
raise ValueError(
"Azure OpenAI support requires openai>=1.0.0. "
"Please upgrade: pip install --upgrade openai"
)
azure_kwargs = {
"azure_endpoint": self.azure_endpoint,
"api_version": self.azure_api_version,
}
if self.api_key:
azure_kwargs["api_key"] = self.api_key
guardrail_llm = AsyncAzureOpenAI(**azure_kwargs)
logger.info("Created Azure OpenAI client for endpoint: %s", self.azure_endpoint)
# OpenAI or OpenAI-compatible API
else:
openai_kwargs = {}
if self.api_key:
openai_kwargs["api_key"] = self.api_key
if self.base_url:
openai_kwargs["base_url"] = self.base_url
logger.info("Created OpenAI-compatible client for base_url: %s", self.base_url)
guardrail_llm = AsyncOpenAI(**openai_kwargs)
return Context(guardrail_llm=guardrail_llm)
def _is_valid_stage(self, pipeline_bundles, stage: str) -> bool:
"""Check if a stage has valid guardrails configured.
Args:
pipeline_bundles: Pipeline bundles object.
stage: Stage name to check.
Returns:
True if stage exists and has guardrails configured.
"""
if not hasattr(pipeline_bundles, stage):
return False
stage_bundle = getattr(pipeline_bundles, stage)
return (
stage_bundle is not None
and hasattr(stage_bundle, 'guardrails')
and bool(stage_bundle.guardrails)
)
def _create_model_specific_stage_bundle(self, stage_bundle, model: str):
"""Create a deep copy of the stage bundle with model-specific configuration."""
try:
modified_bundle = copy.deepcopy(stage_bundle)
except Exception as e:
logger.error("Failed to create deep copy of stage bundle: %s", e)
raise ValueError(f"Failed to create deep copy of stage bundle: {e}") from e
logger.info("Creating model-specific configuration for model: %s", model)
guardrails_updated = 0
for guardrail in modified_bundle.guardrails:
try:
if hasattr(guardrail, 'config') and guardrail.config:
if isinstance(guardrail.config, dict) and 'model' in guardrail.config:
original_model = guardrail.config['model']
guardrail.config['model'] = model
logger.info("Updated guardrail '%s' model from '%s' to '%s'",
guardrail.name, original_model, model)
guardrails_updated += 1
elif hasattr(guardrail.config, 'model'):
original_model = getattr(guardrail.config, 'model', 'unknown')
guardrail.config.model = model
logger.info("Updated guardrail '%s' model from '%s' to '%s'",
guardrail.name, original_model, model)
guardrails_updated += 1
except Exception as e:
logger.error("Failed to update guardrail '%s' configuration: %s", guardrail.name, e)
raise ValueError(f"Failed to update guardrail '{guardrail.name}' configuration: {e}") from e
if guardrails_updated == 0:
logger.warning("No guardrails with model configuration were found")
else:
logger.info("Successfully updated %d guardrail(s) for model: %s", guardrails_updated, model)
return modified_bundle
def _get_valid_stages(self, pipeline_bundles) -> list[str]:
"""Get list of valid stages to evaluate."""
if self.stages is None:
# Auto-detect all valid stages
available_stages = [
stage for stage in VALID_STAGES
if self._is_valid_stage(pipeline_bundles, stage)
]
if not available_stages:
raise ValueError("No valid stages found in configuration")
logger.info("No stages specified, evaluating all available stages: %s", ", ".join(available_stages))
return available_stages
else:
# Validate requested stages
valid_requested_stages = []
for stage in self.stages:
if stage not in VALID_STAGES:
logger.warning("Invalid stage '%s', skipping", stage)
continue
if not self._is_valid_stage(pipeline_bundles, stage):
logger.warning("Stage '%s' not found or has no guardrails configured, skipping", stage)
continue
valid_requested_stages.append(stage)
if not valid_requested_stages:
raise ValueError("No valid stages found in configuration")
return valid_requested_stages
async def _evaluate_single_stage(
self,
stage: str,
pipeline_bundles,
samples: list,
context: Context,
calculator: GuardrailMetricsCalculator
) -> dict[str, Any] | None:
"""Evaluate a single pipeline stage."""
try:
stage_bundle = getattr(pipeline_bundles, stage)
guardrails = instantiate_guardrails(stage_bundle)
engine = AsyncRunEngine(guardrails)
stage_results = await engine.run(
context,
samples,
self.batch_size,
desc=f"Evaluating {stage} stage"
)
stage_metrics = calculator.calculate(stage_results)
return {
"results": stage_results,
"metrics": stage_metrics
}
except Exception as e:
logger.error("Failed to evaluate stage '%s': %s", stage, e)
return None
def _get_benchmark_target(self, pipeline_bundles) -> tuple[str, str]:
"""Get the stage and guardrail to benchmark."""
if self.stages:
stage_to_test = self.stages[0]
if not self._is_valid_stage(pipeline_bundles, stage_to_test):
raise ValueError(f"Stage '{stage_to_test}' has no guardrails configured")
else:
# Find first valid stage
stage_to_test = next(
(stage for stage in VALID_STAGES if self._is_valid_stage(pipeline_bundles, stage)),
None
)
if not stage_to_test:
raise ValueError("No valid stage found for benchmarking")
stage_bundle = getattr(pipeline_bundles, stage_to_test)
guardrail_name = stage_bundle.guardrails[0].name
return stage_to_test, guardrail_name
async def _benchmark_all_models(
self,
stage_to_test: str,
guardrail_name: str,
samples: list,
context: Context,
benchmark_calculator: BenchmarkMetricsCalculator,
basic_calculator: GuardrailMetricsCalculator
) -> tuple[dict[str, list], dict[str, dict]]:
"""Benchmark all models for the specified stage and guardrail."""
pipeline_bundles = load_pipeline_bundles(self.config_path)
stage_bundle = getattr(pipeline_bundles, stage_to_test)
results_by_model = {}
metrics_by_model = {}
for i, model in enumerate(self.models, 1):
logger.info("Testing model %d/%d: %s", i, len(self.models), model)
try:
modified_stage_bundle = self._create_model_specific_stage_bundle(stage_bundle, model)
model_results = await self._benchmark_single_model(
model, modified_stage_bundle, samples, context,
guardrail_name, benchmark_calculator, basic_calculator
)
if model_results:
results_by_model[model] = model_results["results"]
metrics_by_model[model] = model_results["metrics"]
logger.info("Completed benchmarking for model %s (%d/%d)", model, i, len(self.models))
else:
logger.warning("Model %s benchmark returned no results (%d/%d)", model, i, len(self.models))
results_by_model[model] = []
metrics_by_model[model] = {}
except Exception as e:
logger.error("Failed to benchmark model %s (%d/%d): %s", model, i, len(self.models), e)
results_by_model[model] = []
metrics_by_model[model] = {}
# Log summary
successful_models = [model for model, results in results_by_model.items() if results]
failed_models = [model for model, results in results_by_model.items() if not results]
logger.info("BENCHMARK SUMMARY")
logger.info("Successful models: %s", ", ".join(successful_models) if successful_models else "None")
if failed_models:
logger.warning("Failed models: %s", ", ".join(failed_models))
logger.info("Total models tested: %d", len(self.models))
return results_by_model, metrics_by_model
async def _benchmark_single_model(
self,
model: str,
stage_bundle,
samples: list,
context: Context,
guardrail_name: str,
benchmark_calculator: BenchmarkMetricsCalculator,
basic_calculator: GuardrailMetricsCalculator
) -> dict[str, Any] | None:
"""Benchmark a single model."""
try:
model_context = self._create_context()
guardrails = instantiate_guardrails(stage_bundle)
engine = AsyncRunEngine(guardrails)
model_results = await engine.run(
model_context,
samples,
self.batch_size,
desc=f"Benchmarking {model}"
)
guardrail_config = stage_bundle.guardrails[0].config if stage_bundle.guardrails else None
advanced_metrics = benchmark_calculator.calculate_advanced_metrics(
model_results, guardrail_name, guardrail_config
)
basic_metrics = basic_calculator.calculate(model_results)
if guardrail_name in basic_metrics:
guardrail_metrics = basic_metrics[guardrail_name]
basic_metrics_dict = {
"precision": guardrail_metrics.precision,
"recall": guardrail_metrics.recall,
"f1_score": guardrail_metrics.f1_score,
"true_positives": guardrail_metrics.true_positives,
"false_positives": guardrail_metrics.false_positives,
"false_negatives": guardrail_metrics.false_negatives,
"true_negatives": guardrail_metrics.true_negatives,
"total_samples": guardrail_metrics.total_samples,
}
else:
basic_metrics_dict = {}
combined_metrics = {**basic_metrics_dict, **advanced_metrics}
return {
"results": model_results,
"metrics": combined_metrics
}
except Exception as e:
logger.error("Failed to benchmark model %s: %s", model, e)
return None
|