-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenvironment.py
More file actions
269 lines (228 loc) · 9.66 KB
/
environment.py
File metadata and controls
269 lines (228 loc) · 9.66 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
import os
import sys
import json
import math
from loguru import logger
from typing import List, Dict, Any, Union
from datetime import datetime, timedelta
from collections import defaultdict
from tools_parser import ToolManager
from environments.traineebench.schemas.registry import call_evaluator
from virtual_server.registry import create_server
from virtual_server.base_server import BaseServer
class VirtualClock:
def __init__(self, clock_config: Dict):
self.action_costs: Dict[str, int] = clock_config['action_costs']
start_str = clock_config.get("start_datetime")
if start_str:
try:
self.now_dt = datetime.fromisoformat(start_str)
except Exception:
self.now_dt = datetime.now()
else:
self.now_dt = datetime.now()
self.time_scale = clock_config.get("time_scale", 1)
def now_str(self, fmt: str = "%Y-%m-%d %H:%M:%S") -> str:
return self.now_dt.strftime(fmt)
def advance_minutes(self, minutes: float):
"""
Advance simulated clock by minutes, applying scale and ceil to integer minutes.
Rules:
- Apply global time_scale
- Ceil to integer minutes
- Enforce minimum of 1 minute for any positive cost
"""
try:
scaled = minutes * self.time_scale
# ceil to int minutes; allow zero only if base is 0
if scaled > 0:
quantized = max(1, int(math.ceil(scaled)))
else:
quantized = 0
if quantized > 0:
delta = timedelta(minutes=quantized)
self.now_dt = self.now_dt + delta
except Exception:
pass
def advance_tool_call(self, tool_name: str):
tool_cost = self.action_costs.get(tool_name, 1)*self.time_scale
self.advance_minutes(tool_cost)
def setup_logging(level: str = "INFO", log_path: str = ''):
logger.remove()
logger.add(
sys.stdout,
level=level,
format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"{level} | "
"<cyan>{name}</cyan>\n"
"{message}\n",
colorize=True,
backtrace=True,
diagnose=True,
)
if log_path:
try:
logger.add(
log_path,
level=level,
format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}\n{message}\n",
rotation="100 MB",
retention="7 days",
compression="zip",
enqueue=True,
serialize=False,
)
except (ValueError, OSError) as e:
logger.error(f"Failed to configure file logger at path '{log_path}': {e}")
class Environment:
def __init__(
self, task_path: str, log_level: str = 'INFO', log_path: str = ''
) -> None:
self.task_root_path = task_path
self.workspace = os.path.join(task_path, 'workspace')
config_file = os.path.join(task_path, 'config.json')
with open(config_file, 'r', encoding='utf-8') as rf:
config: Dict = json.load(rf)
self.log_path = log_path
setup_logging(log_level, log_path)
self.tasks: List[Dict] = config['tasks']
clock_config = config.get('clock_config', None)
if clock_config:
self.clock = VirtualClock(clock_config)
else:
self.clock = None
agents_config: Dict[str, List[Dict[str, Union[str, Dict]]]] = config['agents']
self.agents_config = agents_config
self.ego_agent_names = [
ac['agent_name'] for ac in agents_config['ego_agents']
]
tools_config: List[Dict] = config['tools']
self.servers: Dict[str, BaseServer] = {}
self.register_tools(tools_config)
self.total_tool_calls: Dict[str, int] = defaultdict(int)
def register_tools(self, tools_config: List[Dict]):
tool_names = []
server_names = []
for tc in tools_config:
tool_names.append(tc.get('name'))
server_names += tc.get('dependency', None)
for sd in server_names:
self.servers[sd] = create_server(
sd,
task_root_path = self.task_root_path,
clock = self.clock,
agents_config = self.agents_config
)
self.tool_manager = ToolManager(self.servers)
self.tool_manager.load_tools(modules=tool_names)
def generate_tasks_prompt(self, agent_name: str) -> str:
system_prompt = ''
for ego_agent in self.agents_config['ego_agents']:
if ego_agent['agent_name'] == agent_name:
system_prompt += ego_agent.get('system_prompt', None)
if system_prompt:
system_prompt += '\n\n'
if self.tasks:
system_prompt += f"Hi, {agent_name.split(' ')[0]} there's some work that needs your help:\n"
for task_id, task in enumerate(self.tasks):
task_name = task.get('task_name', '')
task_description = task.get('task_description')
deadline = task.get('deadline', '')
if deadline:
system_prompt += f"\n## Task {task_id+1}-{task_name}\n\n{task_description}\nYou should finish this work before **{deadline}**.\n\n"
else:
system_prompt += f"## Task {task_id+1}\n{task_description}\n\n"
return system_prompt
def execute_tool_calls(
self, agent_name: str, tool_calls: List[Any]
) -> List[Dict[str, Any]]:
execute_results = []
if tool_calls:
tool_call_info = f'[{agent_name}] Tool Calls:\n\n'
for tc in tool_calls:
try:
tc_args = json.loads(tc.function.arguments)
except Exception as e:
tc_args = None
if tc_args:
try:
tc_result = self.tool_manager.tools[tc.function.name](**tc_args)
except Exception as e:
tc_result = f'[Error] The following error occurred when you called the tool `{tc.function.name}`: {e.__str__()}.'
else:
tc_result = f'[Error] There is a problem with the tool parameters you entered. Please make sure you enter the correct parameters in the correct format.'
tool_call_info += f'ID: {tc.id}\n'
tool_call_info += f'Tool Name: {tc.function.name}()\n'
tool_call_info += f'Arguments: {tc.function.arguments}\n'
if isinstance(tc_result, dict):
tool_call_info += f'Execute Results:\n{json.dumps(tc_result, ensure_ascii=False, indent=4)}\n\n'
else:
tool_call_info += f'Execute Results:\n{tc_result}\n\n'
attach_user_message = None
if isinstance(tc_result, dict) and 'attach_user_message' in tc_result:
attach_user_message = tc_result.get('attach_user_message')
tool_call_result_str = json.dumps({"attach_user_message": True}, ensure_ascii=False)
else:
tool_call_result_str = json.dumps(tc_result, ensure_ascii=False)
execute_results.append(
{
'role': 'tool',
'name': tc.function.name,
'content': tool_call_result_str,
'tool_call_id': tc.id
}
)
if attach_user_message:
execute_results.append(
{
'role': 'user',
'content': attach_user_message
}
)
if self.clock:
self.clock.advance_tool_call(tc.function.name)
self.total_tool_calls[agent_name] += 1
logger.info(tool_call_info)
if self.clock:
time_message = f'[System Time] Current time is {self.clock.now_str()}.'
logger.info(time_message)
execute_results.append(
{
"role": "system",
"content": time_message
}
)
return execute_results
def evaluate(self) -> Dict:
evaluation_results = []
for task in self.tasks:
evaluation_config = task.get('evaluation', None)
if evaluation_config:
func_name, func_args = evaluation_config['name'], evaluation_config['args']
result = call_evaluator(
name=func_name,
task_root_path=self.task_root_path,
workspace_path=self.workspace,
**func_args
)
evaluation_results.append(
{
"task_name": task.get('task_name', ""),
"total_score": result['total_score'],
"full_score": result['full_score'],
"notes": result['notes']
}
)
logger.info(f"Evaluation Reuslt for {task['task_name']}:\n{result}.")
if self.log_path:
logger.info(f"Task has been finished, check {self.log_path} for details.")
else:
logger.info('Task has been finished.')
output = {
"evaluation_results": evaluation_results,
"total_tool_calls": self.total_tool_calls
}
return output
def close(self):
for server in self.servers.values():
server.close()