Pranjal Parashar commited on
Commit
e87d396
·
unverified ·
1 Parent(s): b3660f9
Files changed (2) hide show
  1. baseline.py +1 -0
  2. inference.py +11 -87
baseline.py CHANGED
@@ -136,6 +136,7 @@ def get_model_action(
136
  stream=False,
137
  )
138
  text = (completion.choices[0].message.content or "").strip()
 
139
  return parse_model_action(text)
140
  except Exception as exc:
141
  last_exc = exc
 
136
  stream=False,
137
  )
138
  text = (completion.choices[0].message.content or "").strip()
139
+ debug_log(f"BASELINE:: LLM IS BEING CALLED!")
140
  return parse_model_action(text)
141
  except Exception as exc:
142
  last_exc = exc
inference.py CHANGED
@@ -27,10 +27,9 @@ STDOUT FORMAT
27
  from __future__ import annotations
28
 
29
  import asyncio
30
- import json
31
  import os
32
  import sys
33
- import tempfile
34
  from typing import Any, List, Optional
35
 
36
  from openai import OpenAI
@@ -39,7 +38,8 @@ from baseline import action_is_valid, build_action, fallback_action, fetch_grade
39
  from client import DeveloperControlRoomEnv
40
  from tasks import list_tasks
41
 
42
- DEFAULT_ENV_URL = "https://praanjal-control-room.hf.space"
 
43
  ENV_URL = os.getenv("ENV_URL") or DEFAULT_ENV_URL
44
  API_KEY = os.getenv("API_KEY")
45
 
@@ -48,7 +48,7 @@ MODEL_NAME = os.getenv("MODEL_NAME") or "Qwen/Qwen2.5-72B-Instruct"
48
  TASK_NAME = os.getenv("DEVELOPER_CONTROL_ROOM_TASK")
49
  BENCHMARK = os.getenv("DEVELOPER_CONTROL_ROOM_BENCHMARK", "developer_control_room")
50
  MAX_STEPS = int(os.getenv("DEVELOPER_CONTROL_ROOM_MAX_STEPS", "14"))
51
- DEFAULT_MODEL_STEPS = 2
52
  MAX_MODEL_STEPS = int(
53
  os.getenv("DEVELOPER_CONTROL_ROOM_MAX_MODEL_STEPS", str(DEFAULT_MODEL_STEPS))
54
  )
@@ -60,13 +60,7 @@ SCENARIO_SEED = int(SCENARIO_SEED_RAW) if SCENARIO_SEED_RAW not in (None, "") el
60
  TEMPERATURE = 0.2
61
  MAX_TOKENS = 250
62
  MODEL_TIMEOUT_SECONDS = float(os.getenv("DEVELOPER_CONTROL_ROOM_MODEL_TIMEOUT_SECONDS", "20"))
63
- ENABLE_EPISODE_MEMORY = os.getenv("DEVELOPER_CONTROL_ROOM_ENABLE_EPISODE_MEMORY", "false").lower() == "true"
64
  DEBUG_LOGGING = os.getenv("DEVELOPER_CONTROL_ROOM_DEBUG", "false").lower() == "true"
65
- EPISODE_MEMORY_PATH = os.getenv(
66
- "DEVELOPER_CONTROL_ROOM_EPISODE_MEMORY_PATH",
67
- os.path.join(tempfile.gettempdir(), "developer_control_room_episode_memory.json"),
68
- )
69
- EPISODE_MEMORY_LIMIT = int(os.getenv("DEVELOPER_CONTROL_ROOM_EPISODE_MEMORY_LIMIT", "6"))
70
 
71
 
72
  def log_start(task: str, env: str, model: str) -> None:
@@ -125,70 +119,6 @@ def format_action_str(action_dict: Optional[dict[str, Any]]) -> str:
125
  return f"{action_type}({','.join(parts)})"
126
 
127
 
128
- def load_episode_memory() -> list[dict[str, Any]]:
129
- if not ENABLE_EPISODE_MEMORY or not os.path.exists(EPISODE_MEMORY_PATH):
130
- return []
131
- try:
132
- with open(EPISODE_MEMORY_PATH, "r", encoding="utf-8") as handle:
133
- payload = json.load(handle)
134
- if isinstance(payload, list):
135
- return [item for item in payload if isinstance(item, dict)]
136
- except Exception as exc:
137
- debug_log(f"[DEBUG] episode memory load failed: {exc}")
138
- return []
139
-
140
-
141
- def save_episode_memory(entries: list[dict[str, Any]]) -> None:
142
- if not ENABLE_EPISODE_MEMORY:
143
- return
144
- try:
145
- with open(EPISODE_MEMORY_PATH, "w", encoding="utf-8") as handle:
146
- json.dump(entries[-EPISODE_MEMORY_LIMIT:], handle, ensure_ascii=True, indent=2)
147
- except Exception as exc:
148
- debug_log(f"[DEBUG] episode memory save failed: {exc}")
149
-
150
-
151
- def summarize_episode_memory(entries: list[dict[str, Any]]) -> str:
152
- if not ENABLE_EPISODE_MEMORY or not entries:
153
- return ""
154
- recent = entries[-min(3, len(entries)) :]
155
- lines: list[str] = []
156
- for item in recent:
157
- lines.append(
158
- f"- task={item.get('task_id')} scenario={item.get('scenario_id')} "
159
- f"solved={str(bool(item.get('solved', False))).lower()} "
160
- f"score={float(item.get('score', 0.0)):.2f} takeaway={item.get('takeaway', '')}"
161
- )
162
- return "\n".join(lines)
163
-
164
-
165
- def record_episode_memory(
166
- entries: list[dict[str, Any]],
167
- task_name: str,
168
- scenario_id: str | None,
169
- solved: bool,
170
- score: float,
171
- history: list[str],
172
- ) -> list[dict[str, Any]]:
173
- if not ENABLE_EPISODE_MEMORY:
174
- return entries
175
- takeaway = ""
176
- if solved:
177
- takeaway = "Pattern solved successfully; repeat similar investigation and validation ordering."
178
- elif history:
179
- takeaway = "Recent attempt stayed partial; gather grounding evidence earlier and validate before submitting."
180
- entry = {
181
- "task_id": task_name,
182
- "scenario_id": scenario_id or "unknown",
183
- "solved": bool(solved),
184
- "score": min(0.99, max(0.01, score)),
185
- "takeaway": takeaway,
186
- "actions": history[-4:],
187
- }
188
- updated = entries + [entry]
189
- return updated[-EPISODE_MEMORY_LIMIT:]
190
-
191
-
192
  async def create_env() -> DeveloperControlRoomEnv:
193
  if ENV_URL:
194
  env = DeveloperControlRoomEnv(base_url=ENV_URL)
@@ -200,10 +130,9 @@ async def create_env() -> DeveloperControlRoomEnv:
200
  async def run_task(
201
  client: OpenAI,
202
  task_name: str,
203
- memory_entries: list[dict[str, Any]],
204
  scenario_index: int | None = None,
205
  scenario_seed: int | None = None,
206
- ) -> list[dict[str, Any]]:
207
  env: DeveloperControlRoomEnv | None = None
208
  history: List[str] = []
209
  rewards: List[float] = []
@@ -224,8 +153,6 @@ async def run_task(
224
  result = await env.reset(**reset_kwargs)
225
  observation = result.observation
226
  scenario_id = observation.scenario_id
227
- episode_memory = summarize_episode_memory(memory_entries)
228
-
229
  for step in range(1, MAX_STEPS + 1):
230
  if result.done:
231
  break
@@ -241,7 +168,7 @@ async def run_task(
241
  step,
242
  observation,
243
  history,
244
- episode_memory,
245
  )
246
  if not action_is_valid(action_dict, observation):
247
  action_dict = fallback_action(task_name, observation)
@@ -275,8 +202,10 @@ async def run_task(
275
  success = grader_result["solved"]
276
  except Exception as exc:
277
  debug_log(f"[DEBUG] task run failed: {exc}")
 
278
  success = False
279
  score = 0.0
 
280
 
281
  finally:
282
  try:
@@ -285,12 +214,10 @@ async def run_task(
285
  except Exception as exc:
286
  debug_log(f"[DEBUG] env.close() error (container cleanup): {exc}")
287
  log_end(success=success, steps=steps_taken, score=score, rewards=rewards)
288
- return record_episode_memory(memory_entries, task_name, scenario_id, success, score, history)
289
 
290
 
291
  async def main() -> None:
292
  client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
293
- memory_entries = load_episode_memory()
294
  tasks = list_tasks()
295
  selected_tasks = [task for task in tasks if task["id"] == TASK_NAME] if TASK_NAME else tasks
296
 
@@ -299,28 +226,24 @@ async def main() -> None:
299
  if RUN_ALL_SCENARIOS:
300
  for scenario_index in range(task["scenarios"]):
301
  scenario_seed = None if SCENARIO_SEED is None else SCENARIO_SEED + scenario_index + task_offset
302
- memory_entries = await run_task(
303
  client,
304
  task_name,
305
- memory_entries,
306
  scenario_index=scenario_index,
307
  scenario_seed=scenario_seed,
308
  )
309
- save_episode_memory(memory_entries)
310
  continue
311
 
312
  scenario_seed = None
313
  if SCENARIO_INDEX is None and SCENARIO_SEED is not None:
314
  scenario_seed = SCENARIO_SEED + task_offset
315
 
316
- memory_entries = await run_task(
317
  client,
318
  task_name,
319
- memory_entries,
320
  scenario_index=SCENARIO_INDEX,
321
  scenario_seed=scenario_seed,
322
  )
323
- save_episode_memory(memory_entries)
324
 
325
 
326
  if __name__ == "__main__":
@@ -328,4 +251,5 @@ if __name__ == "__main__":
328
  asyncio.run(main())
329
  except Exception as exc:
330
  debug_log(f"[DEBUG] inference failed: {exc}")
 
331
  raise SystemExit(1)
 
27
  from __future__ import annotations
28
 
29
  import asyncio
 
30
  import os
31
  import sys
32
+ import traceback
33
  from typing import Any, List, Optional
34
 
35
  from openai import OpenAI
 
38
  from client import DeveloperControlRoomEnv
39
  from tasks import list_tasks
40
 
41
+ # DEFAULT_ENV_URL = "https://praanjal-control-room.hf.space"
42
+ DEFAULT_ENV_URL = "http://localhost:7860"
43
  ENV_URL = os.getenv("ENV_URL") or DEFAULT_ENV_URL
44
  API_KEY = os.getenv("API_KEY")
45
 
 
48
  TASK_NAME = os.getenv("DEVELOPER_CONTROL_ROOM_TASK")
49
  BENCHMARK = os.getenv("DEVELOPER_CONTROL_ROOM_BENCHMARK", "developer_control_room")
50
  MAX_STEPS = int(os.getenv("DEVELOPER_CONTROL_ROOM_MAX_STEPS", "14"))
51
+ DEFAULT_MODEL_STEPS = 14
52
  MAX_MODEL_STEPS = int(
53
  os.getenv("DEVELOPER_CONTROL_ROOM_MAX_MODEL_STEPS", str(DEFAULT_MODEL_STEPS))
54
  )
 
60
  TEMPERATURE = 0.2
61
  MAX_TOKENS = 250
62
  MODEL_TIMEOUT_SECONDS = float(os.getenv("DEVELOPER_CONTROL_ROOM_MODEL_TIMEOUT_SECONDS", "20"))
 
63
  DEBUG_LOGGING = os.getenv("DEVELOPER_CONTROL_ROOM_DEBUG", "false").lower() == "true"
 
 
 
 
 
64
 
65
 
66
  def log_start(task: str, env: str, model: str) -> None:
 
119
  return f"{action_type}({','.join(parts)})"
120
 
121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  async def create_env() -> DeveloperControlRoomEnv:
123
  if ENV_URL:
124
  env = DeveloperControlRoomEnv(base_url=ENV_URL)
 
130
  async def run_task(
131
  client: OpenAI,
132
  task_name: str,
 
133
  scenario_index: int | None = None,
134
  scenario_seed: int | None = None,
135
+ ) -> None:
136
  env: DeveloperControlRoomEnv | None = None
137
  history: List[str] = []
138
  rewards: List[float] = []
 
153
  result = await env.reset(**reset_kwargs)
154
  observation = result.observation
155
  scenario_id = observation.scenario_id
 
 
156
  for step in range(1, MAX_STEPS + 1):
157
  if result.done:
158
  break
 
168
  step,
169
  observation,
170
  history,
171
+ "",
172
  )
173
  if not action_is_valid(action_dict, observation):
174
  action_dict = fallback_action(task_name, observation)
 
202
  success = grader_result["solved"]
203
  except Exception as exc:
204
  debug_log(f"[DEBUG] task run failed: {exc}")
205
+ debug_log(traceback.format_exc().rstrip())
206
  success = False
207
  score = 0.0
208
+ raise
209
 
210
  finally:
211
  try:
 
214
  except Exception as exc:
215
  debug_log(f"[DEBUG] env.close() error (container cleanup): {exc}")
216
  log_end(success=success, steps=steps_taken, score=score, rewards=rewards)
 
217
 
218
 
219
  async def main() -> None:
220
  client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
 
221
  tasks = list_tasks()
222
  selected_tasks = [task for task in tasks if task["id"] == TASK_NAME] if TASK_NAME else tasks
223
 
 
226
  if RUN_ALL_SCENARIOS:
227
  for scenario_index in range(task["scenarios"]):
228
  scenario_seed = None if SCENARIO_SEED is None else SCENARIO_SEED + scenario_index + task_offset
229
+ await run_task(
230
  client,
231
  task_name,
 
232
  scenario_index=scenario_index,
233
  scenario_seed=scenario_seed,
234
  )
 
235
  continue
236
 
237
  scenario_seed = None
238
  if SCENARIO_INDEX is None and SCENARIO_SEED is not None:
239
  scenario_seed = SCENARIO_SEED + task_offset
240
 
241
+ await run_task(
242
  client,
243
  task_name,
 
244
  scenario_index=SCENARIO_INDEX,
245
  scenario_seed=scenario_seed,
246
  )
 
247
 
248
 
249
  if __name__ == "__main__":
 
251
  asyncio.run(main())
252
  except Exception as exc:
253
  debug_log(f"[DEBUG] inference failed: {exc}")
254
+ debug_log(traceback.format_exc().rstrip())
255
  raise SystemExit(1)