Spaces:
Sleeping
Sleeping
File size: 8,905 Bytes
2043afa | 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 | """Groq-powered action suggester for PolypharmacyEnv."""
from __future__ import annotations
import json
import os
from typing import Any
from openai import OpenAI
from ..models import PolypharmacyAction, PolypharmacyObservation
DEFAULT_MODEL = "llama-3.1-8b-instant"
FALLBACK_MODELS = [
"llama-3.1-8b-instant",
"llama-3.3-70b-versatile",
"gemma2-9b-it",
]
CRITICAL_DRUG_IDS = {"DRUG_WARFARIN", "DRUG_INSULIN_GLARGINE", "DRUG_DIGOXIN"}
SYSTEM_PROMPT = """You are a clinical medication safety assistant.
Return exactly one JSON object describing the next action.
Allowed output schema:
{
"action_type": "query_ddi" | "propose_intervention" | "finish_review",
"drug_id_1": "optional",
"drug_id_2": "optional",
"target_drug_id": "optional",
"intervention_type": "stop|dose_reduce|substitute|add_monitoring|none",
"proposed_new_drug_id": "optional",
"rationale": "optional"
}
No markdown fences. No extra text.
Do NOT use finish_review early. First, gather evidence with query_ddi and/or
perform at least one meaningful intervention when needed.
"""
def _obs_to_prompt(obs: PolypharmacyObservation) -> str:
meds = ", ".join(m.drug_id for m in obs.current_medications)
conds = ", ".join(obs.conditions)
return (
f"Task: {obs.task_id}\n"
f"Age: {obs.age}, sex: {obs.sex}\n"
f"Conditions: {conds}\n"
f"Medications: {meds}\n"
f"Query budget: {obs.remaining_query_budget}\n"
f"Intervention budget: {obs.remaining_intervention_budget}\n"
f"Step index: {obs.step_index}\n"
"Choose the single safest, most useful next action."
)
def _parse_action(text: str) -> PolypharmacyAction:
raw = text.strip()
if raw.startswith("```"):
raw = raw.split("\n", 1)[-1]
if raw.endswith("```"):
raw = raw.rsplit("```", 1)[0]
raw = raw.strip()
payload: dict[str, Any] = json.loads(raw)
return PolypharmacyAction.model_validate(payload)
def _fallback_query_action(obs: PolypharmacyObservation) -> PolypharmacyAction:
meds = [m.drug_id for m in obs.current_medications]
if len(meds) >= 2 and obs.remaining_query_budget > 0:
return PolypharmacyAction(
action_type="query_ddi",
drug_id_1=meds[0],
drug_id_2=meds[1],
)
return PolypharmacyAction(action_type="finish_review")
def _norm_pair(a: str, b: str) -> tuple[str, str]:
return (a, b) if a < b else (b, a)
def _pick_unseen_query_pair(obs: PolypharmacyObservation) -> tuple[str, str] | None:
meds = [m.drug_id for m in obs.current_medications]
if len(meds) < 2 or obs.remaining_query_budget <= 0:
return None
seen = {
_norm_pair(q.drug_id_1, q.drug_id_2)
for q in obs.interaction_queries
}
# Prioritize pairs containing high-risk drugs.
high_risk = [m.drug_id for m in obs.current_medications if m.is_high_risk_elderly]
ordered = high_risk + [m for m in meds if m not in set(high_risk)]
for i in range(len(ordered)):
for j in range(i + 1, len(ordered)):
p = _norm_pair(ordered[i], ordered[j])
if p not in seen:
return p
return None
def _pick_intervention_target(obs: PolypharmacyObservation) -> str | None:
if obs.remaining_intervention_budget <= 0:
return None
med_set = {m.drug_id for m in obs.current_medications}
# Use latest discovered severe/moderate query as intervention target.
for q in reversed(obs.interaction_queries):
if q.severity in ("severe", "moderate"):
m1 = next((m for m in obs.current_medications if m.drug_id == q.drug_id_1), None)
m2 = next((m for m in obs.current_medications if m.drug_id == q.drug_id_2), None)
candidates = [m for m in (m1, m2) if m is not None]
if not candidates:
continue
# Prefer non-critical risky drugs first.
candidates.sort(
key=lambda m: (
m.drug_id in CRITICAL_DRUG_IDS,
0 if any("avoid" in f for f in m.beers_flags) else 1,
0 if m.is_high_risk_elderly else 1,
)
)
return candidates[0].drug_id
# Fallback: if no severe/moderate discovered, still intervene on obviously
# risky medications (Beers/high-risk flags) when budgets permit.
risky = sorted(
obs.current_medications,
key=lambda m: (
0 if any("avoid" in f for f in m.beers_flags) else 1,
0 if m.is_high_risk_elderly else 1,
1 if m.drug_id in CRITICAL_DRUG_IDS else 0,
),
)
for med in risky:
if any("avoid" in f for f in med.beers_flags) or med.is_high_risk_elderly:
return med.drug_id
return None
def _rule_based_action(obs: PolypharmacyObservation) -> PolypharmacyAction | None:
# If we already discovered significant risk, intervene before more querying.
target = _pick_intervention_target(obs)
if target and (
obs.step_index >= 1
and (
obs.remaining_query_budget <= 2
or len(obs.interaction_queries) >= 4
or any(q.severity in ("severe", "moderate") for q in obs.interaction_queries)
)
):
intervention = "stop"
rationale = "Remove likely contributor to discovered interaction risk"
if target in CRITICAL_DRUG_IDS:
# Avoid blunt stop for critical meds.
intervention = "dose_reduce"
rationale = "Critical medication: prefer dose reduction over abrupt stop"
return PolypharmacyAction(
action_type="propose_intervention",
target_drug_id=target,
intervention_type=intervention,
rationale=rationale,
)
pair = _pick_unseen_query_pair(obs)
if pair:
return PolypharmacyAction(
action_type="query_ddi",
drug_id_1=pair[0],
drug_id_2=pair[1],
)
if obs.remaining_intervention_budget > 0:
# Final fallback before finish: at least one safety action.
target = _pick_intervention_target(obs)
if target:
return PolypharmacyAction(
action_type="propose_intervention",
target_drug_id=target,
intervention_type="dose_reduce"
if target in CRITICAL_DRUG_IDS
else "stop",
rationale="Fallback intervention when query options are exhausted",
)
if obs.step_index >= 3:
return PolypharmacyAction(action_type="finish_review")
return None
def _postprocess_action(
obs: PolypharmacyObservation, action: PolypharmacyAction
) -> PolypharmacyAction:
# First apply deterministic guardrails to avoid repetitive loops.
ruled = _rule_based_action(obs)
if ruled is not None:
return ruled
# Guardrail: prevent useless immediate finish actions.
if action.action_type == "finish_review":
if obs.step_index < 2 and obs.remaining_query_budget > 0:
return _fallback_query_action(obs)
if len(obs.interaction_queries) == 0 and obs.remaining_query_budget > 0:
return _fallback_query_action(obs)
return action
def suggest_action_from_observation(
observation: PolypharmacyObservation,
model_name: str | None = None,
) -> PolypharmacyAction:
"""Use Groq chat completions to suggest a valid action."""
api_key = os.getenv("GROQ_API_KEY", "").strip()
if not api_key:
raise ValueError("GROQ_API_KEY is missing. Add it to your .env file.")
base_url = os.getenv("GROQ_BASE_URL", "https://api.groq.com/openai/v1").strip()
model = (model_name or os.getenv("GROQ_MODEL_NAME", DEFAULT_MODEL)).strip()
client = OpenAI(api_key=api_key, base_url=base_url)
user_prompt = _obs_to_prompt(observation)
tried: list[tuple[str, str]] = []
candidates: list[str] = [model] + [m for m in FALLBACK_MODELS if m != model]
for candidate in candidates:
try:
resp = client.chat.completions.create(
model=candidate,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_prompt},
],
temperature=0.2,
max_tokens=220,
)
generated = (resp.choices[0].message.content or "").strip()
parsed = _parse_action(generated)
return _postprocess_action(observation, parsed)
except Exception as exc:
tried.append((candidate, str(exc)))
tried_txt = " | ".join(f"{m}: {err}" for m, err in tried)
raise ValueError(
"No Groq model worked. Try one of: "
"llama-3.3-70b-versatile, llama-3.1-8b-instant, gemma2-9b-it. "
f"Errors: {tried_txt}"
)
|