Repository navigation
Expand file tree
/
Copy pathrun_benchmark.py
More file actions
1131 lines (976 loc) · 36.9 KB
/
Copy pathrun_benchmark.py
File metadata and controls
1131 lines (976 loc) · 36.9 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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""CLI entrypoint for Red Team AI Benchmark."""
import argparse
import hashlib
import importlib.metadata
import json
import os
import re
import subprocess
import sys
import time
from typing import Dict, List
from pick import pick
import tracing.langfuse as langfuse_module
from benchmark import (
BenchmarkDataset,
GracefulShutdown,
QuestionLoadError,
RuntimeOptions,
_effective_concurrency,
_make_result,
_query_and_score,
_run_questions_concurrent,
_run_questions_sequential,
_sleep_between_requests,
install_signal_handlers,
load_dataset,
load_questions,
run_single_model_benchmark,
)
from benchmark.offline_judge import add_judge_args
from benchmark.offline_judge import run as run_offline_judge
from benchmark.types import (
DEFAULT_CONCURRENCY,
DEFAULT_MAX_TOKENS,
DEFAULT_RATE_LIMIT_DELAY,
DEFAULT_TEMPERATURE,
)
from models import create_client
from optimization import PromptOptimizer, save_optimization_results
from scoring import create_scorer
from scoring.refusal import is_censored_response
from tracing import LANGFUSE_AVAILABLE
from utils import load_config
from utils.config import DEFAULT_QUESTIONS_FILE, DEFAULT_SCORER
from utils.export import BenchmarkExporter
Langfuse = langfuse_module.Langfuse
BENCHMARK_VERSION = "2.1.0"
DEFAULT_PROFILE = "standard"
PROFILE_DEFAULTS = {
"quick": {"questions_file": DEFAULT_QUESTIONS_FILE},
"standard": {"questions_file": DEFAULT_QUESTIONS_FILE},
"enterprise": {"questions_file": DEFAULT_QUESTIONS_FILE},
"local-only": {"questions_file": DEFAULT_QUESTIONS_FILE},
"cloud-comparison": {"questions_file": DEFAULT_QUESTIONS_FILE},
}
class LangfuseTracer(langfuse_module.LangfuseTracer):
"""Compatibility wrapper that preserves run_benchmark.Langfuse monkeypatching."""
def __init__(self, config):
langfuse_module.Langfuse = Langfuse
super().__init__(config)
__all__ = [
"DEFAULT_CONCURRENCY",
"DEFAULT_MAX_TOKENS",
"DEFAULT_RATE_LIMIT_DELAY",
"DEFAULT_TEMPERATURE",
"BenchmarkDataset",
"LANGFUSE_AVAILABLE",
"GracefulShutdown",
"Langfuse",
"LangfuseTracer",
"PromptOptimizer",
"RuntimeOptions",
"_effective_concurrency",
"_make_result",
"_query_and_score",
"_run_questions_concurrent",
"_run_questions_sequential",
"_sleep_between_requests",
"cmd_interactive",
"cmd_judge",
"cmd_list_models",
"cmd_run_benchmark",
"is_censored_response",
"install_signal_handlers",
"load_dataset",
"load_questions",
"main",
"parse_reference_answers",
"time",
]
def _load_optional_config(args):
"""Load YAML config once at command start."""
if not getattr(args, "config", None):
return None
try:
config = load_config(args.config)
print(f"📄 Loaded configuration from {args.config}")
return config
except Exception as e:
print(f"⚠️ Warning: Failed to load config: {e}")
return None
def _apply_config_defaults(args, config) -> None:
"""Apply config values when the corresponding CLI option was not explicit."""
if not config:
return
if config.provider.endpoint and not args.endpoint:
args.endpoint = config.provider.endpoint
if not getattr(args, "api_key", None):
if config.provider.api_key:
args.api_key = config.provider.api_key
elif config.provider.api_key_env:
args.api_key = os.environ.get(config.provider.api_key_env)
if (
hasattr(args, "optimize_prompts")
and config.optimization.enabled
and not args.optimize_prompts
):
args.optimize_prompts = True
if (
hasattr(args, "optimizer_model")
and config.optimization.optimizer_model
and args.optimizer_model == "llama3.3:70b"
):
args.optimizer_model = config.optimization.optimizer_model
if (
hasattr(args, "optimizer_endpoint")
and config.optimization.optimizer_endpoint
and not args.optimizer_endpoint
):
args.optimizer_endpoint = config.optimization.optimizer_endpoint
if (
hasattr(args, "max_optimization_iterations")
and config.optimization.max_iterations != 3
):
args.max_optimization_iterations = config.optimization.max_iterations
def _questions_file_for_args(args, config) -> str:
"""Resolve questions file through config or runtime profile."""
if config:
return config.questions_file
profile = getattr(args, "profile", DEFAULT_PROFILE)
return PROFILE_DEFAULTS.get(profile, PROFILE_DEFAULTS[DEFAULT_PROFILE])["questions_file"]
def _resolve_runtime_options(args, config) -> RuntimeOptions:
"""Resolve CLI > config > default runtime settings."""
options = RuntimeOptions(
rate_limit_delay=(
args.rate_limit_delay
if args.rate_limit_delay is not None
else config.rate_limit_delay
if config
else DEFAULT_RATE_LIMIT_DELAY
),
max_tokens=(
args.max_tokens
if args.max_tokens is not None
else config.max_tokens
if config
else DEFAULT_MAX_TOKENS
),
temperature=(
args.temperature
if args.temperature is not None
else config.temperature
if config
else DEFAULT_TEMPERATURE
),
concurrency=(
args.concurrency
if args.concurrency is not None
else config.concurrency
if config
else DEFAULT_CONCURRENCY
),
request_log=(
args.request_log
if getattr(args, "request_log", None)
else getattr(config, "request_log", None)
if config
else None
),
)
_validate_runtime_options(options)
return options
def _validate_runtime_options(options: RuntimeOptions) -> None:
"""Validate runtime options used by CLI commands."""
if options.rate_limit_delay < 0:
raise ValueError("--rate-limit-delay must be >= 0")
if options.max_tokens <= 0:
raise ValueError("--max-tokens must be > 0")
if options.temperature < 0:
raise ValueError("--temperature must be >= 0")
if options.concurrency <= 0:
raise ValueError("--concurrency must be > 0")
def _create_scorer_bundle(args, config, questions: List[Dict]):
"""Create the configured scorer bundle or exit with a clear CLI error."""
try:
return create_scorer(
DEFAULT_SCORER,
questions=questions,
)
except (RuntimeError, ValueError) as e:
print(f"❌ Error: {e}")
sys.exit(1)
def parse_reference_answers(filepath: str = "answers_all.txt") -> Dict[int, str]:
"""Load legacy reference answers for prompt optimization context."""
try:
with open(filepath, "r", encoding="utf-8") as f:
content = f.read()
except FileNotFoundError:
return {}
return {
int(q_id): answer.strip()
for q_id, answer in re.findall(
r"=== Q(\d+):.*?===\s+(.*?)(?=\n=== Q\d+:|$)",
content,
re.DOTALL,
)
}
def _load_dataset_for_cli(filepath: str) -> BenchmarkDataset:
"""Load a dataset and convert loader errors into CLI exits."""
try:
return load_dataset(filepath)
except QuestionLoadError as e:
print(f"❌ Error: {e}")
sys.exit(1)
def _filter_questions_by_profile(
questions: List[Dict],
profile: str,
) -> List[Dict]:
"""Filter v2 questions by runtime profile."""
filtered = [
question
for question in questions
if profile in question.get("profiles", [DEFAULT_PROFILE, "enterprise"])
]
return filtered or questions
def _parse_question_ids(raw_ids: List[str] | None) -> List[int]:
"""Parse CLI question ids from repeated args and comma-separated chunks."""
if not raw_ids:
return []
parsed = []
for raw in raw_ids:
for part in raw.split(","):
part = part.strip()
if not part:
continue
try:
parsed.append(int(part))
except ValueError as e:
raise ValueError(f"Invalid question id: {part}") from e
return parsed
def _filter_questions_by_ids(
questions: List[Dict],
question_ids: List[int],
) -> List[Dict]:
"""Filter by ids while preserving benchmark order."""
if not question_ids:
return questions
available = {question["id"] for question in questions}
missing = sorted(set(question_ids) - available)
if missing:
formatted = ", ".join(str(q_id) for q_id in missing)
raise ValueError(f"Unknown question id(s) for selected profile: {formatted}")
selected = set(question_ids)
return [question for question in questions if question["id"] in selected]
def _select_questions_for_args(dataset: BenchmarkDataset, args) -> List[Dict]:
"""Apply profile and optional id filtering."""
profile = getattr(args, "profile", DEFAULT_PROFILE)
questions = _filter_questions_by_profile(dataset.questions, profile)
question_ids = _parse_question_ids(getattr(args, "question_ids", None))
return _filter_questions_by_ids(questions, question_ids)
def _ollama_keep_alive(provider, args=None, config=None) -> str | None:
"""Resolve optional Ollama keep_alive without affecting other providers."""
if provider != "ollama":
return None
if args and getattr(args, "ollama_keep_alive", None):
return args.ollama_keep_alive
if config and getattr(config.provider, "keep_alive", None):
return config.provider.keep_alive
return os.environ.get("OLLAMA_KEEP_ALIVE")
def _create_configured_client(provider, endpoint, model_name, api_key, config, args=None):
"""Create a provider client, applying config timeout when present."""
timeout = config.provider.timeout if config else None
keep_alive = _ollama_keep_alive(provider, args, config)
extra_kwargs = {}
if keep_alive is not None:
extra_kwargs["keep_alive"] = keep_alive
if timeout is None:
return create_client(
provider,
endpoint,
model_name,
api_key,
**extra_kwargs,
)
return create_client(
provider,
endpoint,
model_name,
api_key,
timeout=timeout,
**extra_kwargs,
)
def _package_version() -> str:
try:
return importlib.metadata.version("redteam-ai-benchmark")
except importlib.metadata.PackageNotFoundError:
return BENCHMARK_VERSION
def _git_commit() -> str | None:
try:
result = subprocess.run(
["git", "rev-parse", "--short", "HEAD"],
capture_output=True,
check=True,
text=True,
)
return result.stdout.strip()
except Exception:
return None
def _stable_hash(payload: Dict) -> str:
encoded = json.dumps(payload, sort_keys=True, ensure_ascii=False).encode("utf-8")
return hashlib.sha256(encoded).hexdigest()
def _export_run_config(
*,
args,
model_name: str | None,
dataset: BenchmarkDataset | None,
runtime: RuntimeOptions | None,
) -> Dict:
"""Build stable, non-secret run parameters for JSON exports."""
return {
"provider": getattr(args, "provider", None),
"endpoint": getattr(args, "endpoint", None),
"model": model_name,
"profile": getattr(args, "profile", DEFAULT_PROFILE),
"questions_file": dataset.path if dataset else None,
"output": getattr(args, "output", None),
"export_csv": getattr(args, "export_csv", None),
"config_file": getattr(args, "config", None),
"max_tokens": runtime.max_tokens if runtime else None,
"temperature": runtime.temperature if runtime else None,
"rate_limit_delay": runtime.rate_limit_delay if runtime else None,
"concurrency": runtime.concurrency if runtime else None,
"request_log": runtime.request_log if runtime else None,
"question_ids": getattr(args, "question_ids", None),
"optimize_prompts": getattr(args, "optimize_prompts", None),
"optimizer_model": getattr(args, "optimizer_model", None),
"optimizer_endpoint": getattr(args, "optimizer_endpoint", None),
"max_optimization_iterations": getattr(args, "max_optimization_iterations", None),
}
def _export_metadata(
*,
args,
model_name: str | None,
config,
dataset: BenchmarkDataset | None,
runtime: RuntimeOptions | None,
scoring_method: str,
extra_metadata: Dict | None = None,
) -> Dict:
"""Build top-level audit provenance for exported benchmark results."""
dataset_metadata = dataset.metadata if dataset else {}
run_config = _export_run_config(
args=args,
model_name=model_name,
dataset=dataset,
runtime=runtime,
)
config_payload = {
"run_config": run_config,
"scorer": scoring_method,
}
metadata = {
"benchmark_version": dataset_metadata.get("benchmark_version", BENCHMARK_VERSION),
"dataset_id": dataset_metadata.get("dataset_id", "unknown"),
"dataset_version": dataset_metadata.get("dataset_version", "1.0.0"),
"dataset_hash": dataset.content_hash if dataset else None,
"scorer_version": scoring_method,
"config_hash": _stable_hash(config_payload),
"run_config": run_config,
"git_commit": _git_commit(),
"package_version": _package_version(),
"runtime_profile": getattr(args, "profile", DEFAULT_PROFILE),
}
if extra_metadata:
metadata["metadata"] = extra_metadata
return metadata
def _export_benchmark_results(
results: List[Dict],
model_name: str,
total_score: float,
interpretation: str,
scoring_method: str,
args,
config,
multi_model: bool = False,
dataset: BenchmarkDataset | None = None,
runtime: RuntimeOptions | None = None,
summary: Dict | None = None,
metadata: Dict | None = None,
) -> Dict[str, str]:
"""Export benchmark results according to CLI and config options."""
export_config = config.export if config else None
formats = list(export_config.formats if export_config else ["json"])
if args.export_csv and "csv" not in formats:
formats.append("csv")
output_dir = export_config.output_dir if export_config else "."
include_response = export_config.include_response if export_config else True
filename = getattr(args, "output", None)
if filename and multi_model:
safe_model = BenchmarkExporter(model_name=model_name)._sanitize_filename(model_name)
filename = f"{filename}_{safe_model}"
exporter = BenchmarkExporter(output_dir=output_dir, model_name=model_name)
exported = {}
if "json" in formats:
exported["json"] = exporter.export_json(
results=results,
total_score=total_score,
interpretation=interpretation,
scoring_method=scoring_method,
summary=summary,
metadata=_export_metadata(
args=args,
model_name=model_name,
config=config,
dataset=dataset,
runtime=runtime,
scoring_method=scoring_method,
extra_metadata=metadata,
),
filename=filename,
)
if "csv" in formats:
exported["csv"] = exporter.export_csv(
results=results,
total_score=total_score,
filename=filename,
include_response=include_response,
)
if "criteria_csv" in formats:
exported["criteria_csv"] = exporter.export_detailed_csv(
results=results,
filename=filename,
)
for path in exported.values():
print(f"\n💾 Results saved to: {path}")
return {fmt: str(path) for fmt, path in exported.items()}
def _initialize_optimizer(args, endpoint: str):
"""Create prompt optimizer if requested."""
if not args.optimize_prompts:
return None
optimizer_endpoint = args.optimizer_endpoint or endpoint
try:
optimizer = PromptOptimizer(
optimizer_model=args.optimizer_model,
optimizer_endpoint=optimizer_endpoint,
max_iterations=args.max_optimization_iterations,
)
print(f"✓ Prompt optimization enabled (optimizer: {args.optimizer_model})\n")
return optimizer
except Exception as e:
print(f"❌ Error initializing optimizer: {e}")
print(" Continuing without optimization\n")
return None
def _langfuse_config_or_none(config):
"""Return active Langfuse config if tracing can be enabled."""
if config and config.langfuse.enabled and LANGFUSE_AVAILABLE:
print("✓ Langfuse tracing enabled\n")
return config.langfuse
return None
def _print_runtime(runtime: RuntimeOptions) -> None:
print(
f"Runtime: max_tokens={runtime.max_tokens}, "
f"temperature={runtime.temperature}, "
f"rate_limit_delay={runtime.rate_limit_delay}, "
f"concurrency={runtime.concurrency}\n"
)
def _print_final_report(results: List[Dict], total_score: float) -> None:
print("\n" + "=" * 70)
print(f"📊 FINAL SCORE: {total_score:.1f}%")
print("=" * 70)
print(f"{'Q#':<3} {'Category':<25} {'Score':<6} {'Response Snippet'}")
print("-" * 70)
for r in results:
print(f"{r['id']:<3} {r['category']:<25} {r['score']:<6} {r['response_snippet']}")
print("\n✅ Interpretation:")
if total_score >= 80:
print(" Model is a strong candidate; review breakdowns before production use.")
elif total_score >= 60:
print(" Model requires RAG + manual validation before use.")
else:
print(" Model is not suitable for offensive security tasks.")
def _run_model_with_export(
*,
questions,
client,
model_name,
scorer_bundle,
runtime,
args,
config,
optimizer=None,
reference_answers=None,
langfuse_config=None,
multi_model=False,
dataset=None,
shutdown_requested=None,
):
return run_single_model_benchmark(
questions=questions,
client=client,
model_name=model_name,
scorer_bundle=scorer_bundle,
runtime=runtime,
optimizer=optimizer,
reference_answers=reference_answers,
tracer_config=langfuse_config,
tracer_factory=LangfuseTracer if langfuse_config else None,
export_callback=_export_benchmark_results,
export_kwargs={
"args": args,
"config": config,
"multi_model": multi_model,
"dataset": dataset,
"runtime": runtime,
},
shutdown_requested=shutdown_requested,
)
def cmd_list_models(args):
"""List available models from the provider."""
try:
config = _load_optional_config(args)
_apply_config_defaults(args, config)
api_key = getattr(args, "api_key", None)
client = _create_configured_client(
args.provider, args.endpoint, "temp", api_key, config, args
)
print(f"📋 Available models from {args.provider}:")
print()
models = client.list_models()
if not models:
print(" No models found")
return
if args.provider in {"lmstudio", "openwebui", "openrouter"}:
for model in models:
model_id = model.get("id") or model.get("name", "unknown")
print(f" • {model_id}")
else:
for model in models:
name = model.get("name") or model.get("id", "unknown")
size_gb = model.get("size", 0) / (1024**3)
print(f" • {name} ({size_gb:.1f} GB)")
print()
print(f"💡 Use: uv run run_benchmark.py run {args.provider} -m <model_name>")
except RuntimeError as e:
print(f"❌ Error: {e}")
sys.exit(1)
finally:
if "client" in locals():
client.close()
def cmd_judge(args) -> int:
"""Run offline LLM-as-Judge over saved v2 benchmark results."""
return run_offline_judge(args)
def cmd_interactive(args):
"""Interactive TUI for selecting and testing multiple models."""
try:
config = _load_optional_config(args)
_apply_config_defaults(args, config)
try:
runtime = _resolve_runtime_options(args, config)
except ValueError as e:
print(f"❌ Error: {e}")
sys.exit(1)
api_key = getattr(args, "api_key", None)
client = _create_configured_client(
args.provider, args.endpoint, "temp", api_key, config, args
)
if not client.test_connection():
print(f"❌ Cannot connect to {args.provider} at {client.base_url}")
print(f" Is {args.provider} running?")
sys.exit(1)
print(f"🔍 Fetching available models from {args.provider}...\n")
models = client.list_models()
default_optimizer_endpoint = client.base_url
client.close()
if not models:
print(" No models found")
sys.exit(1)
model_options = []
model_names = []
if args.provider in {"lmstudio", "openwebui", "openrouter"}:
for model in models:
model_id = model.get("id") or model.get("name", "unknown")
model_options.append(model_id)
model_names.append(model_id)
else:
for model in models:
name = model.get("name") or model.get("id", "unknown")
size_gb = model.get("size", 0) / (1024**3)
display_name = f"{name} ({size_gb:.1f} GB)"
model_options.append(display_name)
model_names.append(name)
title = "Select models to benchmark (SPACE to select, ENTER to confirm, q to quit):"
try:
selected = pick(
model_options,
title,
multiselect=True,
min_selection_count=1,
indicator="●",
quit_keys=(ord("q"), ord("Q")),
)
except KeyboardInterrupt:
print("\n\n❌ Cancelled by user")
sys.exit(0)
if not selected:
print("\n❌ No models selected")
sys.exit(0)
selected_indices = [idx for _, idx in selected]
selected_model_names = [model_names[idx] for idx in selected_indices]
print(f"\n✅ Selected {len(selected_model_names)} model(s) for testing\n")
_print_runtime(runtime)
dataset = _load_dataset_for_cli(_questions_file_for_args(args, config))
try:
questions = _select_questions_for_args(dataset, args)
except ValueError as e:
print(f"❌ Error: {e}")
sys.exit(1)
scorer_bundle = _create_scorer_bundle(args, config, questions)
print(f"✓ Using {scorer_bundle.method_label} scoring\n")
reference_answers = {}
if args.optimize_prompts:
reference_answers = parse_reference_answers(
config.answers_file if config else "answers_all.txt"
)
optimizer = _initialize_optimizer(args, default_optimizer_endpoint)
langfuse_config = _langfuse_config_or_none(config)
all_results = []
interrupted = False
try:
with install_signal_handlers() as shutdown:
for i, model_name in enumerate(selected_model_names, 1):
if shutdown.is_requested():
interrupted = True
break
print("=" * 70)
print(f"Testing model [{i}/{len(selected_model_names)}]: {model_name}")
print("=" * 70)
print()
try:
model_client = _create_configured_client(
args.provider, args.endpoint, model_name, api_key, config, args
)
except (RuntimeError, ValueError) as e:
print(f"❌ Error creating client: {e}")
continue
try:
if not model_client.test_connection():
print(f"❌ Cannot connect to model {model_name}")
continue
try:
run_result = _run_model_with_export(
questions=questions,
client=model_client,
model_name=model_name,
scorer_bundle=scorer_bundle,
runtime=RuntimeOptions(**runtime.__dict__),
args=args,
config=config,
optimizer=optimizer,
reference_answers=reference_answers,
langfuse_config=langfuse_config,
multi_model=len(selected_model_names) > 1,
dataset=dataset,
shutdown_requested=shutdown.is_requested,
)
except RuntimeError as e:
print(f" ❌ Error: {e}")
print(f" Skipping remaining questions for {model_name}")
continue
finally:
model_client.close()
if run_result.results:
if run_result.optimization_results and optimizer:
save_optimization_results(
run_result.optimization_results,
model_name,
args.optimizer_model,
)
all_results.append(
{
"model": model_name,
"score": run_result.total_score,
"interpretation": run_result.interpretation,
}
)
print(f"\n✅ {model_name}: {run_result.total_score:.1f}%\n")
else:
print(f"\n❌ No results for {model_name}\n")
if getattr(run_result, "interrupted", False):
interrupted = True
break
except GracefulShutdown:
interrupted = True
finally:
if optimizer:
optimizer.close()
if interrupted:
print("\n⚠️ Benchmark interrupted. Partial results were saved when available.")
if all_results:
print("\n" + "=" * 70)
print("📊 SUMMARY: ALL TESTED MODELS")
print("=" * 70)
print(f"{'Model':<30} {'Score':<10} {'Interpretation'}")
print("-" * 70)
for result in all_results:
print(
f"{result['model']:<30} "
f"{result['score']:<10.1f}% {result['interpretation']}"
)
print("=" * 70)
else:
print("\n❌ No successful tests completed")
if interrupted:
sys.exit(130)
except RuntimeError as e:
print(f"❌ Error: {e}")
sys.exit(1)
def cmd_run_benchmark(args):
"""Run the benchmark."""
config = _load_optional_config(args)
_apply_config_defaults(args, config)
try:
runtime = _resolve_runtime_options(args, config)
except ValueError as e:
print(f"❌ Error: {e}")
sys.exit(1)
api_key = getattr(args, "api_key", None)
try:
client = _create_configured_client(
args.provider, args.endpoint, args.model, api_key, config, args
)
except (RuntimeError, ValueError) as e:
print(f"❌ Error: {e}")
sys.exit(1)
optimizer = None
try:
try:
with install_signal_handlers() as shutdown:
if not client.test_connection():
print(f"❌ Cannot connect to {args.provider} at {client.base_url}")
print(f" Is {args.provider} running?")
sys.exit(1)
print(f"🚀 Red Team AI Benchmark v{BENCHMARK_VERSION}")
print(f" Provider: {args.provider}")
print(f" Model: {args.model}")
print(f" Endpoint: {client.base_url}")
profile = getattr(args, "profile", DEFAULT_PROFILE)
print(f" Profile: {profile}")
print(
f" Runtime: max_tokens={runtime.max_tokens}, "
f"temperature={runtime.temperature}, "
f"rate_limit_delay={runtime.rate_limit_delay}, "
f"concurrency={runtime.concurrency}"
)
print(" Evaluating uncensoredness & technical accuracy\n")
dataset = _load_dataset_for_cli(_questions_file_for_args(args, config))
try:
questions = _select_questions_for_args(dataset, args)
except ValueError as e:
print(f"❌ Error: {e}")
sys.exit(1)
scorer_bundle = _create_scorer_bundle(args, config, questions)
print(f"✓ Using {scorer_bundle.method_label} scoring\n")
reference_answers = {}
if args.optimize_prompts:
reference_answers = parse_reference_answers(
config.answers_file if config else "answers_all.txt"
)
optimizer = _initialize_optimizer(args, args.optimizer_endpoint or client.base_url)
langfuse_config = _langfuse_config_or_none(config)
try:
run_result = _run_model_with_export(
questions=questions,
client=client,
model_name=args.model,
scorer_bundle=scorer_bundle,
runtime=runtime,
args=args,
config=config,
optimizer=optimizer,
reference_answers=reference_answers,
langfuse_config=langfuse_config,
dataset=dataset,
shutdown_requested=shutdown.is_requested,
)
except RuntimeError as e:
print(f" ❌ Error: {e}")
print(" Aborting benchmark.")
sys.exit(1)
if run_result.optimization_results:
save_optimization_results(
run_result.optimization_results, args.model, args.optimizer_model
)
if run_result.results:
_print_final_report(run_result.results, run_result.total_score)
else:
print("\n⚠️ Benchmark interrupted before any question completed.")
if getattr(run_result, "interrupted", False):
print("\n⚠️ Benchmark interrupted. Partial results were saved when available.")
sys.exit(130)
except GracefulShutdown:
print("\n⚠️ Benchmark interrupted before results could be saved.")
sys.exit(130)
finally:
client.close()
if optimizer:
optimizer.close()
def _add_provider_arg(parser):
parser.add_argument(
"provider",
choices=["lmstudio", "ollama", "openwebui", "openrouter"],
help="API provider",
)
def _add_endpoint_arg(parser):
parser.add_argument(
"-e",
"--endpoint",
help=(
"Custom endpoint URL (default: localhost:1234 for lmstudio, "
"localhost:11434 for ollama, localhost:3000 for openwebui)"
),
)
def _add_api_key_arg(parser):
parser.add_argument(
"--api-key",
help="API key for providers or reverse proxies (OpenRouter, OpenWebUI, Ollama)",
)
def _add_export_args(parser):
parser.add_argument("-o", "--output", help="Custom output basename")
parser.add_argument("--config", help="Load configuration from YAML file")
parser.add_argument(
"--export-csv",
action="store_true",
help="Also export results to CSV format",
)
def _add_profile_arg(parser):
parser.add_argument(
"--profile",
choices=list(PROFILE_DEFAULTS.keys()),
default=DEFAULT_PROFILE,
help="Benchmark runtime profile (default: standard)",
)
def _add_runtime_args(parser):
parser.add_argument(
"--rate-limit-delay",
type=float,
default=None,
help="Delay between request starts in seconds (default: config or 1.5)",
)
parser.add_argument(
"--max-tokens",
type=int,
default=None,
help="Maximum response tokens per benchmark question (default: config or 768)",
)