Spaces:
Sleeping
Sleeping
File size: 12,916 Bytes
caa2c8b | 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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | """
MedCodeRL - Baseline Inference Script
Uses the OpenAI API client to run a model against the MedCodeRL environment.
Reads API credentials from environment variables.
Usage:
export HF_TOKEN="your-key"
export API_BASE_URL="https://api.openai.com/v1"
export MODEL_NAME="gpt-4o-mini"
python inference.py
"""
import json
import os
import re
import sys
import time
from typing import Optional
from openai import OpenAI
# Add project root to path so we can import the environment directly
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from server.my_env_environment import MyEnvironment, _load_task_cases
from models import MedAction
# ----- Configuration -----
API_BASE_URL = os.environ.get("API_BASE_URL", "https://api.openai.com/v1")
MODEL_NAME = os.environ.get("MODEL_NAME", "gpt-4o-mini")
HF_TOKEN = os.environ.get("HF_TOKEN")
if not HF_TOKEN:
print("ERROR: Set HF_TOKEN environment variable (Your Hugging Face / API key).")
sys.exit(1)
# Number of cases to evaluate per difficulty level
CASES_PER_DIFFICULTY = int(os.environ.get("CASES_PER_DIFFICULTY", "5"))
MAX_RETRIES = 2
def create_client() -> OpenAI:
"""Create an OpenAI-compatible client."""
return OpenAI(api_key=HF_TOKEN, base_url=API_BASE_URL)
def extract_json_from_response(text: str) -> Optional[dict]:
"""Extract JSON from an LLM response, handling markdown code blocks."""
text = text.strip()
if text.startswith("{"):
try:
return json.loads(text)
except json.JSONDecodeError:
pass
patterns = [
r"```json\s*\n?(.*?)\n?\s*```",
r"```\s*\n?(.*?)\n?\s*```",
r"\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}",
]
for pattern in patterns:
matches = re.findall(pattern, text, re.DOTALL)
for match in matches:
try:
return json.loads(match)
except json.JSONDecodeError:
continue
return None
SYSTEM_PROMPT = """You are an expert medical coding and billing compliance specialist.
Your role is to:
1. Analyze clinical documentation
2. Assign appropriate ICD-10-CM diagnosis codes
3. Assign appropriate CPT procedure codes
4. Make billing compliance decisions (approve, reject, or flag for review)
5. Identify compliance risk flags
6. Provide clinical reasoning for your decisions
You have deep knowledge of:
- ICD-10-CM coding guidelines and conventions
- CPT coding and modifier usage
- Medicare/Medicaid billing rules
- Medical necessity requirements
- Common compliance violations (upcoding, unbundling, fraudulent billing)
- Clinical documentation integrity
- Prior authorization requirements
Always respond with ONLY a valid JSON object in the exact format requested.
Be precise with your ICD-10 and CPT codes.
Consider the clinical documentation, symptoms, treatments, and insurance type when making decisions.
Flag any compliance concerns in risk_flags."""
def format_observation(obs) -> str:
"""Format an observation into a readable prompt for the LLM."""
parts = [
f"## Medical Coding Case: {obs.case_id}",
f"**Difficulty:** {obs.difficulty}",
f"**Visit Type:** {obs.visit_type}",
f"**Provider Specialty:** {obs.provider_specialty}",
"",
"### Patient Information",
f"- **Age:** {obs.patient_age} | **Sex:** {obs.patient_sex}",
f"- **Insurance:** {obs.insurance_type}",
f"- **Prior Authorization Required:** {'Yes' if obs.prior_auth_required else 'No'}",
f"- **Treatment Cost Tier:** {obs.treatment_cost}",
"",
"### Clinical Note",
obs.clinical_note,
"",
"### Symptoms",
", ".join(obs.symptoms) if obs.symptoms else "None reported",
"",
"### Treatments",
", ".join(obs.treatments) if obs.treatments else "None",
]
if obs.comorbidities:
parts += ["", "### Comorbidities", ", ".join(obs.comorbidities)]
if obs.lab_results:
parts += ["", "### Lab Results", obs.lab_results]
if obs.medications:
parts += ["", "### Current Medications", ", ".join(obs.medications)]
return "\n".join(parts)
ACTION_PROMPT = """
Based on the clinical case above, provide your medical coding and billing compliance assessment.
You MUST respond with a valid JSON object containing exactly these fields:
{
"diagnosis_codes": ["<ICD-10 code(s)>"],
"procedure_codes": ["<CPT code(s) if applicable, or empty list>"],
"decision": "<approve|reject|review>",
"confidence": <0.0 to 1.0>,
"reasoning": "<15-500 character clinical justification>",
"modifier_codes": ["<optional CPT modifiers, or empty list>"],
"risk_flags": ["<compliance risk flags identified, or empty list>"]
}
Guidelines:
- Use standard ICD-10-CM codes (e.g., J06.9 for upper respiratory infection)
- Use standard CPT codes (5 digits, e.g., 99213 for office visit)
- decision: "approve" if coding is appropriate, "reject" if non-compliant, "review" if ambiguous
- confidence: your certainty (0.0 = unsure, 1.0 = certain)
- reasoning: explain WHY you chose these codes and this decision
- risk_flags: compliance risks (e.g., "upcoding_risk", "missing_documentation", "bundling_violation")
IMPORTANT: Respond ONLY with the JSON object, no additional text.
"""
def call_llm(client: OpenAI, obs) -> Optional[dict]:
"""Call the LLM to get a coding decision for a clinical case."""
formatted = format_observation(obs)
for attempt in range(MAX_RETRIES + 1):
try:
response = client.chat.completions.create(
model=MODEL_NAME,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": formatted + "\n\n" + ACTION_PROMPT},
],
temperature=0.1,
max_tokens=800,
)
content = response.choices[0].message.content
if not content:
print(f" [Attempt {attempt+1}] Empty response from LLM")
continue
action = extract_json_from_response(content)
if action is None:
print(f" [Attempt {attempt+1}] Failed to parse JSON from response")
if attempt < MAX_RETRIES:
time.sleep(1)
continue
# Sanitize fields
if "diagnosis_codes" not in action or not isinstance(action["diagnosis_codes"], list):
action["diagnosis_codes"] = [action["diagnosis_codes"]] if isinstance(action.get("diagnosis_codes"), str) else ["R69"]
if "procedure_codes" not in action:
action["procedure_codes"] = []
if isinstance(action.get("procedure_codes"), str):
action["procedure_codes"] = [action["procedure_codes"]]
if "decision" not in action:
action["decision"] = "review"
if "confidence" not in action:
action["confidence"] = 0.5
action["confidence"] = max(0.0, min(1.0, float(action["confidence"])))
if "reasoning" not in action or len(str(action.get("reasoning", ""))) < 15:
action["reasoning"] = "Medical coding assessment based on clinical documentation review and compliance guidelines."
if "modifier_codes" not in action:
action["modifier_codes"] = []
if "risk_flags" not in action:
action["risk_flags"] = []
return action
except Exception as e:
print(f" [Attempt {attempt+1}] API error: {e}")
if attempt < MAX_RETRIES:
time.sleep(2 ** attempt)
return None
def get_fallback_action() -> dict:
"""Return a safe fallback action if LLM fails."""
return {
"diagnosis_codes": ["R69"],
"procedure_codes": ["99213"],
"decision": "review",
"confidence": 0.1,
"reasoning": "Unable to obtain LLM response. Flagging for manual review as a safety measure.",
"modifier_codes": [],
"risk_flags": ["llm_failure"],
}
def run_evaluation():
"""Run the baseline evaluation across all difficulty levels."""
print("=" * 70)
print("MedCodeRL - Baseline Inference Script")
print("=" * 70)
print(f"API Base URL: {API_BASE_URL}")
print(f"Model: {MODEL_NAME}")
print(f"Cases per difficulty: {CASES_PER_DIFFICULTY}")
print("=" * 70)
client = create_client()
env = MyEnvironment()
all_scores = []
results_by_difficulty = {}
for difficulty in ["easy", "medium", "hard"]:
print(f"\n{'─' * 50}")
print(f" Running {difficulty.upper()} tasks")
print(f"{'─' * 50}")
difficulty_scores = []
available = len(env._task_cases.get(difficulty, []))
num_cases = min(CASES_PER_DIFFICULTY, available)
if num_cases == 0:
print(f" No cases available for {difficulty}")
continue
for i in range(num_cases):
# Reset environment for this difficulty
obs = env.reset(task_id=difficulty)
print(f"\n Case {i+1}/{num_cases}: {obs.case_id}")
# Get LLM action
action_dict = call_llm(client, obs)
if action_dict is None:
print(" ⚠ LLM failed, using fallback action")
action_dict = get_fallback_action()
# Build MedAction
med_action = MedAction(
diagnosis_codes=action_dict["diagnosis_codes"],
procedure_codes=action_dict.get("procedure_codes", []),
decision=action_dict["decision"],
confidence=action_dict["confidence"],
reasoning=action_dict["reasoning"],
modifier_codes=action_dict.get("modifier_codes", []),
risk_flags=action_dict.get("risk_flags", []),
)
# Step the environment
try:
result_obs = env.step(med_action)
score = result_obs.reward if result_obs.reward is not None else 0.0
difficulty_scores.append(score)
all_scores.append(score)
print(f" Score: {score:.4f}")
print(f" Decision: {action_dict.get('decision', 'N/A')}")
print(f" Diagnosis: {action_dict.get('diagnosis_codes', [])}")
print(f" Procedure: {action_dict.get('procedure_codes', [])}")
if result_obs.reward_breakdown:
gc = result_obs.reward_breakdown.get("grade_components", {})
if gc:
print(f" Components: diag={gc.get('diagnosis_accuracy', 0):.2f} "
f"proc={gc.get('procedure_accuracy', 0):.2f} "
f"dec={gc.get('decision_accuracy', 0):.2f}")
pens = result_obs.reward_breakdown.get("penalties", {})
if pens:
print(f" Penalties: {list(pens.keys())}")
if result_obs.feedback:
print(f" Feedback: {result_obs.feedback}")
except Exception as e:
print(f" ✗ Step failed: {e}")
difficulty_scores.append(0.0)
all_scores.append(0.0)
# Rate limiting
time.sleep(0.5)
if difficulty_scores:
avg = sum(difficulty_scores) / len(difficulty_scores)
results_by_difficulty[difficulty] = {
"scores": difficulty_scores,
"average": round(avg, 4),
"count": len(difficulty_scores),
}
print(f"\n {difficulty.upper()} Average: {avg:.4f} ({len(difficulty_scores)} cases)")
# Final summary
print(f"\n{'=' * 70}")
print(" FINAL RESULTS")
print(f"{'=' * 70}")
for diff, result in results_by_difficulty.items():
print(f" {diff.upper():>8}: {result['average']:.4f} ({result['count']} cases)")
if all_scores:
overall = sum(all_scores) / len(all_scores)
print(f"\n {'OVERALL':>8}: {overall:.4f} ({len(all_scores)} total cases)")
else:
overall = 0.0
print("\n No scores recorded.")
print(f"{'=' * 70}")
# Write results to file
results_output = {
"model": MODEL_NAME,
"api_base_url": API_BASE_URL,
"cases_per_difficulty": CASES_PER_DIFFICULTY,
"results_by_difficulty": results_by_difficulty,
"overall_score": round(overall, 4),
"total_cases": len(all_scores),
}
with open("baseline_results.json", "w") as f:
json.dump(results_output, f, indent=2)
print(f"\nResults saved to baseline_results.json")
return overall
if __name__ == "__main__":
score = run_evaluation()
sys.exit(0 if score > 0 else 1)
|