MCPcopy Create free account
hub / github.com/THUDM/AgentTuning / predict_single

Method predict_single

AgentBench.old/src/tasks/card_game/task.py:70–122  ·  view source on GitHub ↗
(self, session: Session, data_item: Dict)

Source from the content-addressed store, hash-verified

68 return ret
69
70 def predict_single(self, session: Session, data_item: Dict) -> Dict:
71 location = {
72 'ai': f'python+{self.root_dir}/AI_SDK/Python/main.py+en+%d+%d+%s+%d',
73 'baseline1': f'python+{self.root_dir}/AI_SDK/Python/basline1.py+%d+%d+%s',
74 'baseline2': f'python+{self.root_dir}/AI_SDK/Python/basline2.py+%d+%d+%s',
75 }
76 # a round consist of 50 subrounds
77 folder = self._random_string(16)
78 total = {}
79
80 stage = data_item["stage"]
81 base = data_item["base"]
82 agent = data_item["agent"]
83 if agent == 0:
84 result_dir = f'{self.cache_dir}/result/{folder}_{stage}_ai_{base}'
85 else:
86 result_dir = f'{self.cache_dir}/result/{folder}_{stage}_{base}_ai'
87 stamp = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(int(time.time())))
88 save_dir = f'{result_dir}/{stamp}'
89 os.makedirs(save_dir)
90
91 cmd = f'python {self.root_dir}/judger/judger.py {self.root_dir}/logic/bin/main %s %s config {save_dir}/replay.json'
92
93 if agent == 0:
94 cmd = cmd % (location["ai"] % (stage, 0, save_dir, self.port), location[base] % (stage, 1, save_dir))
95 else:
96 cmd = cmd % (location[base] % (stage, 1, save_dir), location["ai"] % (stage, 0, save_dir, self.port))
97
98 th = threading.Thread(target=self.server.start, args=(folder, session,))
99 th.start()
100 msg = run_cmd(cmd)[1]
101 time.sleep(1)
102 th.join()
103 current_log = self.server.log[folder]
104
105 if agent == 0:
106 meta = {'ai1': "ai", 'ai2': base}
107 else:
108 meta = {'ai1': base, 'ai2': "ai"}
109
110 if "\"0\" : 0" in msg:
111 meta['winner'] = '1'
112 else:
113 meta['winner'] = '0'
114
115 with open(save_dir + '/meta.json', 'w') as f:
116 f.write(json.dumps(meta))
117 ret = calculate(result_dir=result_dir, agent=agent)
118 total[result_dir] = ret
119
120 self._delete_dir(result_dir)
121
122 return {"meta": total, "game": current_log, "state": meta, "folder": folder}

Callers

nothing calls this directly

Calls 7

_random_stringMethod · 0.95
_delete_dirMethod · 0.95
calculateFunction · 0.85
joinMethod · 0.80
run_cmdFunction · 0.70
startMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected