-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtier2.py
More file actions
633 lines (558 loc) · 26.6 KB
/
tier2.py
File metadata and controls
633 lines (558 loc) · 26.6 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
import json
import os
import asyncio
from typing import Dict, Any
import re
from dotenv import load_dotenv
from actions import get_actions_as_json
from generator import (
generate_home_room_pddl_scenario,
DEFAULT_OBJECT_CONFIG,
)
from utils import merge_objects_configs, find_tript, create_reasoning_prompt
from llms import get_llm, generate_concurrently
from tqdm import tqdm
def generate_prompt_for_evaluation(
pddl_problem: str,
initial_context: str,
candidate_action: str,
) -> str:
"""
Generates a prompt that asks the model to rate a single candidate action.
"""
full_action_list_json_str = get_actions_as_json()
instruction = f"""Based on the Action Sequence so far, please evaluate the appropriateness of the following candidate action.
**Candidate Action to Evaluate:**
{candidate_action}
**Evaluation Criteria:**
Choose one of the following ratings: 5) Very Appropriate 4) Appropriate 3) Neutral 2) Inappropriate 1) Very Inappropriate
Your response should contain the answer in the format "rating(X)" where X is a single number (1-5) indicating your rating.
For example:
I think the action is rating(4)
"""
action_sequence_block = f"""**Action Sequence:**
{initial_context}
"""
return f"""You are an embodied AI agent in a physical environment. Your task is to take sequence of actions and finish the final goal. In the process, you will interact with physical world and take decisions, actions based on the environment and objects information around you.
**Action Definitions:**
{full_action_list_json_str}
**PDDL Problem Definition:**
{pddl_problem}
{action_sequence_block}
**Instruction:**
{instruction}
"""
def generate_prompt_for_action_selection(
pddl_problem: str,
initial_context: str,
action_candidates: list[str],
) -> str:
"""
Generates a prompt that asks the model to select the best action from a list.
"""
full_action_list_json_str = get_actions_as_json()
action_list = "\n".join(
f"{i+1}. {action}" for i, action in enumerate(action_candidates)
)
instruction = f"""Based on the Action Sequence so far, please select the most appropriate action from the following list.
**Candidate Actions:**
{action_list}
Your response should be in the format "selection(X)" where X is the number of the action you choose.
For example:
selection(1)
"""
action_sequence_block = f"""**Action Sequence:**
{initial_context}
"""
return f"""You are an embodied AI agent in a physical environment. Your task is to take sequence of actions and finish the final goal. In the process, you will interact with physical world and take decisions, actions based on the environment and objects information around you.
**Action Definitions:**
{full_action_list_json_str}
**PDDL Problem Definition:**
{pddl_problem}
{action_sequence_block}
**Instruction:**
{instruction}
"""
def main(
model_name: str,
evaluation_mode: str,
num_variations: int = 1,
force_generate: bool = False,
skip_rerun: bool = False,
re_evaluate: bool = False,
get_reasoning: bool = False,
):
"""
Main function to generate prompts for Tier 2.
For each situation, it evaluates every possible action from a unified list.
"""
load_dotenv()
llm = get_llm(model_name=model_name)
script_dir = os.path.dirname(os.path.abspath(__file__))
tier2_input_file = os.path.join(script_dir, "eai_bench", "tier_2.json")
output_dir = os.path.join(script_dir, "output")
prompts_dir = os.path.join(script_dir, "prompts")
os.makedirs(output_dir, exist_ok=True)
os.makedirs(prompts_dir, exist_ok=True)
prompts_file = os.path.join(
prompts_dir, f"tier2_{evaluation_mode}_variations_{num_variations}_prompts.json"
)
results_file_name = f"tier2_{evaluation_mode}_variations_{num_variations}_model_{model_name}_results.json"
if get_reasoning:
results_file_name = results_file_name.replace(".json", "_with_reasoning.json")
results_file = os.path.join(output_dir, results_file_name)
if get_reasoning and (skip_rerun or re_evaluate):
if not os.path.exists(results_file):
print(
f"Error: Results file not found at '{results_file}'. Cannot get reasoning."
)
return
print(f"Loading existing results from '{results_file}' to get reasoning...")
with open(results_file, "r") as f:
existing_results = json.load(f)
# Filter out results that already have reasoning
results_to_process = [
item for item in existing_results if "llm_reasoning" not in item
]
if not results_to_process:
print("All results already have reasoning. Exiting.")
return
print(
f"Found {len(results_to_process)} results without reasoning. Generating now..."
)
reasoning_prompts = [
create_reasoning_prompt(item["prompt"], item["llm_response"])
for item in results_to_process
]
reasoning_responses = asyncio.run(generate_concurrently(llm, reasoning_prompts))
# Create a map to update existing results
response_to_reasoning_map = {
results_to_process[i]["llm_response"]: reasoning_responses[i]
for i in range(len(results_to_process))
}
# Update the original list
for item in existing_results:
if item["llm_response"] in response_to_reasoning_map:
item["llm_reasoning"] = response_to_reasoning_map[item["llm_response"]]
with open(results_file, "w") as f:
json.dump(existing_results, f, indent=4)
print(f"Successfully added reasoning to results file: {results_file}")
return
elif get_reasoning and not (skip_rerun or re_evaluate):
# This case is handled in the main generation block
pass
if re_evaluate:
if not os.path.exists(results_file):
print(
f"Error: Cannot re-evaluate. Results file not found at '{results_file}'"
)
return
print(f"Loading existing results from '{results_file}' to re-evaluate...")
with open(results_file, "r") as f:
existing_results = json.load(f)
all_results = []
for item in tqdm(existing_results, desc="Re-evaluating responses"):
response = item["llm_response"]
try:
if evaluation_mode == "rating":
answer = response.split("rating(")[-1].split(")")[0]
item["llm_rating"] = int(answer)
elif evaluation_mode == "selection":
answer = response.split("selection(")[-1].split(")")[0]
item["llm_selection"] = int(answer)
except (ValueError, IndexError):
item[
"llm_rating" if evaluation_mode == "rating" else "llm_selection"
] = -1
all_results.append(item)
with open(results_file, "w") as f:
json.dump(all_results, f, indent=4)
print(f"Re-evaluation complete. Updated results saved to {results_file}")
elif not skip_rerun:
print("--- Generating prompts and running model ---")
with open(tier2_input_file, "r") as f:
scenarios = json.load(f)
if not force_generate and os.path.exists(prompts_file):
print(f"Loading prompts from {prompts_file}")
with open(prompts_file, "r") as f:
all_prompt_data = json.load(f)
else:
print(
f"Generating {num_variations} variations for each of the {len(scenarios)} scenarios..."
)
all_prompt_data = []
for scenario in tqdm(scenarios, desc="Generating Prompts"):
for i in range(num_variations):
unified_action_candidates = sorted(
list(
set(
action["action"]
for state in scenario["environment_states"]
for action in state["candidate_action_ratings"]
)
)
)
scenario_objects = scenario.get("pddl_objects", {})
full_objects_config = merge_objects_configs(
DEFAULT_OBJECT_CONFIG, scenario_objects
)
room_type_key = next(
(
k
for k in scenario_objects
if any(s in k for s in ["office", "corridor", "room"])
),
"room",
)
room_name = room_type_key.split(".")[0]
for true_state in scenario["environment_states"]:
pddl_problem, _, _ = generate_home_room_pddl_scenario(
room_name=room_name,
objects_config=full_objects_config,
total_small_items=30,
max_furniture_amount=5,
min_small_items_per_container=1,
must_include_item_types=list(scenario_objects.keys()),
custom_goal_str=scenario["main_task"],
)
primary_location_instance = f"{room_type_key}_1"
initial_context = (
f"Take Action 1:\n Action: receive_task('{scenario['main_task']}')\n Return: 'Acknowledged. Proceeding with task.'\n\n"
f"Take Action 2:\n Action: look_at('{primary_location_instance}')\n Return: 'Observation: {true_state['perception_cues']}'"
)
# Find the action with the highest average_rating (or expected_rating as fallback)
highest_rating = 0
correct_next_action = None
for action_data in true_state["candidate_action_ratings"]:
rating = action_data.get(
"average_rating"
) or action_data.get("expected_rating", 0)
if rating > highest_rating:
highest_rating = rating
correct_next_action = action_data["action"]
if (
correct_next_action is None
and evaluation_mode == "selection"
):
print(
f"Warning: No action with expected_rating 5 found for scenario {scenario['scenario_name']}, state {true_state['state_name']}."
)
if evaluation_mode == "rating":
for candidate_action in unified_action_candidates:
prompt = generate_prompt_for_evaluation(
pddl_problem, initial_context, candidate_action
)
# Find the actual rating for this action from the JSON
action_rating_data = next(
(
action_data
for action_data in true_state[
"candidate_action_ratings"
]
if action_data["action"] == candidate_action
),
None,
)
if action_rating_data:
# Use average_rating if available, otherwise fall back to expected_rating
expected_rating = action_rating_data.get(
"average_rating"
) or action_rating_data.get("expected_rating", 1)
else:
# Fallback to old logic if action not found in ratings
expected_rating = (
5
if candidate_action == correct_next_action
else 1
)
prompt_data = {
"scenario_name": scenario["scenario_name"],
"true_state": true_state["state_name"],
"candidate_action_to_evaluate": candidate_action,
"expected_rating": expected_rating,
"average_rating": expected_rating, # Store the same value as average_rating for consistency
"prompt": prompt,
"variation_id": i,
}
all_prompt_data.append(prompt_data)
elif evaluation_mode == "selection":
possible_tripts = find_tript(
true_state["candidate_action_ratings"]
)
if possible_tripts:
selected_candidates = possible_tripts[0]
action_to_rating_map = {
action["action"]: action.get("average_rating")
or action.get("expected_rating", 1)
for action in true_state["candidate_action_ratings"]
}
best_action_in_tript = max(
selected_candidates,
key=lambda action: action_to_rating_map.get(
action, -1
),
)
correct_selection_index = (
selected_candidates.index(best_action_in_tript) + 1
)
prompt = generate_prompt_for_action_selection(
pddl_problem, initial_context, selected_candidates
)
prompt_data = {
"scenario_name": scenario["scenario_name"],
"true_state": true_state["state_name"],
"action_candidates": selected_candidates,
"correct_selection": correct_selection_index,
"action_ratings": {
action: action_to_rating_map.get(action, -1)
for action in selected_candidates
},
"prompt": prompt,
"variation_id": i,
}
all_prompt_data.append(prompt_data)
else:
print(
f"Warning: No suitable action triplet could be generated for scenario '{scenario['scenario_name']}', state '{true_state['state_name']}'."
)
os.makedirs(os.path.dirname(prompts_file), exist_ok=True)
with open(prompts_file, "w") as f:
json.dump(all_prompt_data, f, indent=4)
print(f"Saved {len(all_prompt_data)} generated prompts to {prompts_file}")
prompts = [item["prompt"] for item in all_prompt_data]
print(f"Sending {len(prompts)} prompts to the model...")
responses = asyncio.run(generate_concurrently(llm, prompts))
if get_reasoning:
print("\n--- Getting reasoning for responses ---")
reasoning_prompts = [
create_reasoning_prompt(item["prompt"], responses[i])
for i, item in enumerate(all_prompt_data)
]
print(f"Sending {len(reasoning_prompts)} reasoning prompts to the model...")
reasoning_responses = asyncio.run(
generate_concurrently(llm, reasoning_prompts)
)
all_results = []
invalid_format_count = 0
for i, item in enumerate(all_prompt_data):
response = responses[i]
result_item = item.copy()
result_item["llm_response"] = response
if get_reasoning:
result_item["llm_reasoning"] = reasoning_responses[i]
try:
if evaluation_mode == "rating":
answer = response.split("rating(")[-1].split(")")[0]
result_item["llm_rating"] = int(answer)
elif evaluation_mode == "selection":
answer = response.split("selection(")[-1].split(")")[0]
result_item["llm_selection"] = int(answer)
except (ValueError, IndexError):
result_item[
"llm_rating" if evaluation_mode == "rating" else "llm_selection"
] = -1
invalid_format_count += 1
all_results.append(result_item)
os.makedirs(os.path.dirname(results_file), exist_ok=True)
with open(results_file, "w") as f:
json.dump(all_results, f, indent=4)
print(f"Generated {len(all_results)} results and saved to {results_file}")
# --- Analysis Phase ---
print(f"\n--- Analyzing results from {results_file} ---")
if not os.path.exists(results_file):
print(f"Error: Results file not found at {results_file}. Cannot analyze.")
return
with open(results_file, "r") as f:
all_results = json.load(f)
print(f"Loaded {len(all_results)} results for analysis.")
invalid_format_count = 0
for r in all_results:
key = "llm_rating" if evaluation_mode == "rating" else "llm_selection"
if r.get(key) == -1:
invalid_format_count += 1
print(
f"Found {invalid_format_count} invalid formats out of {len(all_results)} total."
)
if evaluation_mode == "selection":
total_valid_selections = 0
correct_selections = 0
wrong_selections = 0
wrong_and_hard_negative = 0
valid_results = [
r
for r in all_results
if r.get("llm_selection") is not None and r.get("llm_selection") != -1
]
total_valid_selections = len(valid_results)
for result in valid_results:
llm_selection = result.get("llm_selection")
correct_selection = result.get("correct_selection")
if llm_selection == correct_selection:
correct_selections += 1
else:
wrong_selections += 1
action_ratings = result.get("action_ratings")
if action_ratings:
chosen_action_index = llm_selection - 1
if 0 <= chosen_action_index < len(result["action_candidates"]):
chosen_action = result["action_candidates"][chosen_action_index]
chosen_rating = action_ratings.get(chosen_action)
if chosen_rating is not None and chosen_rating <= 2:
wrong_and_hard_negative += 1
print(f"\n--- Tier 2 Analysis (mode='selection') ---")
if total_valid_selections > 0:
accuracy = (correct_selections / total_valid_selections) * 100
print(
f"Accuracy: {accuracy:.2f}% ({correct_selections}/{total_valid_selections} correct)"
)
else:
print("Accuracy: No valid selections found.")
if wrong_selections > 0:
hard_negative_rate = (wrong_and_hard_negative / wrong_selections) * 100
print(
f"Hard Negative Rate (among wrong choices): {hard_negative_rate:.2f}% ({wrong_and_hard_negative}/{wrong_selections} hard negative)"
)
else:
print("Hard Negative Rate: No wrong selections to analyze.")
elif evaluation_mode == "rating":
# Analysis for rating mode - compare LLM ratings with human ratings (primary) and benchmark ratings (secondary)
valid_results = [
r
for r in all_results
if r.get("llm_rating") is not None and r.get("llm_rating") != -1
]
if not valid_results:
print("No valid rating results to analyze.")
return
total_comparisons = len(valid_results)
differences_human = []
differences_benchmark = []
print(f"\n--- Tier 2 Analysis (mode='rating') ---")
print(f"Total valid ratings: {total_comparisons}")
for result in valid_results:
llm_rating = result.get("llm_rating")
# Use average_rating directly for comparison
average_rating = result.get("average_rating")
if average_rating is not None:
diff_avg = abs(llm_rating - average_rating)
differences_human.append(diff_avg)
# Also track expected_rating for reference comparison
expected_rating = result.get("expected_rating")
if expected_rating is not None:
diff_benchmark = abs(llm_rating - expected_rating)
differences_benchmark.append(diff_benchmark)
# Primary analysis: LLM vs Human Average ratings
if differences_human:
avg_diff_combined = sum(differences_human) / len(differences_human)
max_diff_combined = max(differences_human)
exact_matches_combined = sum(1 for d in differences_human if d == 0)
close_matches_combined = sum(1 for d in differences_human if d <= 0.5)
print(f"\n--- LLM vs Human Average Ratings (Primary) ---")
print(f"Average difference: {avg_diff_combined:.2f}")
print(f"Maximum difference: {max_diff_combined:.1f}")
print(
f"Exact matches: {exact_matches_combined}/{len(differences_human)} ({exact_matches_combined/len(differences_human)*100:.1f}%)"
)
print(
f"Close matches (≤0.5): {close_matches_combined}/{len(differences_human)} ({close_matches_combined/len(differences_human)*100:.1f}%)"
)
print(
f"Large differences (>2.0): {sum(1 for d in differences_human if d > 2.0)}"
)
else:
print("\n--- No Human Average Ratings Available ---")
print("Human average rating data is missing from the results.")
# Secondary analysis: Expected ratings only (for comparison)
if differences_benchmark:
avg_diff_benchmark = sum(differences_benchmark) / len(differences_benchmark)
max_diff_benchmark = max(differences_benchmark)
exact_matches_benchmark = sum(1 for d in differences_benchmark if d == 0)
close_matches_benchmark = sum(1 for d in differences_benchmark if d <= 0.5)
print(f"\n--- LLM vs Expected Ratings Only (Reference) ---")
print(f"Average difference: {avg_diff_benchmark:.2f}")
print(f"Maximum difference: {max_diff_benchmark:.1f}")
print(
f"Exact matches: {exact_matches_benchmark}/{len(differences_benchmark)} ({exact_matches_benchmark/len(differences_benchmark)*100:.1f}%)"
)
print(
f"Close matches (≤0.5): {close_matches_benchmark}/{len(differences_benchmark)} ({close_matches_benchmark/len(differences_benchmark)*100:.1f}%)"
)
print(
f"Large differences (>2.0): {sum(1 for d in differences_benchmark if d > 2.0)}"
)
# Comparison summary
if differences_human:
print(f"\n--- Comparison Summary ---")
print(
f"LLM-Human Average vs LLM-Expected average difference: {avg_diff_combined:.2f} vs {avg_diff_benchmark:.2f}"
)
if avg_diff_combined < avg_diff_benchmark:
improvement = (
(avg_diff_benchmark - avg_diff_combined) / avg_diff_benchmark
) * 100
print(
f"✓ LLM ratings are {improvement:.1f}% closer to human average ratings than expected ratings"
)
else:
print(
"✗ LLM ratings are closer to expected ratings than human average ratings"
)
if not differences_human and not differences_benchmark:
print("\n--- No Valid Comparisons Found ---")
print(
"This might indicate missing human average rating or expected rating data."
)
if __name__ == "__main__":
import argparse
from llms import SUPPORTED_MODELS
parser = argparse.ArgumentParser(description="Run Tier 2 evaluation.")
parser.add_argument(
"--model_name",
type=str,
required=True,
choices=list(SUPPORTED_MODELS.keys()),
help="The name of the LLM to evaluate.",
)
parser.add_argument(
"--evaluation_mode",
type=str,
default="rating",
choices=["rating", "selection"],
help="The evaluation mode to use.",
)
parser.add_argument(
"--num_variations",
type=int,
default=1,
help="Number of random variations to generate per scenario.",
)
parser.add_argument(
"--force_generate",
action="store_true",
help="Force regeneration of prompts, overwriting existing ones.",
)
parser.add_argument(
"--skip_rerun",
action="store_true",
help="Skip rerunning the model and just analyze existing results.",
)
parser.add_argument(
"--re_evaluate",
action="store_true",
help="Re-evaluate existing results without calling the model.",
)
parser.add_argument(
"--get_reasoning",
action="store_true",
help="Get reasoning for the model's answer.",
)
args = parser.parse_args()
main(
model_name=args.model_name,
evaluation_mode=args.evaluation_mode,
num_variations=args.num_variations,
force_generate=args.force_generate,
skip_rerun=args.skip_rerun,
re_evaluate=args.re_evaluate,
get_reasoning=args.get_reasoning,
)