-
Notifications
You must be signed in to change notification settings - Fork 947
Expand file tree
/
Copy pathevaluate_prompts.py
More file actions
executable file
·474 lines (392 loc) · 15.8 KB
/
evaluate_prompts.py
File metadata and controls
executable file
·474 lines (392 loc) · 15.8 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
#!/usr/bin/env python3
"""
Unified evaluation script for GEPA benchmark datasets.
Can evaluate baseline or evolved prompts on IFEval, HoVer, and HotpotQA.
"""
import os
import json
import yaml
import time
import argparse
from datetime import datetime
from datasets import load_dataset
from openai import OpenAI
from tqdm import tqdm
# Initialize OpenAI client
def get_client():
api_key = os.environ.get("OPENAI_API_KEY")
if not api_key:
raise ValueError("OPENAI_API_KEY environment variable not set")
return OpenAI(base_url="https://openrouter.ai/api/v1", api_key=api_key)
def load_prompt(dataset_name, prompt_type="baseline"):
"""Load prompt template for a dataset."""
if prompt_type == "baseline":
prompt_path = f"{dataset_name}_prompt.txt"
else: # evolved
prompt_path = f"openevolve_output_qwen3_{dataset_name}/best/best_program.txt"
if not os.path.exists(prompt_path):
raise FileNotFoundError(f"Prompt file not found: {prompt_path}")
with open(prompt_path, "r") as f:
return f.read().strip()
def load_dataset_config(dataset_name):
"""Load dataset configuration."""
config_path = f"{dataset_name}_prompt_dataset.yaml"
with open(config_path, "r") as f:
return yaml.safe_load(f)
def evaluate_ifeval(client, prompt_template, num_samples, model):
"""Evaluate IFEval dataset."""
print("\nLoading IFEval dataset...")
# Try test split first, then train
try:
dataset = load_dataset("google/IFEval", split="test")
split_used = "test"
except:
dataset = load_dataset("google/IFEval", split="train")
split_used = "train"
# Determine samples to process
if num_samples is None:
samples_to_process = len(dataset)
print(f"Using full {split_used} split: {samples_to_process} samples")
dataset_iter = tqdm(dataset, desc="Evaluating")
else:
samples_to_process = min(num_samples, len(dataset))
print(f"Using {samples_to_process} samples from {split_used} split")
dataset = load_dataset("google/IFEval", split=split_used, streaming=True)
dataset_iter = tqdm(
dataset.take(samples_to_process), total=samples_to_process, desc="Evaluating"
)
correct = 0
total = 0
empty_responses = 0
for i, example in enumerate(dataset_iter):
if num_samples is not None and i >= samples_to_process:
break
instruction = example["prompt"]
try:
formatted_prompt = prompt_template.format(instruction=instruction)
except KeyError as e:
print(f"Error: Prompt template missing placeholder: {e}")
return 0.0, 0, total, total
# Call LLM with retries
output_text = None
for attempt in range(3):
try:
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": formatted_prompt}],
temperature=0.1,
max_tokens=4096,
)
if response and response.choices and response.choices[0].message:
output_text = response.choices[0].message.content
if output_text and output_text.strip():
break
except Exception as e:
if attempt == 2:
print(f"\nError after 3 attempts: {e}")
time.sleep(2)
if not output_text or not output_text.strip():
empty_responses += 1
else:
# Simple evaluation: response has reasonable length
if len(output_text.strip()) > 20:
correct += 1
total += 1
accuracy = correct / total if total > 0 else 0.0
return accuracy, correct, total, empty_responses
def evaluate_hover(client, prompt_template, num_samples, model):
"""Evaluate HoVer dataset."""
print("\nLoading HoVer dataset...")
# Try test split first (but it's unlabeled), then validation
try:
test_dataset = load_dataset("hover", split="test", trust_remote_code=True)
# Check if test set has labels
if test_dataset[0]["label"] != -1:
dataset = test_dataset
split_used = "test"
else:
# Test set is unlabeled, use validation
dataset = load_dataset("hover", split="validation", trust_remote_code=True)
split_used = "validation"
except:
dataset = load_dataset("hover", split="validation", trust_remote_code=True)
split_used = "validation"
# Determine samples to process
if num_samples is None:
samples_to_process = len(dataset)
print(f"Using full {split_used} split: {samples_to_process} samples")
dataset_iter = tqdm(dataset, desc="Evaluating")
else:
samples_to_process = min(num_samples, len(dataset))
print(f"Using {samples_to_process} samples from {split_used} split")
dataset = load_dataset("hover", split=split_used, streaming=True, trust_remote_code=True)
dataset_iter = tqdm(
dataset.take(samples_to_process), total=samples_to_process, desc="Evaluating"
)
correct = 0
total = 0
empty_responses = 0
for i, example in enumerate(dataset_iter):
if num_samples is not None and i >= samples_to_process:
break
claim = example["claim"]
label = example["label"] # Integer: 0=SUPPORTED, 1=NOT_SUPPORTED
try:
formatted_prompt = prompt_template.format(claim=claim)
except KeyError as e:
print(f"Error: Prompt template missing placeholder: {e}")
return 0.0, 0, total, total
# Call LLM with retries
output_text = None
for attempt in range(3):
try:
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": formatted_prompt}],
temperature=0.1,
max_tokens=4096,
)
if response and response.choices and response.choices[0].message:
output_text = response.choices[0].message.content
if output_text and output_text.strip():
break
except Exception as e:
if attempt == 2:
print(f"\nError after 3 attempts: {e}")
time.sleep(2)
if not output_text or not output_text.strip():
empty_responses += 1
else:
output_upper = output_text.strip().upper()
# Parse prediction from output
if "NOT SUPPORTED" in output_upper or "NOT_SUPPORTED" in output_upper:
prediction = 1 # NOT_SUPPORTED
elif "SUPPORTED" in output_upper:
prediction = 0 # SUPPORTED
else:
prediction = -1 # Invalid/unclear response
# Compare with actual label
if prediction == label:
correct += 1
total += 1
accuracy = correct / total if total > 0 else 0.0
return accuracy, correct, total, empty_responses
def evaluate_hotpotqa(client, prompt_template, num_samples, model):
"""Evaluate HotpotQA dataset."""
print("\nLoading HotpotQA dataset (this may take a moment)...")
# Try test split first, then validation
try:
dataset = load_dataset(
"hotpotqa/hotpot_qa", "distractor", split="test", trust_remote_code=True
)
split_used = "test"
except:
dataset = load_dataset(
"hotpotqa/hotpot_qa", "distractor", split="validation", trust_remote_code=True
)
split_used = "validation"
print(f"Dataset loaded. Using {split_used} split with {len(dataset)} samples")
# Determine samples to process
if num_samples is None:
samples_to_process = len(dataset)
print(f"Using full dataset: {samples_to_process} samples")
else:
samples_to_process = min(num_samples, len(dataset))
print(f"Using {samples_to_process} samples")
correct = 0
total = 0
empty_responses = 0
for i in tqdm(range(samples_to_process), desc="Evaluating"):
example = dataset[i]
question = example["question"]
context = example["context"]
answer = example["answer"].lower().strip()
# Format context
context_str = ""
titles = context["title"]
sentences = context["sentences"]
for title, sents in zip(titles, sentences):
context_str += f"{title}: {' '.join(sents)}\n"
try:
formatted_prompt = prompt_template.format(
context=context_str.strip(), question=question
)
except KeyError as e:
print(f"Error: Prompt template missing placeholders: {e}")
return 0.0, 0, total, total
# Call LLM with retries
output_text = None
for attempt in range(3):
try:
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": formatted_prompt}],
temperature=0.1,
max_tokens=4096,
)
if response and response.choices and response.choices[0].message:
output_text = response.choices[0].message.content
if output_text and output_text.strip():
break
except Exception as e:
if attempt == 2:
print(f"\nError after 3 attempts: {e}")
time.sleep(2)
if not output_text or not output_text.strip():
empty_responses += 1
else:
output_lower = output_text.strip().lower()
# Check if answer is in output
if answer in output_lower:
correct += 1
total += 1
accuracy = correct / total if total > 0 else 0.0
return accuracy, correct, total, empty_responses
def main():
parser = argparse.ArgumentParser(description="Evaluate prompts on GEPA benchmark datasets")
parser.add_argument(
"--dataset",
type=str,
required=True,
choices=["ifeval", "hover", "hotpotqa", "all"],
help="Dataset to evaluate on",
)
parser.add_argument(
"--prompt-type",
type=str,
default="baseline",
choices=["baseline", "evolved"],
help="Type of prompt to use",
)
parser.add_argument(
"--samples",
type=int,
default=None,
help="Number of samples to evaluate (default: full dataset)",
)
parser.add_argument(
"--model", type=str, default="qwen/qwen3-8b", help="Model to use for evaluation"
)
parser.add_argument(
"--output", type=str, default=None, help="Output file for results (default: auto-generated)"
)
args = parser.parse_args()
# Initialize client
client = get_client()
# Determine which datasets to evaluate
if args.dataset == "all":
datasets = ["ifeval", "hover", "hotpotqa"]
else:
datasets = [args.dataset]
# Evaluation functions
eval_funcs = {"ifeval": evaluate_ifeval, "hover": evaluate_hover, "hotpotqa": evaluate_hotpotqa}
# Load baseline results for comparison
baseline_results = {}
if os.path.exists("baseline_results_50samples.json"):
with open("baseline_results_50samples.json", "r") as f:
baseline_data = json.load(f)
for result in baseline_data.get("results", []):
baseline_results[result["dataset"]] = result["accuracy"]
# Store results
all_results = []
print(f"\n{'='*60}")
print(f"PROMPT EVALUATION - {args.prompt_type.upper()}")
print(f"Model: {args.model}")
if args.samples:
print(f"Samples per dataset: {args.samples}")
else:
print(f"Samples per dataset: Full dataset")
print(f"{'='*60}")
for dataset_name in datasets:
print(f"\nEvaluating {dataset_name.upper()}...")
try:
# Load prompt
prompt_template = load_prompt(dataset_name, args.prompt_type)
print(f"Loaded {args.prompt_type} prompt ({len(prompt_template)} chars)")
# Run evaluation
start_time = time.time()
accuracy, correct, total, empty_responses = eval_funcs[dataset_name](
client, prompt_template, args.samples, args.model
)
elapsed_time = time.time() - start_time
# Get baseline accuracy
baseline_acc = baseline_results.get(dataset_name)
if baseline_acc:
improvement = ((accuracy - baseline_acc) / baseline_acc) * 100
else:
improvement = 0
# Store result
result = {
"dataset": dataset_name,
"prompt_type": args.prompt_type,
"accuracy": accuracy,
"baseline_accuracy": baseline_acc,
"improvement_percent": improvement,
"correct": correct,
"total": total,
"empty_responses": empty_responses,
"elapsed_time": elapsed_time,
"timestamp": datetime.now().isoformat(),
}
all_results.append(result)
# Print results
print(f"\nResults for {dataset_name.upper()}:")
print(f" Accuracy: {accuracy:.3f} ({correct}/{total})")
if baseline_acc:
print(f" Baseline: {baseline_acc:.3f}")
print(f" Improvement: {improvement:+.1f}%")
print(f" Empty responses: {empty_responses}")
print(f" Time: {elapsed_time:.1f}s ({elapsed_time/total:.1f}s per sample)")
except Exception as e:
print(f"Error evaluating {dataset_name}: {str(e)}")
all_results.append(
{
"dataset": dataset_name,
"prompt_type": args.prompt_type,
"error": str(e),
"timestamp": datetime.now().isoformat(),
}
)
# Save results
output_path = args.output
if not output_path:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
output_path = f"evaluation_results_{args.prompt_type}_{timestamp}.json"
final_results = {
"prompt_type": args.prompt_type,
"model": args.model,
"samples_per_dataset": args.samples,
"timestamp": datetime.now().isoformat(),
"results": all_results,
}
# Calculate aggregate statistics
valid_results = [r for r in all_results if "error" not in r]
if valid_results:
total_correct = sum(r["correct"] for r in valid_results)
total_samples = sum(r["total"] for r in valid_results)
aggregate_accuracy = total_correct / total_samples if total_samples > 0 else 0
final_results["summary"] = {
"aggregate_accuracy": aggregate_accuracy,
"total_correct": total_correct,
"total_samples": total_samples,
"datasets_evaluated": len(valid_results),
}
with open(output_path, "w") as f:
json.dump(final_results, f, indent=2)
# Print summary
print(f"\n{'='*60}")
print("EVALUATION SUMMARY")
print(f"{'='*60}")
for result in all_results:
if "error" not in result:
print(f"\n{result['dataset'].upper()}:")
print(f" Accuracy: {result['accuracy']:.3f}")
if result.get("baseline_accuracy"):
print(f" vs Baseline: {result['improvement_percent']:+.1f}%")
if "summary" in final_results:
print(f"\nAGGREGATE:")
print(f" Overall Accuracy: {final_results['summary']['aggregate_accuracy']:.3f}")
print(f" Total Samples: {final_results['summary']['total_samples']}")
print(f"\nResults saved to: {output_path}")
if __name__ == "__main__":
main()