Spaces:
Sleeping
Sleeping
Subhash Vadaparthi commited on
Commit Β·
a3c97ed
1
Parent(s): ed99f19
fix: run 3 separate task episodes matching openenv.yaml task IDs
Browse files200 unmodified lines2012022032042052062072082092102112125 unmodified lines2182192202212222232242252262272282292302312322 unmodified lines2352362372382392402416 unmodified lines2482492502512522532542 unmodified lines257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295200 unmodified linesreturn f"{a_type}(patient={p},doctor={d},slot={s})"# ββ Main inference loop βββββββββββββββββββββββββββββββββββββββββββββdef run_inference():env = HealthcareEnvironment(seed=42)obs = env.reset()# Convert observation to dict for the LLM promptobs_dict = {"doctor_slots": obs.doctor_slots,"patients": obs.patients,5 unmodified linesrewards: List[float] = []action_history: List[Dict[str, Any]] = []try:# ββ Single [START] ββlog_start(task="healthcare_task", env=BENCHMARK, model=MODEL_NAME)# ββ Agent loop: each [STEP] emitted once, immediately after env.step() ββwhile not env.done and steps_taken < MAX_STEPS:action_dict = get_action_from_llm(obs_dict, action_history)# Step using typed actionaction = HealthcareAction(type=action_dict.get("type", "waitlist"),patient_id=action_dict.get("patient_id", 0),2 unmodified lines)obs = env.step(action)# Update observation for next stepobs_dict = {"doctor_slots": obs.doctor_slots,"patients": obs.patients,6 unmodified linessteps_taken += 1rewards.append(obs.reward)# Emit [STEP] ONCE, immediately after env.step()log_step(step=steps_taken,action=action_to_str(action_dict),2 unmodified lineserror=step_error,)# Track history for LLM contextaction_history.append({"action_str": action_to_str(action_dict),"reward": obs.reward,"error": step_error if step_error != "null" else None,})# ββ Determine success (all 3 tasks required by OpenEnv) ββgrader = TaskGrader(env)def fix_score(s):"""Ensure score is strictly between (0, 1) β never 0.0 or 1.0."""if s <= 0.0:return 0.01elif s >= 1.0:return 0.99return ss1 = fix_score(grader.grade_task_1())s2 = fix_score(grader.grade_task_2())s3 = fix_score(grader.grade_task_3())scores = [s1, s2, s3]success = sum(scores) / len(scores) > 0.5# ββ Single [END] ββlog_end(success=success, steps=steps_taken, rewards=rewards)except Exception:# Fallback to ensure [END] is emitted even on failurelog_end(success=False, steps=steps_taken, rewards=rewards if rewards else [0.0])finally:sys.exit(0)if __name__ == "__main__":200 unmodified lines2012022032042052062072082092102112125 unmodified lines2182192202212222232242252262272282292302 unmodified lines2332342352362372386 unmodified lines2452462472482492502 unmodified lines253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298200 unmodified linesreturn f"{a_type}(patient={p},doctor={d},slot={s})"# ββ Run a single task episode βββββββββββββββββββββββββββββββββββββββdef run_task_episode(task_id: str, seed: int, max_steps: int = 10):"""Run one task episode and return (steps, rewards, score)."""env = HealthcareEnvironment(seed=seed)obs = env.reset()obs_dict = {"doctor_slots": obs.doctor_slots,"patients": obs.patients,5 unmodified linesrewards: List[float] = []action_history: List[Dict[str, Any]] = []# ββ [START] ββlog_start(task=task_id, env=BENCHMARK, model=MODEL_NAME)try:while not env.done and steps_taken < max_steps:action_dict = get_action_from_llm(obs_dict, action_history)action = HealthcareAction(type=action_dict.get("type", "waitlist"),patient_id=action_dict.get("patient_id", 0),2 unmodified lines)obs = env.step(action)obs_dict = {"doctor_slots": obs.doctor_slots,"patients": obs.patients,6 unmodified linessteps_taken += 1rewards.append(obs.reward)log_step(step=steps_taken,action=action_to_str(action_dict),2 unmodified lineserror=step_error,)action_history.append({"action_str": action_to_str(action_dict),"reward": obs.reward,"error": step_error if step_error != "null" else None,})# Grade the specific taskgrader = TaskGrader(env)if task_id == "book_appointment":score = grader.grade_task_1()elif task_id == "scheduling_conflicts":score = grader.grade_task_2()else:score = grader.grade_task_3()# Clamp score strictly to (0, 1)if score <= 0.0:score = 0.01elif score >= 1.0:score = 0.99success = score > 0.5log_end(success=success, steps=steps_taken, rewards=rewards)return steps_taken, rewards, scoreexcept Exception:log_end(success=False, steps=steps_taken, rewards=rewards if rewards else [0.0])return steps_taken, rewards if rewards else [0.0], 0.01# ββ Main inference loop βββββββββββββββββββββββββββββββββββββββββββββdef run_inference():# Task IDs must match the `id` field in openenv.yamltask_ids = ["book_appointment", "scheduling_conflicts", "priority_scheduling"]seeds = [42, 123, 456]for task_id, seed in zip(task_ids, seeds):run_task_episode(task_id, seed=seed, max_steps=10)sys.exit(0)if __name__ == "__main__":
inference.py+36-33