-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ace_comprehensive.py
More file actions
770 lines (629 loc) · 28.4 KB
/
test_ace_comprehensive.py
File metadata and controls
770 lines (629 loc) · 28.4 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
"""
Comprehensive Testing Suite for ACE System
Tests 3 modes:
1. Vanilla - No improvements
2. Offline + Online - Pre-trained with offline bullets
3. Online Only - Real-time learning only
Uses GPT-4o mini for simple prompts and LLM judge for evaluation.
"""
import os
import json
import logging
from typing import List, Dict, Any, Tuple
from dataclasses import dataclass
from datetime import datetime
import random
from openai import OpenAI
from ace.llm_judge import LLMJudge
from test_judges import create_all_judges, get_judge_for_mode
from database import SessionLocal
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
@dataclass
class TestResult:
"""Results for a single test."""
mode: str
total: int
correct: int
accuracy: float
details: List[Dict[str, Any]]
class SimpleJudge:
"""Simple LLM judge using GPT-4o mini."""
def __init__(self, api_key: str = None):
self.client = OpenAI(api_key=api_key or os.getenv("OPENAI_API_KEY"))
self.model = "gpt-4o-mini" # Keep 4o-mini for quality judgment
def judge(self, input_text: str, output: str, ground_truth: str) -> Tuple[bool, float, str]:
"""Judge if output matches ground truth."""
try:
response = self.client.chat.completions.create(
model=self.model,
messages=[
{
"role": "system",
"content": "You are a judge evaluating fraud detection outputs. Answer in JSON format."
},
{
"role": "user",
"content": f"""Input: {input_text}
Predicted Output: {output}
Ground Truth: {ground_truth}
Is the predicted output correct? Respond in JSON:
{{
"is_correct": true/false,
"confidence": 0.0-1.0,
"reason": "brief explanation of why it is correct or incorrect"
}}"""
}
],
response_format={"type": "json_object"},
temperature=0.0
)
result = json.loads(response.choices[0].message.content)
return result.get("is_correct", False), result.get("confidence", 0.5), result.get("reason", "")
except Exception as e:
logger.error(f"Judge error: {e}")
return False, 0.0, "Error in judgment"
class SimpleFraudAgent:
"""Simple fraud detection agent using GPT-3.5-turbo for speed."""
def __init__(self, api_key: str = None, bullets: List[str] = None, max_bullets: int = 5):
self.client = OpenAI(api_key=api_key or os.getenv("OPENAI_API_KEY"))
self.model = "gpt-3.5-turbo" # Use 3.5-turbo for fast agent responses
self.bullets = bullets or []
self.bullets_context = "" # Context with bullets organized by evaluator
self.max_bullets = max_bullets # Limit bullets to prevent context rot
def analyze(self, transaction: str) -> Dict[str, Any]:
"""Analyze transaction and return decision."""
try:
# Use bullets_context if provided (organized by evaluator), otherwise use simple bullets
bullets_context = self.bullets_context
if not bullets_context and self.bullets:
# Fallback to simple bullet list
limited_bullets = self.bullets[:self.max_bullets]
bullets_context = "\n\nGuidelines:\n" + "\n".join(f"- {b}" for b in limited_bullets)
response = self.client.chat.completions.create(
model=self.model,
messages=[
{
"role": "system",
"content": """You are a fraud detection expert. Analyze transactions and return structured output.
Your response must be JSON with:
{
"decision": "APPROVE" or "DECLINE",
"reasoning": "brief explanation of your decision"
}"""
},
{
"role": "user",
"content": f"""Transaction: {transaction}{bullets_context}
Analyze this transaction and determine if it should be APPROVED or DECLINED.
Return JSON:
{{
"decision": "APPROVE" or "DECLINE",
"reasoning": "brief explanation"
}}"""
}
],
response_format={"type": "json_object"},
temperature=0.3
)
result = json.loads(response.choices[0].message.content)
return {
"decision": result.get("decision", "DECLINE"),
"reasoning": result.get("reasoning", "")
}
except Exception as e:
logger.error(f"Agent error: {e}")
return {"decision": "DECLINE", "reasoning": f"Error: {e}"}
def load_dataset(file_path: str) -> List[Dict[str, Any]]:
"""Load dataset from JSON file."""
with open(file_path, 'r') as f:
return json.load(f)
def split_dataset(dataset: List[Dict[str, Any]], train_ratio: float = 0.7) -> Tuple[List, List]:
"""Split dataset into train and test sets."""
random.shuffle(dataset)
split_idx = int(len(dataset) * train_ratio)
return dataset[:split_idx], dataset[split_idx:]
def run_vanilla_mode(test_set: List[Dict[str, Any]], judge) -> TestResult:
"""Test vanilla mode - no improvements."""
logger.info("=" * 60)
logger.info("Testing VANILLA mode")
logger.info("=" * 60)
from database import SessionLocal
from models import Transaction
db_session = SessionLocal()
agent = SimpleFraudAgent()
results = []
correct = 0
for idx, item in enumerate(test_set):
logger.info(f"\n[{idx+1}/{len(test_set)}] Testing vanilla mode...")
result = agent.analyze(item['query'])
predicted = result['decision']
ground_truth = item['answer']
# Compare with ground truth directly (no LLM judge for offline evaluation)
is_correct = predicted == ground_truth
confidence = 1.0 if is_correct else 0.0
reason = "Matches ground truth" if is_correct else "Does not match ground truth"
if is_correct:
correct += 1
# Save transaction to database
try:
transaction_data = {
"systemprompt": "You are a fraud detection expert analyzing transactions.",
"userprompt": item['query'],
"output": predicted,
"reasoning": result.get('reasoning', '')
}
txn = Transaction(
transaction_data=transaction_data,
mode="vanilla",
node="fraud_detection",
predicted_decision=predicted,
correct_decision=ground_truth,
is_correct=is_correct
)
db_session.add(txn)
db_session.commit()
db_session.flush()
except Exception as e:
logger.error(f"Error saving transaction: {e}")
results.append({
"query": item['query'][:100] + "...",
"predicted": predicted,
"ground_truth": ground_truth,
"correct": is_correct,
"confidence": confidence,
"reason": reason
})
logger.info(f"Query: {item['query'][:80]}...")
logger.info(f"Predicted: {predicted} | Ground Truth: {ground_truth} | Correct: {is_correct}")
accuracy = correct / len(test_set) if test_set else 0.0
logger.info(f"\nVanilla Mode Accuracy: {accuracy:.2%}")
return TestResult(
mode="vanilla",
total=len(test_set),
correct=correct,
accuracy=accuracy,
details=results
)
async def run_offline_online_mode(
train_set: List[Dict[str, Any]],
test_set: List[Dict[str, Any]],
judge: SimpleJudge
) -> TestResult:
"""Test offline + online mode with LLM Judge."""
logger.info("=" * 60)
logger.info("Testing OFFLINE + ONLINE mode with LLM Judge")
logger.info("=" * 60)
# Step 1: Offline training
logger.info("\n--- Step 1: Offline Training (with LLM Judge) ---")
from ace.training_pipeline import TrainingPipeline
from ace.bullet_playbook import BulletPlaybook
from ace.hybrid_selector import HybridSelector
from ace.reflector import Reflector
from ace.curator import Curator
from ace.pattern_manager import PatternManager
from database import SessionLocal
# Create database session
db_session = SessionLocal()
# Initialize PatternManager first
pattern_manager = PatternManager(db_session=db_session)
# Create SimpleFraudAgent for offline training
agent = SimpleFraudAgent(max_bullets=5)
selector = HybridSelector(db_session=db_session, pattern_manager=pattern_manager)
reflector = Reflector()
curator = Curator()
# Initialize Darwin evolver for integrated evolution
from ace.darwin_bullet_evolver import DarwinBulletEvolver
darwin_evolver = DarwinBulletEvolver(db_session=db_session)
training_pipeline = TrainingPipeline(agent, selector, reflector, curator, darwin_evolver=darwin_evolver)
playbook = BulletPlaybook(db_session=db_session)
# Select diverse, harder training inputs (max 10)
# Strategy: Sample from different parts of the dataset for variety
max_training = min(10, len(train_set))
# Get indices for diverse sampling
import random
random.seed(42) # For reproducibility
# Sample indices from different parts of the dataset
indices = []
if len(train_set) >= max_training:
# Take samples from beginning, middle, and end for variety
step = len(train_set) // max_training
indices = [i * step + (i % 2) * (step // 2) for i in range(max_training)]
indices = [min(i, len(train_set) - 1) for i in indices] # Ensure valid indices
else:
indices = list(range(len(train_set)))
# Select diverse training inputs
training_inputs = [train_set[i] for i in indices]
logger.info(f"Selected {len(training_inputs)} diverse inputs for training")
logger.info(f"Sampled from indices: {indices}")
# Use the same set for testing
offline_train_set = training_inputs
offline_test_set = training_inputs
logger.info(f"Training set: {len(offline_train_set)} examples")
logger.info(f"Test set: {len(offline_test_set)} examples (same as training)")
# Generate bullets from offline training
logger.info(f"Training on {len(offline_train_set)} examples...")
offline_bullets = []
for idx, item in enumerate(offline_train_set):
logger.info(f" [{idx+1}/{len(offline_train_set)}] Processing...")
# Get pattern classification
pattern_id, confidence = pattern_manager.classify_input_to_category(
input_summary=item['query'],
node="fraud_detection"
)
# Get existing bullets
existing_bullets = playbook.get_bullets_for_node("fraud_detection")
# Check if we have relevant bullets for this input
has_relevant_bullets = selector.has_relevant_bullets(
query=item['query'],
bullets=existing_bullets,
similarity_threshold=0.7
)
# Select bullets for agent
bullets_used = 0
if existing_bullets:
selected_bullets, _ = selector.select_bullets(
query=item['query'],
node="fraud_detection",
playbook=playbook,
n_bullets=5, # Request up to 5 bullets
iteration=idx,
pattern_id=pattern_id
)
agent.bullets = [b.content for b in selected_bullets]
bullets_used = len(selected_bullets)
else:
agent.bullets = []
bullets_used = 0
# STEP 1: Execute agent on this input
result = agent.analyze(item['query'])
predicted = result['decision']
ground_truth = item['answer']
# STEP 2: Use LLM Judge to evaluate (like online mode)
is_correct, confidence, reason = judge.judge(item['query'], predicted, ground_truth)
logger.info(f" Predicted: {predicted} | Ground Truth: {ground_truth} | Judge: {is_correct}")
# STEP 3: Generate bullet only if:
# 1. Output was incorrect (wrong prediction), OR
# 2. Bullets used was less than 5 (coverage issue)
should_generate = False
if not is_correct:
# Wrong prediction - always generate bullet
should_generate = True
logger.info(f" Judge says incorrect - Generating bullet")
elif bullets_used < 5:
# Correct but not enough bullets used - generate bullet for coverage
should_generate = True
logger.info(f" Correct prediction but only {bullets_used} bullets used (< 5) - Generating bullet")
if should_generate:
# Generate bullet with evaluator context
evaluator = "fraud_detection" # Default evaluator
bullet_id = await training_pipeline.add_bullet_from_reflection(
query=item['query'],
predicted=predicted,
correct=ground_truth,
node="fraud_detection",
agent_reasoning=result.get('reasoning', ''),
playbook=playbook,
source="offline",
evaluator=evaluator,
judge_reasoning=reason # Pass judge reasoning to bullet generation
)
if bullet_id:
offline_bullets.append(bullet_id)
logger.info(f" Generated bullet: {bullet_id}")
# Log training progress every 10 items
if (idx + 1) % 10 == 0:
total_bullets = len(playbook.get_bullets_for_node("fraud_detection"))
logger.info(f" Training progress: {idx+1}/{len(offline_train_set)} | Bullets created: {total_bullets}")
# Get bullets from playbook
bullets = playbook.get_bullets_for_node("fraud_detection")
bullet_texts = [b.content for b in bullets]
logger.info(f"\n✓ Offline training complete!")
logger.info(f" Total bullets generated: {len(bullet_texts)}")
logger.info(f" Unique bullets: {len(set(bullet_texts))}")
logger.info(f" Deduplication rate: {(1 - len(set(bullet_texts)) / len(bullet_texts)) * 100:.1f}%")
# Note: Darwin-Gödel evolution is now integrated into bullet generation via TrainingPipeline
# Step 2: Test with offline bullets
logger.info("\n--- Step 2: Testing with Offline Bullets (Online Learning with LLM Judge) ---")
# Use HybridSelector to intelligently select bullets for each transaction
selector = HybridSelector()
max_bullets = 5 # Hyperparameter: limit bullets to prevent context rot
agent = SimpleFraudAgent(max_bullets=max_bullets)
results = []
correct = 0
for idx, item in enumerate(offline_test_set):
logger.info(f"\n[{idx+1}/{len(offline_test_set)}] Testing offline+online mode...")
# Get pattern classification for this input
pattern_id, confidence = pattern_manager.classify_input_to_category(
input_summary=item['query'],
node="fraud_detection"
)
# Get all evaluators from database
from models import Bullet as BulletModel
all_evaluators = db_session.query(BulletModel.evaluator).filter(
BulletModel.node == "fraud_detection",
BulletModel.evaluator.isnot(None)
).distinct().all()
evaluator_names = [e[0] for e in all_evaluators]
# Get bullets for each evaluator and build prompt
bullets_context = ""
all_selected_bullets = []
all_selected_bullet_objects = [] # Track bullet objects for effectiveness recording
all_scores = [] # Track scores from all evaluators
for evaluator_name in evaluator_names:
# Get bullets for this evaluator
evaluator_bullets = playbook.get_bullets_for_node("fraud_detection", evaluator=evaluator_name)
# Select top bullets for this evaluator using intelligent selection
if evaluator_bullets:
# Temporarily create a filtered playbook for this evaluator
temp_bullets = [b for b in evaluator_bullets]
temp_playbook = BulletPlaybook()
temp_playbook.bullets = temp_bullets
temp_playbook._node_index["fraud_detection"] = temp_bullets
selected, scores = selector.select_bullets(
query=item['query'],
node="fraud_detection",
playbook=temp_playbook,
n_bullets=min(10, len(temp_bullets)), # Max 10 per evaluator
iteration=idx,
pattern_id=pattern_id
)
# Add to context
if selected:
bullets_context += f"\n\n{evaluator_name.upper()} Rules:\n"
for bullet in selected[:10]: # Limit to 10 per evaluator
bullets_context += f"- {bullet.content}\n"
all_selected_bullets.append(bullet.content)
all_selected_bullet_objects.append(bullet)
# Add scores from this evaluator
if scores:
all_scores.extend(scores)
agent.bullets = all_selected_bullets
agent.bullets_context = bullets_context
# Log metrics
logger.info(f" Pattern ID: {pattern_id} (confidence: {confidence:.2f})")
logger.info(f" Evaluators: {evaluator_names}")
logger.info(f" Selected {len(all_selected_bullets)} bullets total from {len(playbook.get_bullets_for_node('fraud_detection'))} total")
result = agent.analyze(item['query'])
predicted = result['decision']
ground_truth = item['answer']
# Compare with ground truth using LLM Judge (like offline training)
is_correct, confidence, reason = judge.judge(item['query'], predicted, ground_truth)
logger.info(f" Predicted: {predicted} | Ground Truth: {ground_truth} | Judge: {is_correct}")
if is_correct:
correct += 1
# Online learning: Generate bullet if incorrect or low coverage
if not is_correct or len(all_selected_bullets) < 5:
# Get evaluator for bullet generation
evaluator = evaluator_names[0] if evaluator_names else "fraud_detection"
bullet_id = await training_pipeline.add_bullet_from_reflection(
query=item['query'],
predicted=predicted,
correct=ground_truth,
node="fraud_detection",
agent_reasoning=result.get('reasoning', ''),
playbook=playbook,
source="online",
evaluator=evaluator,
judge_reasoning=reason # Pass judge reasoning to bullet generation
)
if bullet_id:
logger.info(f" ✓ Generated online bullet: {bullet_id}")
# Save transaction to database
try:
from models import Transaction
# Construct transaction data with system and user prompts
transaction_data = {
"systemprompt": f"You are a fraud detection expert analyzing transactions.{bullets_context}",
"userprompt": item['query'],
"output": predicted,
"reasoning": result.get('reasoning', '')
}
txn = Transaction(
transaction_data=transaction_data,
mode="offline_online",
node="fraud_detection",
predicted_decision=predicted,
correct_decision=ground_truth,
is_correct=is_correct,
input_pattern_id=pattern_id
)
db_session.add(txn)
db_session.commit()
db_session.flush()
except Exception as e:
logger.error(f"Error saving transaction: {e}")
# Record bullet effectiveness for this pattern
if pattern_id:
for bullet in all_selected_bullet_objects:
pattern_manager.record_bullet_effectiveness(
pattern_id=pattern_id,
bullet_id=bullet.id,
node="fraud_detection",
is_helpful=is_correct
)
# Log progress metrics
if (idx + 1) % 5 == 0:
current_accuracy = correct / (idx + 1)
logger.info(f" Progress: {idx+1}/{len(test_set)} | Current Accuracy: {current_accuracy:.2%}")
results.append({
"query": item['query'][:100] + "...",
"predicted": predicted,
"ground_truth": ground_truth,
"correct": is_correct,
"confidence": confidence,
"reason": reason,
"bullets_used": len(all_selected_bullets),
"scores": all_scores[:3] if all_scores else []
})
logger.info(f"Query: {item['query'][:80]}...")
logger.info(f"Predicted: {predicted} | Ground Truth: {ground_truth} | Correct: {is_correct}")
accuracy = correct / len(offline_test_set) if offline_test_set else 0.0
logger.info(f"\nOffline + Online Mode Accuracy: {accuracy:.2%}")
return TestResult(
mode="offline_online",
total=len(offline_test_set),
correct=correct,
accuracy=accuracy,
details=results
)
async def run_online_only_mode(test_set: List[Dict[str, Any]], judge: SimpleJudge) -> TestResult:
"""Test online only mode."""
logger.info("=" * 60)
logger.info("Testing ONLINE ONLY mode")
logger.info("=" * 60)
from ace.training_pipeline import TrainingPipeline
from ace.bullet_playbook import BulletPlaybook
from ace.hybrid_selector import HybridSelector
from ace.reflector import Reflector
from ace.curator import Curator
# Mock agent
class MockAgent:
async def analyze(self, transaction):
return {"decision": "DECLINE", "reasoning": "Default"}
mock_agent = MockAgent()
selector = HybridSelector()
reflector = Reflector()
curator = Curator()
training_pipeline = TrainingPipeline(mock_agent, selector, reflector, curator)
playbook = BulletPlaybook()
agent = SimpleFraudAgent()
results = []
correct = 0
for idx, item in enumerate(test_set):
logger.info(f"\n[{idx+1}/{len(test_set)}] Testing online only mode...")
# Analyze
result = agent.analyze(item['query'])
predicted = result['decision']
ground_truth = item['answer']
# Judge (SimpleJudge is synchronous)
is_correct, confidence, reason = judge.judge(item['query'], predicted, ground_truth)
if is_correct:
correct += 1
# Online learning with judge reasoning
bullet_id = await training_pipeline.add_bullet_from_reflection(
query=item['query'],
predicted=predicted,
correct=ground_truth,
node="fraud_detection",
agent_reasoning=predicted,
playbook=playbook,
source="online",
judge_reasoning=reason # Pass judge reasoning to bullet generation
)
results.append({
"query": item['query'][:100] + "...",
"predicted": predicted,
"ground_truth": ground_truth,
"correct": is_correct,
"confidence": confidence,
"reason": reason,
"bullet_added": bullet_id is not None
})
logger.info(f"Query: {item['query'][:80]}...")
logger.info(f"Predicted: {predicted} | Ground Truth: {ground_truth} | Correct: {is_correct}")
accuracy = correct / len(test_set) if test_set else 0.0
logger.info(f"\nOnline Only Mode Accuracy: {accuracy:.2%}")
return TestResult(
mode="online_only",
total=len(test_set),
correct=correct,
accuracy=accuracy,
details=results
)
def main():
"""Main test runner."""
logger.info("Starting Comprehensive ACE Test Suite")
logger.info("=" * 60)
# Load datasets
logger.info("\nLoading datasets...")
complex_dataset = load_dataset("agents/complex_fraud_detection.json")
ultra_hard_dataset = load_dataset("agents/ultra_hard_subset.json") # Use subset for faster testing
logger.info(f"Complex dataset: {len(complex_dataset)} examples")
logger.info(f"Ultra hard dataset (subset): {len(ultra_hard_dataset)} examples")
# Combine datasets
full_dataset = complex_dataset + ultra_hard_dataset
logger.info(f"Combined dataset: {len(full_dataset)} examples")
# Split into train/test
train_set, test_set = split_dataset(full_dataset, train_ratio=0.7)
logger.info(f"Train set: {len(train_set)} examples")
logger.info(f"Test set: {len(test_set)} examples")
# Limit test set to 20 items for faster testing
test_set = test_set[:20]
logger.info(f"Limited test set to {len(test_set)} examples for faster testing")
# Initialize LLM judges with different perspectives
db_session = SessionLocal()
try:
judge_configs = create_all_judges(db_session)
logger.info(f"Created {len(judge_configs)} LLM judges: {list(judge_configs.keys())}")
# Create SimpleJudge instances for each judge
judge_instances = {
'risk_focused': SimpleJudge(),
'pattern_focused': SimpleJudge(),
'context_focused': SimpleJudge()
}
except Exception as e:
logger.warning(f"Could not create judges in DB: {e}")
# Fallback to SimpleJudge
judge_instances = {
'risk_focused': SimpleJudge(),
'pattern_focused': SimpleJudge(),
'context_focused': SimpleJudge()
}
# Run tests with appropriate judges
results = []
# 1. Vanilla mode - Pattern-focused judge (not used, just ground truth)
logger.info("\n" + "="*60)
logger.info("VANILLA MODE - Ground Truth Comparison")
logger.info("="*60)
vanilla_judge = judge_instances['pattern_focused'] # Not used, just for compatibility
vanilla_result = run_vanilla_mode(test_set, vanilla_judge)
results.append(vanilla_result)
# 2. Offline + Online mode - Risk-focused judge (USES LLM JUDGE!)
logger.info("\n" + "="*60)
logger.info("OFFLINE + ONLINE MODE - Using LLM Judge")
logger.info("="*60)
offline_online_judge = judge_instances['risk_focused'] # USES LLM JUDGE
import asyncio
offline_online_result = asyncio.run(run_offline_online_mode(train_set, test_set, offline_online_judge))
results.append(offline_online_result)
# Skip online-only mode for now
# # 3. Online only mode - Context-focused judge (USES LLM JUDGE!)
# logger.info("\n" + "="*60)
# logger.info("Online Only Mode - Using CONTEXT-FOCUSED LLM Judge")
# logger.info("="*60)
# online_only_judge = judge_instances['context_focused']
# online_only_result = asyncio.run(run_online_only_mode(test_set, online_only_judge))
# results.append(online_only_result)
# Summary
logger.info("\n" + "=" * 60)
logger.info("TEST SUMMARY")
logger.info("=" * 60)
for result in results:
logger.info(f"\n{result.mode.upper()}:")
logger.info(f" Total: {result.total}")
logger.info(f" Correct: {result.correct}")
logger.info(f" Accuracy: {result.accuracy:.2%}")
# Save results
output_file = f"test_results_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
with open(output_file, 'w') as f:
json.dump({
"summary": {
r.mode: {
"total": r.total,
"correct": r.correct,
"accuracy": r.accuracy
}
for r in results
},
"details": {
r.mode: r.details
for r in results
}
}, f, indent=2)
logger.info(f"\nResults saved to: {output_file}")
if __name__ == "__main__":
main()