-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvaluator.py
More file actions
139 lines (99 loc) · 3.95 KB
/
Copy pathEvaluator.py
File metadata and controls
139 lines (99 loc) · 3.95 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
# Copyright (c) 2019-2020, INESC TEC (https://www.inesctec.pt)
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import json
import pandas as pd
from llm_utils import call_llm
# -----------------------------
# CONFIGURATION
# -----------------------------
DESIGN = 'base'#1
EVALUATOR_PROMPT_PATH = "Evaluator_prompt.txt"
EXPLANATIONS_PATH = f"test_actor_explanations_{DESIGN}.json"
SNAPSHOTS_PATH = "test_snapshots_case39_cf2.json"
OUTPUT_CSV = f"evaluation_scores_design_{DESIGN}.csv"
# -----------------------------
# LOAD PROMPT
# -----------------------------
def load_prompt(path):
with open(path, "r", encoding="utf-8") as f:
return f.read()
# -----------------------------
# RUN EVALUATOR LLM
# -----------------------------
def run_evaluator(evaluator_prompt, snapshot, explanation):
prompt = f"""
{evaluator_prompt}
POWER SYSTEM SNAPSHOT:
{json.dumps(snapshot, indent=2)}
LLM EXPLANATION:
{explanation}
Return ONLY valid JSON according to the specified output format.
"""
response = call_llm(prompt, role="Evaluator", temperature=0.1)
try:
scores = json.loads(response)
except Exception:
print("⚠ Failed to parse evaluator response")
print(response)
scores = {
"section_scores": {},
"question_scores": {},
"total_score": None,
"percentage": None,
"evaluation_summary": None
}
return scores
# -----------------------------
# MAIN
# -----------------------------
def main():
print("Loading evaluator prompt...")
evaluator_prompt = load_prompt(EVALUATOR_PROMPT_PATH)
print("Loading explanations...")
with open(EXPLANATIONS_PATH, "r", encoding="utf-8") as f:
explanations = json.load(f)
print("Loading snapshots...")
with open(SNAPSHOTS_PATH, "r", encoding="utf-8") as f:
snapshots = json.load(f)
results = []
for snapshot_id, explanation in explanations.items():
print(f"\nEvaluating snapshot: {snapshot_id}")
snapshot = snapshots[snapshot_id]["opf_summary"]
scores = run_evaluator(
evaluator_prompt,
snapshot,
explanation
)
section = scores.get("section_scores", {})
questions = scores.get("question_scores", {})
row = {
"snapshot_id": snapshot_id,
# Section scores
"redispatch_trigger_identification": section.get("redispatch_trigger_identification"),
"operational_logic": section.get("operational_logic"),
"ptdf_directional_reasoning": section.get("ptdf_directional_reasoning"),
"counterfactual_reasoning": section.get("counterfactual_reasoning"),
"numerical_grounding_and_report_discipline": section.get("numerical_grounding_and_report_discipline"),
"economic_and_dual_interpretation": section.get("economic_and_dual_interpretation"),
"security_screening": section.get("security_screening"),
"language_and_operator_usefulness": section.get("language_and_operator_usefulness"),
# Global metrics
"total_score": scores.get("total_score"),
"percentage": scores.get("percentage"),
"evaluation_summary": scores.get("evaluation_summary"),
}
# Store question scores (Q1–Q20)
for q in range(1, 21):
row[f"Q{q}"] = questions.get(f"Q{q}")
results.append(row)
df = pd.DataFrame(results)
df.to_csv(OUTPUT_CSV, index=False)
print("\n✅ Evaluation completed")
print(f"Scores saved to: {OUTPUT_CSV}")
# -----------------------------
# RUN SCRIPT
# -----------------------------
if __name__ == "__main__":
main()