forked from jinhaoduan/GTBench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNRA_test.py
289 lines (214 loc) · 8.66 KB
/
NRA_test.py
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
import os
import json
import subprocess
import sys
import numpy as np
import datetime
# Get the current datetime
current_time = datetime.datetime.now()
# Format the datetime to MM-DD-YYYY-HH-MM
formatted_time = current_time.strftime("%m-%d_%H-%M")
directory = f"./experiments_{formatted_time}"
if not os.path.exists(directory):
os.makedirs(directory)
log_file_name = f"./experiments_{formatted_time}/run_log.txt"
def overall_log(file_name, line_to_write):
# Open the file in append mode ('a') so that data is written to the end of the file
# If the file does not exist, it will be created
with open(file_name, 'a') as file:
# Write the line_to_write to the file, and add a newline character to ensure
# it starts on a new line
file.write(line_to_write + '\n')
def NRA_value_calculation(me_score, opponent_score):
'''
return Normalized Relative Advantage (NRA) value
'''
# avoid division by zero
if me_score == 0 and opponent_score == 0:
return 0
return (me_score - opponent_score) / (abs(me_score) + abs(opponent_score))
def process_game_data(json_data):
status = json_data["matches"][0]["status"]
if status != "Normal":
return "ABNORMAL STATUS"
agents_config = json_data['agents_config']
models_config = json_data['models_config']
matches = json_data['matches']
agents_info = []
for agent, model in zip(agents_config, models_config):
agents_info.append({
'agent_name': agent['agent_name'],
'nickname': model['nick_name']
})
me = f'{agents_info[0]["agent_name"]}_{agents_info[0]["nickname"]}'
opponent = f'{agents_info[1]["agent_name"]}_{agents_info[1]["nickname"]}'
if matches:
winner = matches[0]['winner']
winner_score = matches[0]['winner_score']
loser_score = matches[0]['loser_score']
# make dic mapping agent name to score
if winner == me:
scores = {me: winner_score,
opponent: loser_score}
else:
scores = {me: loser_score,
opponent: winner_score}
else:
winner = None
scores = {me: 0,
opponent: 0}
return {
'agents': agents_info,
'winner': winner,
'scores': scores,
}
def evaluate_single_file(file_path):
# just read the first line
with open(file_path, 'r') as file:
for line in file:
json_data = json.loads(line.strip())
result = process_game_data(json_data)
return result
# RUN JUST ONE MATCH
def run_single_experiment(
seed=0,
output_root=f"./experiments_{formatted_time}",
exp_name='test',
num_matches=1,
num_workers=1,
threshold_matches=1,
game_name="crazy_eights",
model_config_root='gamingbench/configs/model_configs',
llm_name='Bob',
opponent_llm_name='Alice',
agent_config_root='gamingbench/configs/agent_configs',
agent_name='prompt_agent',
opponent_agent_name='prompt_agent',
api_keys=["" ""] # {OPEN AI API KEY} {DEEP INFRA API KEY}
):
rand = str(int(np.random.rand() * 1000))
command = [
"python3", "-W", "ignore", "-m", "gamingbench.main",
"--num-matches", str(num_matches),
"--exp-root", f"{output_root}/{exp_name}",
"--seed", rand,
"--game-name", game_name,
"--agent-configs",
f"{agent_config_root}/{agent_name}.yaml",
f"{agent_config_root}/{opponent_agent_name}.yaml",
"--model-configs",
f"{model_config_root}/{llm_name}.yaml",
f"{model_config_root}/{opponent_llm_name}.yaml",
"--api-keys", *api_keys,
"--exchange-first-player",
"--num-workers", str(num_workers),
"--threshold-matches", str(threshold_matches)
]
# for debugging purposes
# print("Running command:")
# print(" ".join(command))
try:
# result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
resut = subprocess.run(command)
# print("Command output:", result.stdout)
# print("Error output:", result.stderr)
except subprocess.CalledProcessError as e:
print("Error running command:", e)
def setup_and_run_experiments(model, method, game, n):
static_model = model
exp_name = f'{static_model}_{method}_{n}'
output_root = f'./experiments_{formatted_time}'
output_dir = os.path.join(output_root, exp_name)
os.makedirs(output_dir, exist_ok=True)
if n % 2 == 1:
opponent_model = 'gpt-35-turbo-1106'
opponent_method = "prompt_agent"
model = model
method = method
else:
opponent_model = model
opponent_method = method
model = 'gpt-35-turbo-1106'
method = "prompt_agent"
run_single_experiment(
output_root=output_root,
exp_name=exp_name,
game_name=game,
llm_name=model,
opponent_llm_name=opponent_model,
agent_name=method.lower(),
opponent_agent_name=opponent_method.lower(),
)
def main():
if len(sys.argv) != 5:
print("NRA_test.py usage: python3 NRA_test.py {game} {opponent_llm_model} {llm_reasoning_type} {num_matches}")
print("NRA_test.py usage example: python3 NRA_test.py crazy_eights gpt-4-turbo prompt_agent 50")
sys.exit(1)
line_to_write = "python3"
for el in sys.argv:
line_to_write += " " + el
overall_log(log_file_name, line_to_write)
game = sys.argv[1]
model = sys.argv[2]
method = sys.argv[3]
num_matches = int(sys.argv[4])
valid_matches_list = []
overall_number_of_matches_run = 1
# while False:
while len(valid_matches_list) < num_matches:
print(f"--------- On Match #{overall_number_of_matches_run:02} ---------")
overall_log(log_file_name, f"--------- On Match #{overall_number_of_matches_run:02} ---------")
try:
setup_and_run_experiments(model, method, game, overall_number_of_matches_run)
except Exception as e:
# Print the exception message
print("Caught an exception:", e)
overall_log(log_file_name, e)
continue # just rerun
# evaluate the match result
result = check_if_match_result_was_valid(model, method, overall_number_of_matches_run, game)
# did not complete
if result == "ABNORMAL STATUS":
print("did not finish this match")
overall_log(log_file_name, "ABNORMAL STATUS DID NOT FINISH THIS MATCH")
else:
print("Winner of the Match: ", result[0])
print("gpt_3.5 score", result[1])
print(f"{model} score", result[2])
overall_log(log_file_name, f"Winner of the Match: {result[0]}")
overall_log(log_file_name, f"gpt_3.5 score: {result[1]}")
overall_log(log_file_name, f"{model} score: {result[2]}")
valid_matches_list.append(result)
if overall_number_of_matches_run == num_matches:
completion_rate = len(valid_matches_list) / num_matches
print(f"------ Completed Match #{overall_number_of_matches_run:02} ------")
overall_log(log_file_name, f"------ Completed Match #{overall_number_of_matches_run:02} ------")
overall_number_of_matches_run+=1
gpt_35_score_sum = sum([el[1] for el in valid_matches_list])
opponent_score_sum = sum([el[2] for el in valid_matches_list])
nra_value = NRA_value_calculation(opponent_score_sum,gpt_35_score_sum)
print("FINAL LOG:")
print("NRA Value:" , nra_value)
print("Completion Rate", completion_rate)
overall_log(log_file_name, f"NRA Value: {nra_value}")
overall_log(log_file_name, f"Completion Rate: {completion_rate}")
def check_if_match_result_was_valid(model, method, index, game):
folder = os.listdir(f'experiments_{formatted_time}/{model}_{method}_{index}/{game}')
# get the jsonl file from the folder
jsonl_file = [
file for file in folder if file.endswith('.jsonl')][0]
results = evaluate_single_file(
f'experiments_{formatted_time}/{model}_{method}_{index}/{game}/{jsonl_file}')
if results == "ABNORMAL STATUS":
return "ABNORMAL STATUS"
print(f"Results for {model}_{method}_{index}:", results)
gpt_35_nickame = "PromptAgent_gpt-3.5-turbo-1106"
temp = list(results["scores"].keys())
temp.remove("PromptAgent_gpt-3.5-turbo-1106")
opponent_nickname = temp[0]
winner = results["winner"]
gpt_35_score = results['scores'][gpt_35_nickame]
opponent_score = results['scores'][opponent_nickname]
return (winner, gpt_35_score, opponent_score)
if __name__ == "__main__":
main()