Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- README.md +6 -132
- env.py +411 -302
- inference.py +155 -156
README.md
CHANGED
|
@@ -1,135 +1,9 @@
|
|
| 1 |
-
|
| 2 |
-
title: Clinical Trial Patient Screening
|
| 3 |
-
emoji: 🧪
|
| 4 |
-
colorFrom: green
|
| 5 |
-
colorTo: indigo
|
| 6 |
-
sdk: docker
|
| 7 |
-
pinned: false
|
| 8 |
-
app_port: 8000
|
| 9 |
-
tags:
|
| 10 |
-
- openenv
|
| 11 |
-
short_description: RL environment for clinical trial patient screening.
|
| 12 |
-
---
|
| 13 |
-
# Clinical Trial Patient Screening Environment
|
| 14 |
|
| 15 |
-
|
| 16 |
|
| 17 |
-
- `easy`:
|
| 18 |
-
- `medium`: ranking
|
| 19 |
-
- `hard`:
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
## Task Overview
|
| 24 |
-
|
| 25 |
-
### Easy
|
| 26 |
-
EGFR-mutated metastatic NSCLC eligibility check using structured oncology data.
|
| 27 |
-
|
| 28 |
-
### Medium
|
| 29 |
-
Rank 3 HER2-positive metastatic breast cancer candidates by fit for a trial.
|
| 30 |
-
|
| 31 |
-
### Hard
|
| 32 |
-
Detect subtle exclusions from an AML screening note, including protocol deviations such as recent investigational treatment, active infection, QTc prolongation, and CYP3A4 inhibitor exposure.
|
| 33 |
-
|
| 34 |
-
## Reward Design
|
| 35 |
-
|
| 36 |
-
- `+0.20` for each correct clinical data point or valid deviation extracted
|
| 37 |
-
- `+1.00` for a correct final screening decision
|
| 38 |
-
- `-0.50` for hallucinated fields, invalid deviation claims, or destructive actions
|
| 39 |
-
|
| 40 |
-
Each task also produces a deterministic grader score in `[0.0, 1.0]`.
|
| 41 |
-
|
| 42 |
-
## Project Structure
|
| 43 |
-
|
| 44 |
-
```text
|
| 45 |
-
clinical_trial_env/
|
| 46 |
-
├── .env
|
| 47 |
-
├── Dockerfile
|
| 48 |
-
├── README.md
|
| 49 |
-
├── __init__.py
|
| 50 |
-
├── client.py
|
| 51 |
-
├── env.py
|
| 52 |
-
├── inference.py
|
| 53 |
-
├── models.py
|
| 54 |
-
├── openenv.yaml
|
| 55 |
-
├── pyproject.toml
|
| 56 |
-
└── server/
|
| 57 |
-
├── __init__.py
|
| 58 |
-
├── app.py
|
| 59 |
-
└── clinical_trial_env_environment.py
|
| 60 |
-
```
|
| 61 |
-
|
| 62 |
-
## Build And Run
|
| 63 |
-
|
| 64 |
-
Build the container from the project root:
|
| 65 |
-
|
| 66 |
-
```bash
|
| 67 |
-
docker build -t clinical-trial-env:latest .
|
| 68 |
-
```
|
| 69 |
-
|
| 70 |
-
Run the server locally:
|
| 71 |
-
|
| 72 |
-
```bash
|
| 73 |
-
docker run --rm -p 8000:8000 clinical-trial-env:latest
|
| 74 |
-
```
|
| 75 |
-
|
| 76 |
-
Validate the environment:
|
| 77 |
-
|
| 78 |
-
```bash
|
| 79 |
-
openenv validate .
|
| 80 |
-
```
|
| 81 |
-
|
| 82 |
-
## Inference
|
| 83 |
-
|
| 84 |
-
The root [inference.py](/Users/abhishekkanade/Desktop/Hackathon/OpenEnv/clinical_trial_env/inference.py) uses the OpenAI client and emits exactly:
|
| 85 |
-
|
| 86 |
-
- `[START]`
|
| 87 |
-
- `[STEP]`
|
| 88 |
-
- `[END]`
|
| 89 |
-
|
| 90 |
-
Required environment variables:
|
| 91 |
-
|
| 92 |
-
- `HF_TOKEN`
|
| 93 |
-
- `LOCAL_IMAGE_NAME` or `ENV_BASE_URL`
|
| 94 |
-
- `API_BASE_URL` optional, defaults to Hugging Face router
|
| 95 |
-
- `MODEL_NAME` optional
|
| 96 |
-
- `CLINICAL_TRIAL_TASK` with values `easy`, `medium`, or `hard`
|
| 97 |
-
|
| 98 |
-
Example:
|
| 99 |
-
|
| 100 |
-
```bash
|
| 101 |
-
set -a
|
| 102 |
-
source .env
|
| 103 |
-
set +a
|
| 104 |
-
python3 inference.py
|
| 105 |
-
```
|
| 106 |
-
|
| 107 |
-
## Python Usage
|
| 108 |
-
|
| 109 |
-
```python
|
| 110 |
-
import asyncio
|
| 111 |
-
|
| 112 |
-
from clinical_trial_env import ClinicalTrialAction, ClinicalTrialEnv
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
async def main() -> None:
|
| 116 |
-
env = await ClinicalTrialEnv.from_docker_image("clinical-trial-env:latest")
|
| 117 |
-
try:
|
| 118 |
-
result = await env.reset(task_id="easy")
|
| 119 |
-
result = await env.step(
|
| 120 |
-
ClinicalTrialAction(action_type="extract_data", field_name="age", value="56")
|
| 121 |
-
)
|
| 122 |
-
print(result.reward, result.observation.reward_details.grader_score)
|
| 123 |
-
finally:
|
| 124 |
-
await env.close()
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
asyncio.run(main())
|
| 128 |
-
```
|
| 129 |
-
|
| 130 |
-
## Audit Notes
|
| 131 |
-
|
| 132 |
-
- `reset()`, `step(action)`, and OpenEnv `state` access are implemented in [env.py](/Users/abhishekkanade/Desktop/Hackathon/OpenEnv/clinical_trial_env/env.py).
|
| 133 |
-
- Gold answers are not exposed through observation metadata.
|
| 134 |
-
- Graders are deterministic and bounded in `[0.0, 1.0]`.
|
| 135 |
-
- The hard task uses unstructured medical text and clinically realistic exclusion criteria.
|
|
|
|
| 1 |
+
# Clinical Trial Screening OpenEnv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
This project implements a realistic reinforcement-learning environment for clinical trial patient screening with three tasks:
|
| 4 |
|
| 5 |
+
- `easy`: eligibility determination against five binary protocol criteria
|
| 6 |
+
- `medium`: ranking three candidates for an EGFR-mutated NSCLC trial
|
| 7 |
+
- `hard`: detecting exclusions and protocol deviations from unstructured chart text
|
| 8 |
|
| 9 |
+
Use `openenv validate` to validate the environment and `python inference.py` for a local scripted run.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
env.py
CHANGED
|
@@ -1,348 +1,457 @@
|
|
| 1 |
-
"""Core RL
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
import
|
| 6 |
-
from
|
| 7 |
-
from typing import Any, Dict, List,
|
| 8 |
from uuid import uuid4
|
| 9 |
|
| 10 |
-
from openenv.core.env_server.
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
ClinicalTrialState,
|
| 19 |
-
)
|
| 20 |
-
except ImportError:
|
| 21 |
-
from models import (
|
| 22 |
-
ClinicalTrialAction,
|
| 23 |
-
ClinicalTrialObservation,
|
| 24 |
-
ClinicalTrialReward,
|
| 25 |
-
ClinicalTrialState,
|
| 26 |
-
)
|
| 27 |
|
| 28 |
INCREMENTAL_REWARD = 0.20
|
| 29 |
FINAL_REWARD = 1.00
|
| 30 |
HALLUCINATION_PENALTY = -0.50
|
| 31 |
-
TASK_SEQUENCE = ["easy", "medium", "hard"]
|
| 32 |
-
MIN_STRICT_SCORE = 0.01
|
| 33 |
-
MAX_STRICT_SCORE = 0.99
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
def _normalize(value: Optional[str]) -> str:
|
| 37 |
-
return " ".join((value or "").strip().lower().replace("_", " ").split())
|
| 38 |
-
|
| 39 |
|
| 40 |
-
class GroundTruth(BaseModel):
|
| 41 |
-
"""Deterministic grader targets for a scenario."""
|
| 42 |
-
|
| 43 |
-
extracted_fields: Dict[str, str] = Field(default_factory=dict)
|
| 44 |
-
ranking: List[str] = Field(default_factory=list)
|
| 45 |
-
final_decision: str
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
class ScenarioSpec(BaseModel):
|
| 49 |
-
"""Scenario loaded from patient_data.json."""
|
| 50 |
|
|
|
|
|
|
|
| 51 |
task_id: str
|
| 52 |
-
difficulty:
|
| 53 |
title: str
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
class ClinicalTrialEnvironment(
|
| 63 |
-
Environment[ClinicalTrialAction, ClinicalTrialObservation, ClinicalTrialState]
|
| 64 |
-
):
|
| 65 |
-
"""Clinical trial screening environment backed by externalized JSON scenarios."""
|
| 66 |
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
def __init__(self) -> None:
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
self.
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
self.
|
| 79 |
-
self.
|
| 80 |
-
self.
|
| 81 |
-
self._state = ClinicalTrialState(episode_id=str(uuid4()), step_count=0)
|
| 82 |
-
|
| 83 |
-
def reset(
|
| 84 |
-
self,
|
| 85 |
-
seed: Optional[int] = None,
|
| 86 |
-
episode_id: Optional[str] = None,
|
| 87 |
-
task_id: Optional[str] = None,
|
| 88 |
-
**kwargs: Any,
|
| 89 |
-
) -> ClinicalTrialObservation:
|
| 90 |
-
del seed, kwargs
|
| 91 |
-
selected_task_id = task_id or self._next_task_id()
|
| 92 |
-
self._current_scenario = self._scenarios[selected_task_id]
|
| 93 |
-
self._submitted_ranking = []
|
| 94 |
-
self._state = ClinicalTrialState(
|
| 95 |
-
episode_id=episode_id or str(uuid4()),
|
| 96 |
-
step_count=0,
|
| 97 |
-
current_task_id=self._current_scenario.task_id,
|
| 98 |
-
difficulty=self._current_scenario.difficulty,
|
| 99 |
-
title=self._current_scenario.title,
|
| 100 |
-
extracted_fields={},
|
| 101 |
-
identified_deviations=[],
|
| 102 |
-
final_decision=None,
|
| 103 |
-
grading_score=0.5,
|
| 104 |
-
)
|
| 105 |
return self._build_observation(
|
| 106 |
-
|
| 107 |
-
|
| 108 |
)
|
| 109 |
|
| 110 |
-
def step(
|
| 111 |
-
self,
|
| 112 |
-
action: ClinicalTrialAction,
|
| 113 |
-
timeout_s: Optional[float] = None,
|
| 114 |
-
**kwargs: Any,
|
| 115 |
-
) -> ClinicalTrialObservation:
|
| 116 |
-
del timeout_s, kwargs
|
| 117 |
-
if self._current_scenario is None:
|
| 118 |
-
return self.reset()
|
| 119 |
-
|
| 120 |
self._state.step_count += 1
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
elif action.action_type == "flag_deviation":
|
| 128 |
-
self._handle_deviation_flag(action, reward)
|
| 129 |
-
elif action.action_type == "rank_patients":
|
| 130 |
-
self._handle_ranking(action, reward)
|
| 131 |
-
if action.ranking:
|
| 132 |
-
done = True
|
| 133 |
-
terminal_reason = "ranking_submitted"
|
| 134 |
-
elif action.action_type == "submit_decision":
|
| 135 |
-
self._state.final_decision = action.final_decision
|
| 136 |
-
done = True
|
| 137 |
-
terminal_reason = "final_decision_submitted"
|
| 138 |
-
elif action.action_type == "delete_evidence":
|
| 139 |
reward.penalty += HALLUCINATION_PENALTY
|
| 140 |
-
reward.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
else:
|
| 142 |
reward.penalty += HALLUCINATION_PENALTY
|
| 143 |
-
reward.
|
| 144 |
-
|
| 145 |
-
if self._state.step_count >= self._current_scenario.max_steps and not done:
|
| 146 |
-
done = True
|
| 147 |
-
terminal_reason = "max_steps_reached"
|
| 148 |
-
|
| 149 |
-
reward.grader_score = self.grader()
|
| 150 |
-
self._state.grading_score = reward.grader_score
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
reward.notes.append("Correct final screening decision.")
|
| 156 |
-
reward.missing_items = self._missing_items()
|
| 157 |
-
|
| 158 |
-
reward.total_reward = round(
|
| 159 |
-
reward.incremental_reward + reward.final_reward + reward.penalty, 4
|
| 160 |
-
)
|
| 161 |
-
return self._build_observation(
|
| 162 |
-
reward_details=reward,
|
| 163 |
-
done=done,
|
| 164 |
-
terminal_reason=terminal_reason,
|
| 165 |
)
|
|
|
|
| 166 |
|
| 167 |
-
|
| 168 |
-
def state(self) -> ClinicalTrialState:
|
| 169 |
return self._state
|
| 170 |
|
| 171 |
-
def
|
| 172 |
-
|
| 173 |
-
assert self._current_scenario is not None
|
| 174 |
-
components: List[float] = []
|
| 175 |
-
truth = self._current_scenario.ground_truth
|
| 176 |
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
components.append(score)
|
| 198 |
-
|
| 199 |
-
if truth.ranking:
|
| 200 |
-
ranking = self._submitted_ranking
|
| 201 |
-
if ranking and len(ranking) == len(truth.ranking):
|
| 202 |
-
positional_hits = sum(
|
| 203 |
-
1 for actual, expected in zip(ranking, truth.ranking) if actual == expected
|
| 204 |
-
) / len(truth.ranking)
|
| 205 |
-
pairwise_hits = 0
|
| 206 |
-
total_pairs = 0
|
| 207 |
-
for index, higher in enumerate(truth.ranking):
|
| 208 |
-
for lower in truth.ranking[index + 1 :]:
|
| 209 |
-
total_pairs += 1
|
| 210 |
-
if ranking.index(higher) < ranking.index(lower):
|
| 211 |
-
pairwise_hits += 1
|
| 212 |
-
pairwise_score = pairwise_hits / max(total_pairs, 1)
|
| 213 |
-
score = (0.6 * positional_hits) + (0.4 * pairwise_score)
|
| 214 |
-
else:
|
| 215 |
-
score = MIN_STRICT_SCORE # Penalize missing/incorrect ranking
|
| 216 |
-
# Clamp component to ensure it never hits exact 0.0 or 1.0
|
| 217 |
-
score = min(max(score, MIN_STRICT_SCORE), MAX_STRICT_SCORE)
|
| 218 |
-
components.append(score)
|
| 219 |
-
|
| 220 |
-
# Final decision correctness
|
| 221 |
-
final_match = _normalize(self._state.final_decision) == _normalize(truth.final_decision)
|
| 222 |
-
score = MAX_STRICT_SCORE if final_match else MIN_STRICT_SCORE # Already clamped
|
| 223 |
-
components.append(score)
|
| 224 |
-
|
| 225 |
-
if not components:
|
| 226 |
-
return MIN_STRICT_SCORE
|
| 227 |
-
|
| 228 |
-
raw_score = sum(components) / len(components)
|
| 229 |
-
strict_score = min(max(raw_score, MIN_STRICT_SCORE), MAX_STRICT_SCORE)
|
| 230 |
-
return round(strict_score, 4)
|
| 231 |
-
|
| 232 |
-
def _next_task_id(self) -> str:
|
| 233 |
-
self._task_cursor = (self._task_cursor + 1) % len(TASK_SEQUENCE)
|
| 234 |
-
return TASK_SEQUENCE[self._task_cursor]
|
| 235 |
-
|
| 236 |
-
def _handle_extraction(self, action: ClinicalTrialAction, reward: ClinicalTrialReward) -> None:
|
| 237 |
-
assert self._current_scenario is not None
|
| 238 |
-
if not action.field_name or action.value is None:
|
| 239 |
reward.penalty += HALLUCINATION_PENALTY
|
| 240 |
-
reward.
|
| 241 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
reward.notes.append(f"Hallucinated field: {action.field_name}")
|
| 247 |
-
return
|
| 248 |
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
else:
|
| 256 |
reward.penalty += HALLUCINATION_PENALTY
|
| 257 |
-
reward.
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
-
def
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
reward.penalty += HALLUCINATION_PENALTY
|
| 264 |
-
reward.
|
| 265 |
-
return
|
| 266 |
|
| 267 |
-
for
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
self._state.identified_deviations.append(deviation)
|
| 271 |
-
reward.incremental_reward += INCREMENTAL_REWARD
|
| 272 |
-
reward.matched_items.append(deviation)
|
| 273 |
-
reward.notes.append(f"Validated deviation: {deviation}.")
|
| 274 |
-
else:
|
| 275 |
-
reward.penalty += HALLUCINATION_PENALTY
|
| 276 |
-
reward.notes.append(f"Unsupported deviation claim: {deviation}.")
|
| 277 |
-
|
| 278 |
-
def _handle_ranking(self, action: ClinicalTrialAction, reward: ClinicalTrialReward) -> None:
|
| 279 |
-
assert self._current_scenario is not None
|
| 280 |
-
ranking = action.ranking
|
| 281 |
-
valid_patients = [
|
| 282 |
-
patient["patient_id"]
|
| 283 |
-
for patient in self._current_scenario.context.get("patients", [])
|
| 284 |
-
]
|
| 285 |
-
if sorted(ranking) != sorted(valid_patients):
|
| 286 |
reward.penalty += HALLUCINATION_PENALTY
|
| 287 |
-
reward.
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
def _missing_items(self) -> List[str]:
|
| 300 |
-
assert self._current_scenario is not None
|
| 301 |
-
truth = self._current_scenario.ground_truth
|
| 302 |
-
missing_fields = [
|
| 303 |
-
field_name
|
| 304 |
-
for field_name, expected in truth.extracted_fields.items()
|
| 305 |
-
if _normalize(self._state.extracted_fields.get(field_name)) != _normalize(expected)
|
| 306 |
-
]
|
| 307 |
-
missing_fields.extend(
|
| 308 |
-
exclusion
|
| 309 |
-
for exclusion in self._current_scenario.hidden_exclusions
|
| 310 |
-
if exclusion not in self._state.identified_deviations
|
| 311 |
)
|
| 312 |
-
|
| 313 |
-
missing_fields.append("ranking")
|
| 314 |
-
if _normalize(self._state.final_decision) != _normalize(truth.final_decision):
|
| 315 |
-
missing_fields.append("final_decision")
|
| 316 |
-
return missing_fields
|
| 317 |
|
| 318 |
-
def
|
| 319 |
self,
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
reward
|
| 339 |
-
|
| 340 |
-
metadata={"grading_score": self._state.grading_score},
|
| 341 |
-
terminal_reason=terminal_reason,
|
| 342 |
-
)
|
| 343 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
|
| 345 |
-
|
| 346 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
|
| 348 |
-
pass
|
|
|
|
| 1 |
+
"""Core RL logic for clinical trial patient screening."""
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
+
from copy import deepcopy
|
| 6 |
+
from dataclasses import dataclass, field
|
| 7 |
+
from typing import Any, Dict, List, Sequence, Set
|
| 8 |
from uuid import uuid4
|
| 9 |
|
| 10 |
+
from openenv.core.env_server.types import State
|
| 11 |
+
|
| 12 |
+
from models import (
|
| 13 |
+
ClinicalTrialScreeningAction,
|
| 14 |
+
ClinicalTrialScreeningObservation,
|
| 15 |
+
RewardModel,
|
| 16 |
+
TaskDifficulty,
|
| 17 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
INCREMENTAL_REWARD = 0.20
|
| 20 |
FINAL_REWARD = 1.00
|
| 21 |
HALLUCINATION_PENALTY = -0.50
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
@dataclass(frozen=True)
|
| 25 |
+
class ScreeningTask:
|
| 26 |
task_id: str
|
| 27 |
+
difficulty: TaskDifficulty
|
| 28 |
title: str
|
| 29 |
+
brief: str
|
| 30 |
+
prompt: str
|
| 31 |
+
extraction_targets: Dict[str, str]
|
| 32 |
+
expected_decision: str
|
| 33 |
+
trial_metadata: Dict[str, Any]
|
| 34 |
+
ranking_ground_truth: List[str] = field(default_factory=list)
|
| 35 |
+
exclusion_ground_truth: Set[str] = field(default_factory=set)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@dataclass
|
| 39 |
+
class TaskRunState:
|
| 40 |
+
extracted_points: Dict[str, str] = field(default_factory=dict)
|
| 41 |
+
granted_rewards: Set[str] = field(default_factory=set)
|
| 42 |
+
final_submitted: bool = False
|
| 43 |
+
latest_grader_score: float = 0.0
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def _build_tasks() -> List[ScreeningTask]:
|
| 47 |
+
medium_scores = {
|
| 48 |
+
"P-M101": 0.88,
|
| 49 |
+
"P-M102": 0.71,
|
| 50 |
+
"P-M103": 0.54,
|
| 51 |
+
}
|
| 52 |
+
return [
|
| 53 |
+
ScreeningTask(
|
| 54 |
+
task_id="easy_eligibility",
|
| 55 |
+
difficulty=TaskDifficulty.EASY,
|
| 56 |
+
title="Phase II EGFR-Mutated NSCLC Eligibility Check",
|
| 57 |
+
brief="Determine whether the candidate meets five binary enrollment criteria.",
|
| 58 |
+
prompt=(
|
| 59 |
+
"Trial CT-NSCLC-201 enrolls adults with metastatic EGFR exon 19 or L858R "
|
| 60 |
+
"non-small cell lung cancer after first-line osimertinib. Candidate E-001 is "
|
| 61 |
+
"47 years old with biopsy-proven metastatic lung adenocarcinoma, EGFR exon 19 "
|
| 62 |
+
"deletion, ECOG 1, no active brain metastases, and adequate hepatic function. "
|
| 63 |
+
"Binary criteria: age >=18, confirmed metastatic NSCLC, sensitizing EGFR "
|
| 64 |
+
"mutation present, ECOG 0-1, no active CNS disease."
|
| 65 |
+
),
|
| 66 |
+
extraction_targets={
|
| 67 |
+
"age": "47",
|
| 68 |
+
"diagnosis": "metastatic nsclc",
|
| 69 |
+
"biomarker": "egfr exon 19 deletion",
|
| 70 |
+
"ecog": "1",
|
| 71 |
+
"active_cns_disease": "no",
|
| 72 |
+
},
|
| 73 |
+
expected_decision="eligible",
|
| 74 |
+
trial_metadata={
|
| 75 |
+
"trial_id": "CT-NSCLC-201",
|
| 76 |
+
"specialty": "thoracic oncology",
|
| 77 |
+
"binary_criteria": [
|
| 78 |
+
"adult patient",
|
| 79 |
+
"metastatic NSCLC confirmed",
|
| 80 |
+
"sensitizing EGFR mutation",
|
| 81 |
+
"ECOG 0-1",
|
| 82 |
+
"no active CNS disease",
|
| 83 |
+
],
|
| 84 |
+
},
|
| 85 |
+
),
|
| 86 |
+
ScreeningTask(
|
| 87 |
+
task_id="medium_patient_ranking",
|
| 88 |
+
difficulty=TaskDifficulty.MEDIUM,
|
| 89 |
+
title="Rank Patients for TROP2 ADC Expansion Cohort",
|
| 90 |
+
brief="Rank three real-world candidates by protocol fit-score for an EGFR-mutated NSCLC study.",
|
| 91 |
+
prompt=(
|
| 92 |
+
"Trial CT-LUNG-312 is an antibody-drug conjugate study for metastatic EGFR-mutated "
|
| 93 |
+
"NSCLC after progression on osimertinib. Rank candidates by expected screening fit. "
|
| 94 |
+
"P-M101: 56 years, EGFR exon 19 deletion, post-osimertinib only, ECOG 0, stable "
|
| 95 |
+
"treated brain metastases, CrCl 82 mL/min, AST/ALT normal. P-M102: 63 years, EGFR "
|
| 96 |
+
"L858R, post-osimertinib and platinum, ECOG 1, mild AST elevation 1.4x ULN, no "
|
| 97 |
+
"brain metastases, CrCl 68. P-M103: 59 years, exon 20 insertion, ECOG 1, chronic "
|
| 98 |
+
"prednisone 15 mg, recent palliative radiation 5 days ago, CrCl 61. Internal fit "
|
| 99 |
+
f"scores are predetermined as {medium_scores}."
|
| 100 |
+
),
|
| 101 |
+
extraction_targets={
|
| 102 |
+
"P-M101_fit_score": "0.88",
|
| 103 |
+
"P-M102_fit_score": "0.71",
|
| 104 |
+
"P-M103_fit_score": "0.54",
|
| 105 |
+
"best_candidate": "P-M101",
|
| 106 |
+
"lowest_candidate": "P-M103",
|
| 107 |
+
},
|
| 108 |
+
expected_decision="P-M101>P-M102>P-M103",
|
| 109 |
+
ranking_ground_truth=["P-M101", "P-M102", "P-M103"],
|
| 110 |
+
trial_metadata={
|
| 111 |
+
"trial_id": "CT-LUNG-312",
|
| 112 |
+
"specialty": "thoracic oncology",
|
| 113 |
+
"ranking_rule": "higher fit-score ranks earlier",
|
| 114 |
+
"fit_scores": medium_scores,
|
| 115 |
+
},
|
| 116 |
+
),
|
| 117 |
+
ScreeningTask(
|
| 118 |
+
task_id="hard_protocol_deviations",
|
| 119 |
+
difficulty=TaskDifficulty.HARD,
|
| 120 |
+
title="Identify Protocol Deviations from Unstructured Screening Note",
|
| 121 |
+
brief="Extract exclusions and protocol deviations from a realistic unstructured chart note.",
|
| 122 |
+
prompt=(
|
| 123 |
+
"Trial CT-LYMPH-440 is a CD19 bispecific study for relapsed diffuse large B-cell "
|
| 124 |
+
"lymphoma. Exclusions include prednisone >10 mg/day within 7 days, live vaccine "
|
| 125 |
+
"within 30 days, active hepatitis B viremia, ANC <1.0 x10^9/L, and major surgery "
|
| 126 |
+
"within 14 days. Screening note: 'Mr. R is a 68-year-old man with relapsed DLBCL. "
|
| 127 |
+
"He received a shingles live-attenuated vaccine 12 days ago at his PCP visit. He "
|
| 128 |
+
"remains on prednisone 20 mg daily for COPD flare and underwent laparoscopic "
|
| 129 |
+
"cholecystectomy 9 days ago. Labs today: ANC 0.9, HBV DNA undetectable on entecavir, "
|
| 130 |
+
"bilirubin normal. Team asks whether any items trigger screen failure or protocol "
|
| 131 |
+
"deviation before scheduling first dose.'"
|
| 132 |
+
),
|
| 133 |
+
extraction_targets={
|
| 134 |
+
"age": "68",
|
| 135 |
+
"live_vaccine_days": "12",
|
| 136 |
+
"prednisone_mg": "20",
|
| 137 |
+
"surgery_days": "9",
|
| 138 |
+
"anc": "0.9",
|
| 139 |
+
},
|
| 140 |
+
expected_decision="exclude",
|
| 141 |
+
exclusion_ground_truth={
|
| 142 |
+
"live_vaccine_within_30_days",
|
| 143 |
+
"prednisone_over_10mg",
|
| 144 |
+
"major_surgery_within_14_days",
|
| 145 |
+
"anc_below_1.0",
|
| 146 |
+
},
|
| 147 |
+
trial_metadata={
|
| 148 |
+
"trial_id": "CT-LYMPH-440",
|
| 149 |
+
"specialty": "hematologic malignancy",
|
| 150 |
+
"expected_exclusion_schema": [
|
| 151 |
+
"live_vaccine_within_30_days",
|
| 152 |
+
"prednisone_over_10mg",
|
| 153 |
+
"major_surgery_within_14_days",
|
| 154 |
+
"anc_below_1.0",
|
| 155 |
+
"active_hbv_viremia",
|
| 156 |
+
],
|
| 157 |
+
},
|
| 158 |
+
),
|
| 159 |
+
]
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
def _normalize(value: str | None) -> str:
|
| 163 |
+
return "" if value is None else value.strip().lower()
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
class EasyEligibilityGrader:
|
| 167 |
+
@staticmethod
|
| 168 |
+
def grade(task: ScreeningTask, task_state: TaskRunState, decision: str | None = None) -> float:
|
| 169 |
+
extracted = sum(
|
| 170 |
+
1
|
| 171 |
+
for field_name, expected in task.extraction_targets.items()
|
| 172 |
+
if _normalize(task_state.extracted_points.get(field_name)) == _normalize(expected)
|
| 173 |
+
)
|
| 174 |
+
extraction_score = extracted / len(task.extraction_targets)
|
| 175 |
+
decision_score = 1.0 if _normalize(decision) == _normalize(task.expected_decision) else 0.0
|
| 176 |
+
return round((0.5 * extraction_score) + (0.5 * decision_score), 4)
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
class MediumRankingGrader:
|
| 180 |
+
@staticmethod
|
| 181 |
+
def grade(task: ScreeningTask, task_state: TaskRunState, ranking: Sequence[str] | None = None) -> float:
|
| 182 |
+
extracted = sum(
|
| 183 |
+
1
|
| 184 |
+
for field_name, expected in task.extraction_targets.items()
|
| 185 |
+
if _normalize(task_state.extracted_points.get(field_name)) == _normalize(expected)
|
| 186 |
+
)
|
| 187 |
+
extraction_score = extracted / len(task.extraction_targets)
|
| 188 |
+
ranking = list(ranking or [])
|
| 189 |
+
if len(ranking) != len(task.ranking_ground_truth):
|
| 190 |
+
ranking_score = 0.0
|
| 191 |
+
else:
|
| 192 |
+
correct_positions = sum(
|
| 193 |
+
1
|
| 194 |
+
for observed, expected in zip(ranking, task.ranking_ground_truth)
|
| 195 |
+
if observed == expected
|
| 196 |
+
)
|
| 197 |
+
ranking_score = correct_positions / len(task.ranking_ground_truth)
|
| 198 |
+
return round((0.4 * extraction_score) + (0.6 * ranking_score), 4)
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
class HardDeviationGrader:
|
| 202 |
+
@staticmethod
|
| 203 |
+
def grade(
|
| 204 |
+
task: ScreeningTask,
|
| 205 |
+
task_state: TaskRunState,
|
| 206 |
+
exclusions: Sequence[str] | None = None,
|
| 207 |
+
decision: str | None = None,
|
| 208 |
+
) -> float:
|
| 209 |
+
extracted = sum(
|
| 210 |
+
1
|
| 211 |
+
for field_name, expected in task.extraction_targets.items()
|
| 212 |
+
if _normalize(task_state.extracted_points.get(field_name)) == _normalize(expected)
|
| 213 |
+
)
|
| 214 |
+
extraction_score = extracted / len(task.extraction_targets)
|
| 215 |
+
predicted = {_normalize(item) for item in exclusions or [] if item}
|
| 216 |
+
if task.exclusion_ground_truth:
|
| 217 |
+
exclusion_score = len(predicted & task.exclusion_ground_truth) / len(task.exclusion_ground_truth)
|
| 218 |
+
else:
|
| 219 |
+
exclusion_score = 0.0
|
| 220 |
+
decision_score = 1.0 if _normalize(decision) == _normalize(task.expected_decision) else 0.0
|
| 221 |
+
return round((0.3 * extraction_score) + (0.4 * exclusion_score) + (0.3 * decision_score), 4)
|
| 222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
+
class ClinicalTrialScreeningEnv:
|
| 225 |
+
"""Stateful environment covering easy, medium, and hard screening tasks."""
|
| 226 |
|
| 227 |
def __init__(self) -> None:
|
| 228 |
+
self._tasks = _build_tasks()
|
| 229 |
+
self._state = State(episode_id=str(uuid4()), step_count=0)
|
| 230 |
+
self._index = 0
|
| 231 |
+
self._task_runs: Dict[str, TaskRunState] = {}
|
| 232 |
+
self._episode_complete = False
|
| 233 |
+
|
| 234 |
+
def reset(self) -> ClinicalTrialScreeningObservation:
|
| 235 |
+
self._state = State(episode_id=str(uuid4()), step_count=0)
|
| 236 |
+
self._index = 0
|
| 237 |
+
self._episode_complete = False
|
| 238 |
+
self._task_runs = {task.task_id: TaskRunState() for task in self._tasks}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
return self._build_observation(
|
| 240 |
+
reward_model=RewardModel(total=0.0),
|
| 241 |
+
feedback="Episode reset. Start with the easy eligibility assessment.",
|
| 242 |
)
|
| 243 |
|
| 244 |
+
def step(self, action: ClinicalTrialScreeningAction) -> ClinicalTrialScreeningObservation:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
self._state.step_count += 1
|
| 246 |
+
task = self._current_task()
|
| 247 |
+
task_state = self._task_runs[task.task_id]
|
| 248 |
+
reward = RewardModel()
|
| 249 |
+
feedback_parts: List[str] = []
|
| 250 |
+
|
| 251 |
+
if self._episode_complete:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
reward.penalty += HALLUCINATION_PENALTY
|
| 253 |
+
reward.reasons.append("episode_already_complete")
|
| 254 |
+
reward.total = reward.penalty
|
| 255 |
+
return self._build_observation(reward, "Episode already completed. Reset to start a new run.")
|
| 256 |
+
|
| 257 |
+
if action.action_type == "destructive_action":
|
| 258 |
+
reward.penalty += HALLUCINATION_PENALTY
|
| 259 |
+
reward.reasons.append("destructive_action")
|
| 260 |
+
feedback_parts.append("Destructive action blocked in screening workflow.")
|
| 261 |
+
elif action.action_type == "extract_data":
|
| 262 |
+
feedback_parts.append(self._handle_extract_data(task, task_state, action, reward))
|
| 263 |
+
elif action.action_type == "submit_ranking":
|
| 264 |
+
feedback_parts.append(self._handle_submit_ranking(task, task_state, action, reward))
|
| 265 |
+
elif action.action_type == "flag_exclusions":
|
| 266 |
+
feedback_parts.append(self._handle_flag_exclusions(task, task_state, action, reward))
|
| 267 |
+
elif action.action_type == "final_decision":
|
| 268 |
+
feedback_parts.append(self._handle_final_decision(task, task_state, action, reward))
|
| 269 |
else:
|
| 270 |
reward.penalty += HALLUCINATION_PENALTY
|
| 271 |
+
reward.reasons.append("unsupported_action")
|
| 272 |
+
feedback_parts.append("Unsupported action type for this environment.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
|
| 274 |
+
reward.total = round(
|
| 275 |
+
reward.incremental_reward + reward.final_reward + reward.penalty,
|
| 276 |
+
4,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
)
|
| 278 |
+
return self._build_observation(reward, " ".join(part for part in feedback_parts if part))
|
| 279 |
|
| 280 |
+
def state(self) -> State:
|
|
|
|
| 281 |
return self._state
|
| 282 |
|
| 283 |
+
def _current_task(self) -> ScreeningTask:
|
| 284 |
+
return self._tasks[self._index]
|
|
|
|
|
|
|
|
|
|
| 285 |
|
| 286 |
+
def _build_observation(
|
| 287 |
+
self,
|
| 288 |
+
reward_model: RewardModel,
|
| 289 |
+
feedback: str,
|
| 290 |
+
) -> ClinicalTrialScreeningObservation:
|
| 291 |
+
task = self._current_task()
|
| 292 |
+
task_state = self._task_runs.get(task.task_id, TaskRunState())
|
| 293 |
+
missing_targets = [
|
| 294 |
+
field_name
|
| 295 |
+
for field_name in task.extraction_targets
|
| 296 |
+
if field_name not in task_state.granted_rewards
|
| 297 |
+
]
|
| 298 |
+
return ClinicalTrialScreeningObservation(
|
| 299 |
+
task_id=task.task_id,
|
| 300 |
+
difficulty=task.difficulty,
|
| 301 |
+
title=task.title,
|
| 302 |
+
brief=task.brief,
|
| 303 |
+
prompt=task.prompt,
|
| 304 |
+
extracted_points=deepcopy(task_state.extracted_points),
|
| 305 |
+
missing_targets=missing_targets,
|
| 306 |
+
available_actions=[
|
| 307 |
+
"extract_data",
|
| 308 |
+
"submit_ranking" if task.difficulty is TaskDifficulty.MEDIUM else "flag_exclusions",
|
| 309 |
+
"final_decision",
|
| 310 |
+
"destructive_action",
|
| 311 |
+
],
|
| 312 |
+
grader_score=task_state.latest_grader_score,
|
| 313 |
+
reward_breakdown=reward_model,
|
| 314 |
+
feedback=feedback,
|
| 315 |
+
done=self._episode_complete,
|
| 316 |
+
reward=reward_model.total,
|
| 317 |
+
trial_metadata=deepcopy(task.trial_metadata),
|
| 318 |
+
)
|
| 319 |
|
| 320 |
+
def _handle_extract_data(
|
| 321 |
+
self,
|
| 322 |
+
task: ScreeningTask,
|
| 323 |
+
task_state: TaskRunState,
|
| 324 |
+
action: ClinicalTrialScreeningAction,
|
| 325 |
+
reward: RewardModel,
|
| 326 |
+
) -> str:
|
| 327 |
+
field_name = action.field_name or ""
|
| 328 |
+
if field_name not in task.extraction_targets:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
reward.penalty += HALLUCINATION_PENALTY
|
| 330 |
+
reward.reasons.append("hallucinated_field")
|
| 331 |
+
return f"Field '{field_name}' is not part of the protocol data model."
|
| 332 |
+
|
| 333 |
+
expected = task.extraction_targets[field_name]
|
| 334 |
+
observed = _normalize(action.value)
|
| 335 |
+
if observed == _normalize(expected):
|
| 336 |
+
task_state.extracted_points[field_name] = action.value or ""
|
| 337 |
+
if field_name not in task_state.granted_rewards:
|
| 338 |
+
task_state.granted_rewards.add(field_name)
|
| 339 |
+
reward.incremental_reward += INCREMENTAL_REWARD
|
| 340 |
+
reward.reasons.append(f"correct_extraction:{field_name}")
|
| 341 |
+
task_state.latest_grader_score = self._current_grader_score(task, task_state, action)
|
| 342 |
+
return f"Captured {field_name} correctly."
|
| 343 |
|
| 344 |
+
reward.penalty += HALLUCINATION_PENALTY
|
| 345 |
+
reward.reasons.append(f"incorrect_extraction:{field_name}")
|
| 346 |
+
return f"Extracted value for {field_name} does not match the chart."
|
|
|
|
|
|
|
| 347 |
|
| 348 |
+
def _handle_submit_ranking(
|
| 349 |
+
self,
|
| 350 |
+
task: ScreeningTask,
|
| 351 |
+
task_state: TaskRunState,
|
| 352 |
+
action: ClinicalTrialScreeningAction,
|
| 353 |
+
reward: RewardModel,
|
| 354 |
+
) -> str:
|
| 355 |
+
if task.difficulty is not TaskDifficulty.MEDIUM:
|
| 356 |
+
reward.penalty += HALLUCINATION_PENALTY
|
| 357 |
+
reward.reasons.append("ranking_on_non_medium_task")
|
| 358 |
+
return "Ranking is only valid on the medium task."
|
| 359 |
+
|
| 360 |
+
task_state.final_submitted = True
|
| 361 |
+
is_correct = list(action.ranking) == task.ranking_ground_truth
|
| 362 |
+
if is_correct:
|
| 363 |
+
reward.final_reward += FINAL_REWARD
|
| 364 |
+
reward.reasons.append("correct_ranking")
|
| 365 |
else:
|
| 366 |
reward.penalty += HALLUCINATION_PENALTY
|
| 367 |
+
reward.reasons.append("incorrect_ranking")
|
| 368 |
+
task_state.latest_grader_score = MediumRankingGrader.grade(task, task_state, action.ranking)
|
| 369 |
+
self._advance_task()
|
| 370 |
+
return "Ranking accepted." if is_correct else "Ranking accepted but does not match the deterministic fit ordering."
|
| 371 |
|
| 372 |
+
def _handle_flag_exclusions(
|
| 373 |
+
self,
|
| 374 |
+
task: ScreeningTask,
|
| 375 |
+
task_state: TaskRunState,
|
| 376 |
+
action: ClinicalTrialScreeningAction,
|
| 377 |
+
reward: RewardModel,
|
| 378 |
+
) -> str:
|
| 379 |
+
if task.difficulty is not TaskDifficulty.HARD:
|
| 380 |
reward.penalty += HALLUCINATION_PENALTY
|
| 381 |
+
reward.reasons.append("exclusion_flag_on_non_hard_task")
|
| 382 |
+
return "Exclusion flagging is reserved for the hard chart-review task."
|
| 383 |
|
| 384 |
+
predicted = {_normalize(item) for item in action.exclusions if item}
|
| 385 |
+
invalid = predicted - task.exclusion_ground_truth
|
| 386 |
+
if invalid:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
reward.penalty += HALLUCINATION_PENALTY
|
| 388 |
+
reward.reasons.append("hallucinated_exclusion")
|
| 389 |
+
task_state.latest_grader_score = HardDeviationGrader.grade(
|
| 390 |
+
task,
|
| 391 |
+
task_state,
|
| 392 |
+
exclusions=action.exclusions,
|
| 393 |
+
)
|
| 394 |
+
return f"Unsupported exclusion codes submitted: {sorted(invalid)}."
|
| 395 |
+
|
| 396 |
+
task_state.latest_grader_score = HardDeviationGrader.grade(
|
| 397 |
+
task,
|
| 398 |
+
task_state,
|
| 399 |
+
exclusions=action.exclusions,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
)
|
| 401 |
+
return "Exclusion codes recorded."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
|
| 403 |
+
def _handle_final_decision(
|
| 404 |
self,
|
| 405 |
+
task: ScreeningTask,
|
| 406 |
+
task_state: TaskRunState,
|
| 407 |
+
action: ClinicalTrialScreeningAction,
|
| 408 |
+
reward: RewardModel,
|
| 409 |
+
) -> str:
|
| 410 |
+
decision = action.value or ""
|
| 411 |
+
task_state.final_submitted = True
|
| 412 |
+
if task.difficulty is TaskDifficulty.MEDIUM:
|
| 413 |
+
reward.penalty += HALLUCINATION_PENALTY
|
| 414 |
+
reward.reasons.append("decision_instead_of_ranking")
|
| 415 |
+
task_state.latest_grader_score = MediumRankingGrader.grade(task, task_state)
|
| 416 |
+
return "Medium task requires submit_ranking rather than final_decision."
|
| 417 |
+
|
| 418 |
+
is_correct = _normalize(decision) == _normalize(task.expected_decision)
|
| 419 |
+
if is_correct:
|
| 420 |
+
reward.final_reward += FINAL_REWARD
|
| 421 |
+
reward.reasons.append("correct_final_decision")
|
| 422 |
+
else:
|
| 423 |
+
reward.penalty += HALLUCINATION_PENALTY
|
| 424 |
+
reward.reasons.append("incorrect_final_decision")
|
|
|
|
|
|
|
|
|
|
| 425 |
|
| 426 |
+
if task.difficulty is TaskDifficulty.EASY:
|
| 427 |
+
task_state.latest_grader_score = EasyEligibilityGrader.grade(task, task_state, decision=decision)
|
| 428 |
+
else:
|
| 429 |
+
exclusions = sorted(task.exclusion_ground_truth)
|
| 430 |
+
task_state.latest_grader_score = HardDeviationGrader.grade(
|
| 431 |
+
task,
|
| 432 |
+
task_state,
|
| 433 |
+
exclusions=exclusions,
|
| 434 |
+
decision=decision,
|
| 435 |
+
)
|
| 436 |
|
| 437 |
+
self._advance_task()
|
| 438 |
+
return "Final screening decision accepted." if is_correct else "Final decision conflicts with protocol evidence."
|
| 439 |
+
|
| 440 |
+
def _current_grader_score(
|
| 441 |
+
self,
|
| 442 |
+
task: ScreeningTask,
|
| 443 |
+
task_state: TaskRunState,
|
| 444 |
+
action: ClinicalTrialScreeningAction,
|
| 445 |
+
) -> float:
|
| 446 |
+
if task.difficulty is TaskDifficulty.EASY:
|
| 447 |
+
return EasyEligibilityGrader.grade(task, task_state)
|
| 448 |
+
if task.difficulty is TaskDifficulty.MEDIUM:
|
| 449 |
+
return MediumRankingGrader.grade(task, task_state)
|
| 450 |
+
return HardDeviationGrader.grade(task, task_state, exclusions=action.exclusions)
|
| 451 |
+
|
| 452 |
+
def _advance_task(self) -> None:
|
| 453 |
+
if self._index == len(self._tasks) - 1:
|
| 454 |
+
self._episode_complete = True
|
| 455 |
+
return
|
| 456 |
+
self._index += 1
|
| 457 |
|
|
|
inference.py
CHANGED
|
@@ -1,43 +1,32 @@
|
|
| 1 |
-
"""
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import asyncio
|
| 6 |
import json
|
| 7 |
import os
|
| 8 |
-
import
|
| 9 |
-
from typing import
|
| 10 |
|
| 11 |
from openai import OpenAI
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
except ImportError:
|
| 16 |
-
from client import ClinicalTrialEnv
|
| 17 |
-
from models import ClinicalTrialAction
|
| 18 |
|
| 19 |
-
LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME") or os.getenv("IMAGE_NAME")
|
| 20 |
-
API_KEY = os.getenv("HF_TOKEN") or os.getenv("API_KEY")
|
| 21 |
API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
|
| 22 |
MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
|
| 23 |
-
|
| 24 |
-
|
| 25 |
ENV_BASE_URL = os.getenv("ENV_BASE_URL")
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
""
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
action_type, field_name, value, ranking, deviations, final_decision, rationale.
|
| 36 |
-
Use only supported action_type values:
|
| 37 |
-
extract_data, rank_patients, flag_deviation, submit_decision.
|
| 38 |
-
Do not add markdown, commentary, or code fences.
|
| 39 |
-
"""
|
| 40 |
-
).strip()
|
| 41 |
|
| 42 |
|
| 43 |
def log_start(task: str, env: str, model: str) -> None:
|
|
@@ -45,191 +34,201 @@ def log_start(task: str, env: str, model: str) -> None:
|
|
| 45 |
|
| 46 |
|
| 47 |
def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]) -> None:
|
| 48 |
-
|
| 49 |
print(
|
| 50 |
f"[STEP] step={step} action={action} reward={reward:.2f} "
|
| 51 |
-
f"done={str(done).lower()} error={
|
| 52 |
flush=True,
|
| 53 |
)
|
| 54 |
|
| 55 |
|
| 56 |
-
def log_end(success: bool, steps: int, rewards: List[float]) -> None:
|
| 57 |
rewards_str = ",".join(f"{reward:.2f}" for reward in rewards)
|
| 58 |
print(
|
| 59 |
-
f"[END] success={str(success).lower()} steps={steps}
|
|
|
|
| 60 |
flush=True,
|
| 61 |
)
|
| 62 |
|
| 63 |
|
| 64 |
-
def
|
| 65 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
return None
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
{
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
{
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
""
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
def heuristic_action(task_name: str, step: int) -> ClinicalTrialAction:
|
| 89 |
-
heuristics: Dict[Tuple[str, int], ClinicalTrialAction] = {
|
| 90 |
-
("easy", 1): ClinicalTrialAction(action_type="extract_data", field_name="age", value="56"),
|
| 91 |
-
("easy", 2): ClinicalTrialAction(
|
| 92 |
-
action_type="extract_data", field_name="egfr_mutation", value="L858R positive"
|
| 93 |
-
),
|
| 94 |
-
("easy", 3): ClinicalTrialAction(action_type="submit_decision", final_decision="eligible"),
|
| 95 |
-
("medium", 1): ClinicalTrialAction(
|
| 96 |
-
action_type="extract_data", field_name="BC-101_her2_status", value="IHC 3+"
|
| 97 |
-
),
|
| 98 |
-
("medium", 2): ClinicalTrialAction(
|
| 99 |
-
action_type="extract_data", field_name="BC-102_trastuzumab_exposure", value="none"
|
| 100 |
-
),
|
| 101 |
-
("medium", 3): ClinicalTrialAction(
|
| 102 |
-
action_type="rank_patients", ranking=["BC-101", "BC-103", "BC-102"]
|
| 103 |
-
),
|
| 104 |
-
("hard", 1): ClinicalTrialAction(action_type="extract_data", field_name="biomarker", value="FLT3-ITD"),
|
| 105 |
-
("hard", 2): ClinicalTrialAction(
|
| 106 |
-
action_type="flag_deviation",
|
| 107 |
-
deviations=[
|
| 108 |
-
"neutropenic fever",
|
| 109 |
-
"qtc greater than 480 ms",
|
| 110 |
-
"recent strong CYP3A4 inhibitor",
|
| 111 |
-
],
|
| 112 |
-
),
|
| 113 |
-
("hard", 3): ClinicalTrialAction(action_type="submit_decision", final_decision="ineligible"),
|
| 114 |
-
}
|
| 115 |
-
return heuristics.get((task_name, step), ClinicalTrialAction(action_type="submit_decision", final_decision="ineligible"))
|
| 116 |
|
| 117 |
|
| 118 |
-
def
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
|
| 123 |
def get_model_action(
|
| 124 |
client: OpenAI,
|
| 125 |
-
|
| 126 |
-
step: int,
|
| 127 |
-
observation_payload: Dict,
|
| 128 |
history: List[str],
|
| 129 |
-
) ->
|
| 130 |
-
|
| 131 |
try:
|
| 132 |
-
|
| 133 |
model=MODEL_NAME,
|
| 134 |
messages=[
|
| 135 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 136 |
-
{"role": "user", "content":
|
| 137 |
],
|
| 138 |
-
temperature=
|
| 139 |
-
max_tokens=
|
| 140 |
-
stream=False,
|
| 141 |
)
|
| 142 |
-
content = (
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
| 146 |
|
| 147 |
|
| 148 |
-
def
|
| 149 |
-
payload = {
|
| 150 |
-
"action_type": action.action_type,
|
| 151 |
-
"field_name": action.field_name,
|
| 152 |
-
"value": action.value,
|
| 153 |
-
"ranking": action.ranking,
|
| 154 |
-
"deviations": action.deviations,
|
| 155 |
-
"final_decision": action.final_decision,
|
| 156 |
-
}
|
| 157 |
-
return json.dumps(payload, separators=(",", ":"), sort_keys=True)
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
async def create_env() -> ClinicalTrialEnv:
|
| 161 |
if LOCAL_IMAGE_NAME:
|
| 162 |
-
return await
|
| 163 |
if ENV_BASE_URL:
|
| 164 |
-
|
| 165 |
-
await
|
| 166 |
-
|
| 167 |
-
|
| 168 |
|
| 169 |
|
| 170 |
async def main() -> None:
|
| 171 |
-
client = OpenAI(base_url=API_BASE_URL, api_key=
|
| 172 |
-
env
|
| 173 |
rewards: List[float] = []
|
|
|
|
| 174 |
steps_taken = 0
|
| 175 |
-
|
| 176 |
success = False
|
| 177 |
-
history: List[str] = []
|
| 178 |
last_error: Optional[str] = None
|
|
|
|
| 179 |
|
| 180 |
log_start(task=TASK_NAME, env=BENCHMARK, model=MODEL_NAME)
|
| 181 |
|
| 182 |
try:
|
| 183 |
-
|
| 184 |
-
result = await env.reset(task_id=TASK_NAME)
|
| 185 |
-
|
| 186 |
for step in range(1, MAX_STEPS + 1):
|
| 187 |
if result.done:
|
| 188 |
break
|
| 189 |
|
| 190 |
-
action = get_model_action(
|
| 191 |
-
|
| 192 |
-
task_name=TASK_NAME,
|
| 193 |
-
step=step,
|
| 194 |
-
observation_payload=result.observation.model_dump(mode="json"),
|
| 195 |
-
history=history,
|
| 196 |
-
)
|
| 197 |
-
|
| 198 |
-
try:
|
| 199 |
-
result = await env.step(action)
|
| 200 |
-
reward = float(result.reward or 0.0)
|
| 201 |
-
done = bool(result.done)
|
| 202 |
-
last_error = None
|
| 203 |
-
except Exception as exc:
|
| 204 |
-
reward = 0.0
|
| 205 |
-
done = True
|
| 206 |
-
last_error = sanitize_error(str(exc))
|
| 207 |
|
|
|
|
| 208 |
rewards.append(reward)
|
| 209 |
steps_taken = step
|
|
|
|
| 210 |
log_step(
|
| 211 |
step=step,
|
| 212 |
action=format_action(action),
|
| 213 |
reward=reward,
|
| 214 |
-
done=done,
|
| 215 |
-
error=
|
| 216 |
)
|
| 217 |
-
history.append(
|
| 218 |
-
|
| 219 |
-
|
|
|
|
| 220 |
break
|
| 221 |
|
| 222 |
-
if
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
finally:
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
await env.close()
|
| 230 |
-
except Exception as exc:
|
| 231 |
-
last_error = last_error or sanitize_error(str(exc))
|
| 232 |
-
log_end(success=success, steps=steps_taken, rewards=rewards)
|
| 233 |
|
| 234 |
|
| 235 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
"""Benchmark-compatible inference runner for clinical trial screening."""
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import asyncio
|
| 6 |
import json
|
| 7 |
import os
|
| 8 |
+
import re
|
| 9 |
+
from typing import List, Optional
|
| 10 |
|
| 11 |
from openai import OpenAI
|
| 12 |
|
| 13 |
+
from client import ClinicalTrialScreeningEnvClient
|
| 14 |
+
from models import ClinicalTrialScreeningAction, ClinicalTrialScreeningObservation
|
|
|
|
|
|
|
|
|
|
| 15 |
|
|
|
|
|
|
|
| 16 |
API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
|
| 17 |
MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
|
| 18 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 19 |
+
LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME")
|
| 20 |
ENV_BASE_URL = os.getenv("ENV_BASE_URL")
|
| 21 |
+
BENCHMARK = os.getenv("BENCHMARK", "clinical_trial_screening")
|
| 22 |
+
TASK_NAME = os.getenv("TASK_NAME", "clinical_trial_patient_screening")
|
| 23 |
+
MAX_STEPS = int(os.getenv("MAX_STEPS", "19"))
|
| 24 |
+
|
| 25 |
+
SYSTEM_PROMPT = (
|
| 26 |
+
"You are a clinical trial screening agent. "
|
| 27 |
+
"Return one compact JSON object with keys action_type, target_id, field_name, value, "
|
| 28 |
+
"ranking, exclusions, rationale. Use only protocol-supported fields and codes."
|
| 29 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
def log_start(task: str, env: str, model: str) -> None:
|
|
|
|
| 34 |
|
| 35 |
|
| 36 |
def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]) -> None:
|
| 37 |
+
error_value = error if error is not None else "null"
|
| 38 |
print(
|
| 39 |
f"[STEP] step={step} action={action} reward={reward:.2f} "
|
| 40 |
+
f"done={str(done).lower()} error={error_value}",
|
| 41 |
flush=True,
|
| 42 |
)
|
| 43 |
|
| 44 |
|
| 45 |
+
def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
|
| 46 |
rewards_str = ",".join(f"{reward:.2f}" for reward in rewards)
|
| 47 |
print(
|
| 48 |
+
f"[END] success={str(success).lower()} steps={steps} "
|
| 49 |
+
f"score={score:.2f} rewards={rewards_str}",
|
| 50 |
flush=True,
|
| 51 |
)
|
| 52 |
|
| 53 |
|
| 54 |
+
def format_action(action: ClinicalTrialScreeningAction) -> str:
|
| 55 |
+
if action.action_type == "extract_data":
|
| 56 |
+
return f"extract_data({action.field_name}={action.value})"
|
| 57 |
+
if action.action_type == "submit_ranking":
|
| 58 |
+
return f"submit_ranking({'>'.join(action.ranking)})"
|
| 59 |
+
if action.action_type == "flag_exclusions":
|
| 60 |
+
return f"flag_exclusions({','.join(action.exclusions)})"
|
| 61 |
+
if action.action_type == "final_decision":
|
| 62 |
+
return f"final_decision({action.value})"
|
| 63 |
+
return action.action_type
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def safe_error(message: Optional[str]) -> Optional[str]:
|
| 67 |
+
if not message:
|
| 68 |
return None
|
| 69 |
+
return re.sub(r"\s+", " ", message.strip())
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def build_user_prompt(
|
| 73 |
+
observation: ClinicalTrialScreeningObservation,
|
| 74 |
+
history: List[str],
|
| 75 |
+
) -> str:
|
| 76 |
+
recent_history = " | ".join(history[-4:]) if history else "none"
|
| 77 |
+
return (
|
| 78 |
+
f"task_id={observation.task_id}\n"
|
| 79 |
+
f"difficulty={observation.difficulty.value}\n"
|
| 80 |
+
f"title={observation.title}\n"
|
| 81 |
+
f"brief={observation.brief}\n"
|
| 82 |
+
f"prompt={observation.prompt}\n"
|
| 83 |
+
f"missing_targets={observation.missing_targets}\n"
|
| 84 |
+
f"available_actions={observation.available_actions}\n"
|
| 85 |
+
f"trial_metadata={json.dumps(observation.trial_metadata, sort_keys=True)}\n"
|
| 86 |
+
f"history={recent_history}\n"
|
| 87 |
+
"Reply with JSON only."
|
| 88 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
+
def fallback_action(observation: ClinicalTrialScreeningObservation) -> ClinicalTrialScreeningAction:
|
| 92 |
+
task_id = observation.task_id
|
| 93 |
+
extracted = observation.extracted_points
|
| 94 |
+
if task_id == "easy_eligibility":
|
| 95 |
+
for field_name, value in [
|
| 96 |
+
("age", "47"),
|
| 97 |
+
("diagnosis", "metastatic nsclc"),
|
| 98 |
+
("biomarker", "egfr exon 19 deletion"),
|
| 99 |
+
("ecog", "1"),
|
| 100 |
+
("active_cns_disease", "no"),
|
| 101 |
+
]:
|
| 102 |
+
if field_name not in extracted:
|
| 103 |
+
return ClinicalTrialScreeningAction(
|
| 104 |
+
action_type="extract_data",
|
| 105 |
+
field_name=field_name,
|
| 106 |
+
value=value,
|
| 107 |
+
)
|
| 108 |
+
return ClinicalTrialScreeningAction(action_type="final_decision", value="eligible")
|
| 109 |
+
if task_id == "medium_patient_ranking":
|
| 110 |
+
for field_name, value in [
|
| 111 |
+
("P-M101_fit_score", "0.88"),
|
| 112 |
+
("P-M102_fit_score", "0.71"),
|
| 113 |
+
("P-M103_fit_score", "0.54"),
|
| 114 |
+
("best_candidate", "P-M101"),
|
| 115 |
+
("lowest_candidate", "P-M103"),
|
| 116 |
+
]:
|
| 117 |
+
if field_name not in extracted:
|
| 118 |
+
return ClinicalTrialScreeningAction(
|
| 119 |
+
action_type="extract_data",
|
| 120 |
+
field_name=field_name,
|
| 121 |
+
value=value,
|
| 122 |
+
)
|
| 123 |
+
return ClinicalTrialScreeningAction(
|
| 124 |
+
action_type="submit_ranking",
|
| 125 |
+
ranking=["P-M101", "P-M102", "P-M103"],
|
| 126 |
+
)
|
| 127 |
+
for field_name, value in [
|
| 128 |
+
("age", "68"),
|
| 129 |
+
("live_vaccine_days", "12"),
|
| 130 |
+
("prednisone_mg", "20"),
|
| 131 |
+
("surgery_days", "9"),
|
| 132 |
+
("anc", "0.9"),
|
| 133 |
+
]:
|
| 134 |
+
if field_name not in extracted:
|
| 135 |
+
return ClinicalTrialScreeningAction(
|
| 136 |
+
action_type="extract_data",
|
| 137 |
+
field_name=field_name,
|
| 138 |
+
value=value,
|
| 139 |
+
)
|
| 140 |
+
if observation.grader_score < 0.7:
|
| 141 |
+
return ClinicalTrialScreeningAction(
|
| 142 |
+
action_type="flag_exclusions",
|
| 143 |
+
exclusions=[
|
| 144 |
+
"live_vaccine_within_30_days",
|
| 145 |
+
"prednisone_over_10mg",
|
| 146 |
+
"major_surgery_within_14_days",
|
| 147 |
+
"anc_below_1.0",
|
| 148 |
+
],
|
| 149 |
+
)
|
| 150 |
+
return ClinicalTrialScreeningAction(action_type="final_decision", value="exclude")
|
| 151 |
|
| 152 |
|
| 153 |
def get_model_action(
|
| 154 |
client: OpenAI,
|
| 155 |
+
observation: ClinicalTrialScreeningObservation,
|
|
|
|
|
|
|
| 156 |
history: List[str],
|
| 157 |
+
) -> tuple[ClinicalTrialScreeningAction, Optional[str]]:
|
| 158 |
+
prompt = build_user_prompt(observation, history)
|
| 159 |
try:
|
| 160 |
+
response = client.chat.completions.create(
|
| 161 |
model=MODEL_NAME,
|
| 162 |
messages=[
|
| 163 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 164 |
+
{"role": "user", "content": prompt},
|
| 165 |
],
|
| 166 |
+
temperature=0.0,
|
| 167 |
+
max_tokens=200,
|
|
|
|
| 168 |
)
|
| 169 |
+
content = (response.choices[0].message.content or "").strip()
|
| 170 |
+
payload = json.loads(content)
|
| 171 |
+
return ClinicalTrialScreeningAction.model_validate(payload), None
|
| 172 |
+
except Exception as exc:
|
| 173 |
+
return fallback_action(observation), safe_error(str(exc))
|
| 174 |
|
| 175 |
|
| 176 |
+
async def create_env_client() -> ClinicalTrialScreeningEnvClient:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
if LOCAL_IMAGE_NAME:
|
| 178 |
+
return await ClinicalTrialScreeningEnvClient.from_docker_image(LOCAL_IMAGE_NAME)
|
| 179 |
if ENV_BASE_URL:
|
| 180 |
+
client = ClinicalTrialScreeningEnvClient(base_url=ENV_BASE_URL)
|
| 181 |
+
return await client.connect()
|
| 182 |
+
client = ClinicalTrialScreeningEnvClient(base_url="http://localhost:8000")
|
| 183 |
+
return await client.connect()
|
| 184 |
|
| 185 |
|
| 186 |
async def main() -> None:
|
| 187 |
+
client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
|
| 188 |
+
env = await create_env_client()
|
| 189 |
rewards: List[float] = []
|
| 190 |
+
history: List[str] = []
|
| 191 |
steps_taken = 0
|
| 192 |
+
final_score = 0.0
|
| 193 |
success = False
|
|
|
|
| 194 |
last_error: Optional[str] = None
|
| 195 |
+
result = None
|
| 196 |
|
| 197 |
log_start(task=TASK_NAME, env=BENCHMARK, model=MODEL_NAME)
|
| 198 |
|
| 199 |
try:
|
| 200 |
+
result = await env.reset()
|
|
|
|
|
|
|
| 201 |
for step in range(1, MAX_STEPS + 1):
|
| 202 |
if result.done:
|
| 203 |
break
|
| 204 |
|
| 205 |
+
action, planning_error = get_model_action(client, result.observation, history)
|
| 206 |
+
result = await env.step(action)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
+
reward = float(result.reward or 0.0)
|
| 209 |
rewards.append(reward)
|
| 210 |
steps_taken = step
|
| 211 |
+
last_error = planning_error
|
| 212 |
log_step(
|
| 213 |
step=step,
|
| 214 |
action=format_action(action),
|
| 215 |
reward=reward,
|
| 216 |
+
done=bool(result.done),
|
| 217 |
+
error=last_error,
|
| 218 |
)
|
| 219 |
+
history.append(
|
| 220 |
+
f"{result.observation.task_id}:{format_action(action)}:{reward:.2f}:{result.done}"
|
| 221 |
+
)
|
| 222 |
+
if result.done:
|
| 223 |
break
|
| 224 |
|
| 225 |
+
if result is not None:
|
| 226 |
+
final_score = float(result.observation.grader_score)
|
| 227 |
+
final_score = min(max(final_score, 0.0), 1.0)
|
| 228 |
+
success = bool(result.done) and final_score >= 0.99
|
| 229 |
finally:
|
| 230 |
+
await env.close()
|
| 231 |
+
log_end(success=success, steps=steps_taken, score=final_score, rewards=rewards)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
|
| 234 |
if __name__ == "__main__":
|