File size: 4,214 Bytes
b618214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import asyncio
import os
import json
from openai import OpenAI
from my_env_v4 import MyEnvV4Env, MyEnvV4Action

API_KEY = os.getenv("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")
API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-7B-Instruct")

TASK_NAME = "triage"
BENCHMARK = "healthcare_env"

client = OpenAI(api_key=API_KEY, base_url=API_BASE_URL)

# ================= RULE-BASED OVERRIDE =================
HIGH_RISK_KEYWORDS = [
    "chest pain", "breathing", "unconscious",
    "bleeding", "fainting", "severe", "pressure"
]

def rule_override(text):
    t = text.lower()
    for k in HIGH_RISK_KEYWORDS:
        if k in t:
            return MyEnvV4Action(
                action_type="finalize",
                urgency="high",
                department="emergency",
                escalate=True
            )
    return None

# ================= FEW-SHOT PROMPT =================
def build_prompt(obs):
    return f"""
You are a medical triage assistant.

Determine:
- urgency: low / medium / high
- department: general / dermatology / cardiology / emergency
- escalate: true / false

Examples:

Patient: mild headache since morning
Answer:
{{"action_type":"finalize","urgency":"low","department":"general","escalate":false}}

Patient: small rash and itching
Answer:
{{"action_type":"finalize","urgency":"low","department":"dermatology","escalate":false}}

Patient: chest pain with sweating
Answer:
{{"action_type":"finalize","urgency":"high","department":"emergency","escalate":true}}

Patient: occasional chest discomfort
Answer:
{{"action_type":"finalize","urgency":"medium","department":"cardiology","escalate":false}}

Patient: severe breathing problem
Answer:
{{"action_type":"finalize","urgency":"high","department":"emergency","escalate":true}}

Rules:
- High = life-threatening
- Medium = persistent symptoms
- Low = mild symptoms

Now classify:

Patient: {obs.current_query.message}

Return ONLY JSON:
"""

# ================= SAFE PARSER =================
def safe_parse(text):
    try:
        text = text.strip()

        if text.startswith("```"):
            text = text.split("```")[1]

        data = json.loads(text)

        return MyEnvV4Action(
            action_type="finalize",
            urgency=data.get("urgency", "medium"),
            department=data.get("department", "general"),
            escalate=data.get("escalate", False)
        )

    except:
        return MyEnvV4Action(
            action_type="finalize",
            urgency="medium",
            department="general",
            escalate=False
        )

# ================= MAIN =================
async def main():
    env = MyEnvV4Env()
    obs = env.reset()

    rewards = []
    step = 0
    done = False
    last_error = None

    print(f"[START] task={TASK_NAME} env={BENCHMARK} model={MODEL_NAME}")

    while not done and step < 20:
        step += 1

        try:
            # 🔥 RULE-BASED SHORTCUT
            action = rule_override(obs.current_query.message)

            # 🤖 If no rule → call API
            if not action:
                res = client.chat.completions.create(
                    model=MODEL_NAME,
                    messages=[{"role": "user", "content": build_prompt(obs)}],
                    temperature=0.0,
                    max_tokens=120
                )

                action = safe_parse(res.choices[0].message.content)

            obs, reward, done, info = env.step(action)
            last_error = info.get("error", None)

        except Exception as e:
            action = "error"
            reward = 0.0
            done = True
            last_error = str(e)

        rewards.append(reward)

        print(f"[STEP] step={step} action={action} reward={reward:.2f} done={str(done).lower()} error={last_error if last_error else 'null'}")

    env.close()

    score = env.normalized_score()
    success = score >= 0.5

    reward_str = ",".join([f"{r:.2f}" for r in rewards])

    print(f"[END] success={str(success).lower()} steps={step} score={score:.2f} rewards={reward_str}")

# ================= ENTRY =================
if __name__ == "__main__":
    asyncio.run(main())