File size: 9,519 Bytes
54092f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21d2feb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54092f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21d2feb
 
 
 
 
 
 
54092f4
21d2feb
 
54092f4
21d2feb
54092f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21d2feb
54092f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21d2feb
 
54092f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21d2feb
 
 
54092f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Hackathon baseline runner for Model Release Env."""

from __future__ import annotations

import asyncio
import json
import os
import re
import sys
from typing import Any, Dict, List, Optional


def _sanitize_sys_path() -> None:
    current_tag = f"python{sys.version_info.major}.{sys.version_info.minor}"
    sys.path[:] = [
        entry
        for entry in sys.path
        if entry == "" or "/site-packages" not in entry or current_tag in entry
    ]


_sanitize_sys_path()

from openai import OpenAI

try:
    from model_release_env import ModelReleaseAction, ModelReleaseEnv
except ImportError:
    from client import ModelReleaseEnv
    from models import ModelReleaseAction


API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME", "model-release-env:latest")
ENV_BASE_URL = os.getenv("ENV_BASE_URL")
BENCHMARK = os.getenv("BENCHMARK_NAME", "model_release_env")


def _get_env_int(name: str, default: str) -> int:
    try:
        return int(os.getenv(name, default))
    except ValueError as exc:
        raise SystemExit(f"Invalid integer for {name}: {exc}") from exc


def _get_env_float(name: str, default: str) -> float:
    try:
        return float(os.getenv(name, default))
    except ValueError as exc:
        raise SystemExit(f"Invalid float for {name}: {exc}") from exc


MAX_STEPS = _get_env_int("MODEL_RELEASE_MAX_STEPS", "8")
SUCCESS_THRESHOLD = _get_env_float("MODEL_RELEASE_SUCCESS_THRESHOLD", "0.75")

DEFAULT_TASKS = [
    "card_completion_easy",
    "policy_alignment_medium",
    "launch_gate_hard",
]

HEURISTIC_PLANS: Dict[str, List[Dict[str, str]]] = {
    "card_completion_easy": [
        {"action_type": "inspect", "target": "release_brief"},
        {"action_type": "inspect", "target": "eval_sheet"},
        {"action_type": "inspect", "target": "risk_note"},
        {
            "action_type": "set_field",
            "target": "base_model",
            "value": "Qwen2.5-7B-Instruct",
        },
        {
            "action_type": "set_field",
            "target": "eval_summary",
            "value": "gsm8k=0.78; math500=0.61; aime24=0.18",
        },
        {
            "action_type": "set_field",
            "target": "limitations",
            "value": "Fails on long derivations and is not approved for medical advice.",
        },
        {"action_type": "set_decision", "value": "beta"},
        {"action_type": "submit"},
    ],
    "policy_alignment_medium": [
        {"action_type": "inspect", "target": "license_audit"},
        {"action_type": "inspect", "target": "safety_report"},
        {"action_type": "inspect", "target": "policy_note"},
        {
            "action_type": "set_field",
            "target": "license",
            "value": "apache-2.0",
        },
        {
            "action_type": "set_field",
            "target": "safety_guardrails",
            "value": "prompt_injection_filter; pii_redaction",
        },
        {
            "action_type": "set_field",
            "target": "blocked_use_cases",
            "value": "legal advice; medical advice",
        },
        {"action_type": "set_decision", "value": "beta"},
        {"action_type": "submit"},
    ],
    "launch_gate_hard": [
        {"action_type": "inspect", "target": "regression_report"},
        {"action_type": "inspect", "target": "red_team_note"},
        {"action_type": "inspect", "target": "launch_policy"},
        {
            "action_type": "set_field",
            "target": "known_regressions",
            "value": "latency; throughput",
        },
        {
            "action_type": "set_field",
            "target": "launch_blockers",
            "value": "chain-of-thought extraction bypass",
        },
        {
            "action_type": "set_field",
            "target": "public_summary",
            "value": "release on hold pending latency and safety fixes.",
        },
        {"action_type": "set_decision", "value": "hold"},
        {"action_type": "submit"},
    ],
}


def _task_names() -> List[str]:
    raw = os.getenv("MODEL_RELEASE_TASKS")
    if not raw:
        return list(DEFAULT_TASKS)
    return [item.strip() for item in raw.split(",") if item.strip()]


def _compact_action(action: ModelReleaseAction) -> str:
    value = action.value.replace(" ", "_") if action.value else ""
    if action.action_type == "inspect":
        return f"inspect({action.target})"
    if action.action_type == "set_field":
        return f"set_field({action.target}={value})"
    if action.action_type == "set_decision":
        return f"set_decision({value})"
    return "submit()"


def _stderr(message: str) -> None:
    print(message, file=sys.stderr)


def _redact_message(message: str) -> str:
    redacted = re.sub(r"hf_[A-Za-z0-9]+", "hf_[REDACTED]", message)
    redacted = re.sub(r"sk_[A-Za-z0-9]+", "sk_[REDACTED]", redacted)
    redacted = re.sub(r"https://[^\s:@]+:[^\s@]+@", "https://[REDACTED]@", redacted)
    return redacted


def _llm_client() -> Optional[OpenAI]:
    token = os.getenv("HF_TOKEN")
    if not token:
        return None
    return OpenAI(base_url=API_BASE_URL, api_key=token)


def _extract_json_block(content: str) -> Dict[str, Any]:
    match = re.search(r"\{.*\}", content, re.DOTALL)
    if not match:
        raise ValueError("No JSON object found in model response")
    return json.loads(match.group(0))


def _observation_prompt(observation: Any) -> str:
    payload = {
        "task_name": observation.task_name,
        "goal": observation.goal,
        "document_index": observation.document_index,
        "visible_documents": observation.visible_documents,
        "package_snapshot": observation.package_snapshot,
        "checklist_status": observation.checklist_status,
        "critical_gaps": observation.critical_gaps,
        "available_fields": observation.available_fields,
        "available_decisions": observation.available_decisions,
        "inspected_documents": observation.inspected_documents,
        "remaining_steps": observation.remaining_steps,
        "score": observation.score,
        "last_action_error": observation.last_action_error,
    }
    return json.dumps(payload, indent=2, sort_keys=True)


def _heuristic_action(task_name: str, step_index: int) -> ModelReleaseAction:
    plan = HEURISTIC_PLANS[task_name]
    if step_index >= len(plan):
        return ModelReleaseAction(action_type="submit")
    return ModelReleaseAction(**plan[step_index])


def _model_action(
    client: OpenAI,
    task_name: str,
    observation: Any,
) -> ModelReleaseAction:
    system_prompt = (
        "You are operating an OpenEnv release-readiness environment. "
        "Return exactly one JSON object with keys action_type, target, and value. "
        "Allowed action_type values: inspect, set_field, set_decision, submit. "
        "Use inspect before editing. Keep values compact and deterministic."
    )
    user_prompt = (
        f"Task: {task_name}\n"
        "Choose the single best next action given the observation below.\n"
        "Observation JSON:\n"
        f"{_observation_prompt(observation)}"
    )
    response = client.chat.completions.create(
        model=MODEL_NAME,
        temperature=0.0,
        max_tokens=220,
        messages=[
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_prompt},
        ],
    )
    if not response.choices:
        raise ValueError("Model returned no choices")
    content = response.choices[0].message.content or ""
    return ModelReleaseAction(**_extract_json_block(content))


async def _create_env() -> ModelReleaseEnv:
    if ENV_BASE_URL:
        return ModelReleaseEnv(base_url=ENV_BASE_URL)
    return await ModelReleaseEnv.from_docker_image(LOCAL_IMAGE_NAME)


async def _run_task(env: ModelReleaseEnv, task_name: str, llm: Optional[OpenAI]) -> float:
    print(f"[START] benchmark={BENCHMARK} task={task_name}")
    result = await env.reset(task_name=task_name)
    step_index = 0

    while not result.done and step_index < MAX_STEPS:
        try:
            if llm is None:
                action = _heuristic_action(task_name, step_index)
            else:
                action = _model_action(llm, task_name, result.observation)
        except Exception as exc:
            _stderr(
                f"planner fallback for {task_name}: {_redact_message(str(exc))}"
            )
            action = _heuristic_action(task_name, step_index)

        result = await env.step(action)
        error = result.observation.last_action_error or "null"
        reward = 0.0 if result.reward is None else float(result.reward)
        print(
            f"[STEP] action={_compact_action(action)} reward={reward:.2f} "
            f"done={str(result.done)} error={error}"
        )
        step_index += 1

    score = float(result.observation.score)
    success = score >= SUCCESS_THRESHOLD
    print(f"[END] success={str(success)} score={score:.2f}")
    return score


async def main() -> int:
    llm = _llm_client()
    env = await _create_env()
    scores: List[float] = []

    async with env:
        for task_name in _task_names():
            scores.append(await _run_task(env, task_name, llm))

    average_score = sum(scores) / len(scores) if scores else 0.0
    _stderr(f"average_score={average_score:.2f}")
    return 0


if __name__ == "__main__":
    raise SystemExit(asyncio.run(main()))