Spaces:
Sleeping
Sleeping
Commit ·
7b9f847
1
Parent(s): 7c81a00
Harden phase-2 runtime and improve deterministic grading coverage
Browse files- app/__pycache__/tasks.cpython-313.pyc +0 -0
- app/tasks.py +11 -11
- inference.py +112 -62
app/__pycache__/tasks.cpython-313.pyc
DELETED
|
Binary file (2.64 kB)
|
|
|
app/tasks.py
CHANGED
|
@@ -16,9 +16,9 @@ TASKS: dict[str, TaskDefinition] = {
|
|
| 16 |
"The response should mention the specific line-level issue and corrected syntax."
|
| 17 |
),
|
| 18 |
criteria=[
|
| 19 |
-
["syntax error", "invalid syntax"],
|
| 20 |
-
["missing colon", "colon after function definition", "def line needs colon"],
|
| 21 |
-
["def add_numbers(a, b):", "add a colon at end of function definition"],
|
| 22 |
],
|
| 23 |
),
|
| 24 |
"medium": TaskDefinition(
|
|
@@ -38,9 +38,9 @@ TASKS: dict[str, TaskDefinition] = {
|
|
| 38 |
"Mention at least one concrete optimization strategy and why it helps."
|
| 39 |
),
|
| 40 |
criteria=[
|
| 41 |
-
["list comprehension", "comprehension"],
|
| 42 |
-
["same behavior", "preserve behavior", "without changing output"],
|
| 43 |
-
["readability", "cleaner", "more concise"],
|
| 44 |
],
|
| 45 |
),
|
| 46 |
"hard": TaskDefinition(
|
|
@@ -64,11 +64,11 @@ TASKS: dict[str, TaskDefinition] = {
|
|
| 64 |
"Include an explicit overall score in the review."
|
| 65 |
),
|
| 66 |
criteria=[
|
| 67 |
-
["division by zero", "empty list", "len(values) == 0"],
|
| 68 |
-
["broad exception", "except Exception", "too broad catch"],
|
| 69 |
-
["iterate directly", "avoid range(len", "pythonic loop"],
|
| 70 |
-
["type hints", "docstring", "tests"],
|
| 71 |
-
["overall score", "score", "rating"],
|
| 72 |
],
|
| 73 |
),
|
| 74 |
}
|
|
|
|
| 16 |
"The response should mention the specific line-level issue and corrected syntax."
|
| 17 |
),
|
| 18 |
criteria=[
|
| 19 |
+
["syntax error", "invalid syntax", "parse error"],
|
| 20 |
+
["missing colon", "colon after function definition", "def line needs colon", "function header missing :"],
|
| 21 |
+
["def add_numbers(a, b):", "add a colon at end of function definition", "append colon to def add_numbers(a, b)"],
|
| 22 |
],
|
| 23 |
),
|
| 24 |
"medium": TaskDefinition(
|
|
|
|
| 38 |
"Mention at least one concrete optimization strategy and why it helps."
|
| 39 |
),
|
| 40 |
criteria=[
|
| 41 |
+
["list comprehension", "comprehension", "single expression"],
|
| 42 |
+
["same behavior", "preserve behavior", "without changing output", "no behavior change", "identical output"],
|
| 43 |
+
["readability", "cleaner", "more concise", "more readable", "clearer"],
|
| 44 |
],
|
| 45 |
),
|
| 46 |
"hard": TaskDefinition(
|
|
|
|
| 64 |
"Include an explicit overall score in the review."
|
| 65 |
),
|
| 66 |
criteria=[
|
| 67 |
+
["division by zero", "empty list", "len(values) == 0", "guard against empty values"],
|
| 68 |
+
["broad exception", "except Exception", "too broad catch", "catching all exceptions"],
|
| 69 |
+
["iterate directly", "avoid range(len", "pythonic loop", "loop over values directly"],
|
| 70 |
+
["type hints", "docstring", "tests", "unit tests", "annotations"],
|
| 71 |
+
["overall score", "score", "rating", "final score", "overall rating"],
|
| 72 |
],
|
| 73 |
),
|
| 74 |
}
|
inference.py
CHANGED
|
@@ -15,6 +15,8 @@ except Exception: # pragma: no cover
|
|
| 15 |
TestClient = None
|
| 16 |
|
| 17 |
TASK_IDS: list[str] = ["easy", "medium", "hard"]
|
|
|
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
@dataclass
|
|
@@ -61,6 +63,11 @@ def _required_env(name: str) -> str:
|
|
| 61 |
return value
|
| 62 |
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
def _build_prompt(task_id: str, instructions: str, code: str) -> str:
|
| 65 |
return (
|
| 66 |
"You are a code reviewer. Complete exactly the requested review task.\n"
|
|
@@ -68,7 +75,8 @@ def _build_prompt(task_id: str, instructions: str, code: str) -> str:
|
|
| 68 |
f"Instructions: {instructions}\n"
|
| 69 |
"Code:\n"
|
| 70 |
f"{code}\n\n"
|
| 71 |
-
"Return concise, actionable review feedback."
|
|
|
|
| 72 |
)
|
| 73 |
|
| 74 |
|
|
@@ -89,51 +97,76 @@ def _dry_run_review(task_id: str) -> str:
|
|
| 89 |
)
|
| 90 |
|
| 91 |
|
| 92 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
t0 = time.perf_counter()
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
json={"task_id": task_id, "seed": 42},
|
| 102 |
-
timeout=30,
|
| 103 |
-
)
|
| 104 |
reset_response.raise_for_status()
|
| 105 |
observation = reset_response.json()["observation"]
|
| 106 |
|
| 107 |
if dry_run:
|
| 108 |
review = _dry_run_review(task_id)
|
| 109 |
else:
|
|
|
|
|
|
|
| 110 |
prompt = _build_prompt(
|
| 111 |
task_id=observation["task_id"],
|
| 112 |
instructions=observation["instructions"],
|
| 113 |
code=observation["code"],
|
| 114 |
)
|
| 115 |
-
completion =
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
review = completion.choices[0].message.content or ""
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
timeout=30,
|
| 136 |
-
)
|
| 137 |
step_response.raise_for_status()
|
| 138 |
reward = step_response.json()["reward"]
|
| 139 |
|
|
@@ -147,12 +180,18 @@ def _run_task(env_base_url: str, client: OpenAI, model_name: str, task_id: str,
|
|
| 147 |
|
| 148 |
def main() -> None:
|
| 149 |
start_ts = int(time.time())
|
| 150 |
-
env_base_url = os.getenv("ENV_BASE_URL", "http://localhost:7860")
|
| 151 |
-
api_base_url = _required_env("API_BASE_URL")
|
| 152 |
-
model_name = _required_env("MODEL_NAME")
|
| 153 |
-
hf_token = _required_env("HF_TOKEN")
|
| 154 |
dry_run = os.getenv("DRY_RUN", "0") == "1"
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
_emit(
|
| 157 |
"START",
|
| 158 |
{
|
|
@@ -163,41 +202,52 @@ def main() -> None:
|
|
| 163 |
"model_name": model_name,
|
| 164 |
"task_count": len(TASK_IDS),
|
| 165 |
"dry_run": dry_run,
|
|
|
|
| 166 |
},
|
| 167 |
)
|
| 168 |
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
-
results: list[TaskRun] = []
|
| 172 |
-
for index, task_id in enumerate(TASK_IDS, start=1):
|
| 173 |
-
result = _run_task(env_base_url, client, model_name, task_id, dry_run)
|
| 174 |
-
results.append(result)
|
| 175 |
_emit(
|
| 176 |
-
"
|
| 177 |
{
|
| 178 |
-
"event": "
|
| 179 |
-
"
|
| 180 |
-
"
|
| 181 |
-
"
|
| 182 |
-
"
|
| 183 |
-
"
|
| 184 |
},
|
| 185 |
)
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
"tasks": [{"task_id": r.task_id, "score": r.score} for r in results],
|
| 198 |
-
"status": "ok",
|
| 199 |
-
},
|
| 200 |
-
)
|
| 201 |
|
| 202 |
|
| 203 |
if __name__ == "__main__":
|
|
|
|
| 15 |
TestClient = None
|
| 16 |
|
| 17 |
TASK_IDS: list[str] = ["easy", "medium", "hard"]
|
| 18 |
+
REQUEST_TIMEOUT_S = 30
|
| 19 |
+
MAX_ATTEMPTS = 3
|
| 20 |
|
| 21 |
|
| 22 |
@dataclass
|
|
|
|
| 63 |
return value
|
| 64 |
|
| 65 |
|
| 66 |
+
def _optional_env(name: str, default: str) -> str:
|
| 67 |
+
value = os.getenv(name)
|
| 68 |
+
return value if value else default
|
| 69 |
+
|
| 70 |
+
|
| 71 |
def _build_prompt(task_id: str, instructions: str, code: str) -> str:
|
| 72 |
return (
|
| 73 |
"You are a code reviewer. Complete exactly the requested review task.\n"
|
|
|
|
| 75 |
f"Instructions: {instructions}\n"
|
| 76 |
"Code:\n"
|
| 77 |
f"{code}\n\n"
|
| 78 |
+
"Return concise, actionable review feedback. "
|
| 79 |
+
"Call out concrete issues, suggest a correction, and for hard tasks include an explicit overall score."
|
| 80 |
)
|
| 81 |
|
| 82 |
|
|
|
|
| 97 |
)
|
| 98 |
|
| 99 |
|
| 100 |
+
def _post_with_retries(env_base_url: str, session: _InProcessSession | None, path: str, payload: dict[str, Any]) -> _TestResponse | requests.Response:
|
| 101 |
+
last_error: Exception | None = None
|
| 102 |
+
for attempt in range(1, MAX_ATTEMPTS + 1):
|
| 103 |
+
try:
|
| 104 |
+
if env_base_url == "inprocess":
|
| 105 |
+
if session is None:
|
| 106 |
+
raise RuntimeError("In-process session unavailable")
|
| 107 |
+
return session.post(path, payload)
|
| 108 |
+
return requests.post(f"{env_base_url}{path}", json=payload, timeout=REQUEST_TIMEOUT_S)
|
| 109 |
+
except Exception as exc:
|
| 110 |
+
last_error = exc
|
| 111 |
+
if attempt < MAX_ATTEMPTS:
|
| 112 |
+
time.sleep(0.4 * attempt)
|
| 113 |
+
raise RuntimeError(f"Failed POST {path} after {MAX_ATTEMPTS} attempts: {last_error}")
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def _run_task(env_base_url: str, client: OpenAI | None, model_name: str, task_id: str, dry_run: bool) -> TaskRun:
|
| 117 |
t0 = time.perf_counter()
|
| 118 |
+
session = _InProcessSession() if env_base_url == "inprocess" else None
|
| 119 |
|
| 120 |
+
reset_response = _post_with_retries(
|
| 121 |
+
env_base_url=env_base_url,
|
| 122 |
+
session=session,
|
| 123 |
+
path="/reset",
|
| 124 |
+
payload={"task_id": task_id, "seed": 42},
|
| 125 |
+
)
|
|
|
|
|
|
|
|
|
|
| 126 |
reset_response.raise_for_status()
|
| 127 |
observation = reset_response.json()["observation"]
|
| 128 |
|
| 129 |
if dry_run:
|
| 130 |
review = _dry_run_review(task_id)
|
| 131 |
else:
|
| 132 |
+
if client is None:
|
| 133 |
+
raise RuntimeError("OpenAI client is not configured for non-dry-run execution")
|
| 134 |
prompt = _build_prompt(
|
| 135 |
task_id=observation["task_id"],
|
| 136 |
instructions=observation["instructions"],
|
| 137 |
code=observation["code"],
|
| 138 |
)
|
| 139 |
+
completion = None
|
| 140 |
+
last_error: Exception | None = None
|
| 141 |
+
for attempt in range(1, MAX_ATTEMPTS + 1):
|
| 142 |
+
try:
|
| 143 |
+
completion = client.chat.completions.create(
|
| 144 |
+
model=model_name,
|
| 145 |
+
temperature=0,
|
| 146 |
+
seed=42,
|
| 147 |
+
messages=[
|
| 148 |
+
{
|
| 149 |
+
"role": "system",
|
| 150 |
+
"content": "You are an expert software engineer specializing in code review.",
|
| 151 |
+
},
|
| 152 |
+
{"role": "user", "content": prompt},
|
| 153 |
+
],
|
| 154 |
+
)
|
| 155 |
+
break
|
| 156 |
+
except Exception as exc:
|
| 157 |
+
last_error = exc
|
| 158 |
+
if attempt < MAX_ATTEMPTS:
|
| 159 |
+
time.sleep(0.5 * attempt)
|
| 160 |
+
if completion is None:
|
| 161 |
+
raise RuntimeError(f"LLM completion failed after {MAX_ATTEMPTS} attempts: {last_error}")
|
| 162 |
review = completion.choices[0].message.content or ""
|
| 163 |
|
| 164 |
+
step_response = _post_with_retries(
|
| 165 |
+
env_base_url=env_base_url,
|
| 166 |
+
session=session,
|
| 167 |
+
path="/step",
|
| 168 |
+
payload={"action": {"review": review}},
|
| 169 |
+
)
|
|
|
|
|
|
|
| 170 |
step_response.raise_for_status()
|
| 171 |
reward = step_response.json()["reward"]
|
| 172 |
|
|
|
|
| 180 |
|
| 181 |
def main() -> None:
|
| 182 |
start_ts = int(time.time())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
dry_run = os.getenv("DRY_RUN", "0") == "1"
|
| 184 |
|
| 185 |
+
env_base_url = _optional_env("ENV_BASE_URL", "http://localhost:7860")
|
| 186 |
+
api_base_url = _optional_env("API_BASE_URL", "https://api.openai.com/v1")
|
| 187 |
+
model_name = _optional_env("MODEL_NAME", "gpt-4o-mini")
|
| 188 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 189 |
+
|
| 190 |
+
if not hf_token:
|
| 191 |
+
dry_run = True
|
| 192 |
+
|
| 193 |
+
client = OpenAI(base_url=api_base_url, api_key=hf_token) if not dry_run else None
|
| 194 |
+
|
| 195 |
_emit(
|
| 196 |
"START",
|
| 197 |
{
|
|
|
|
| 202 |
"model_name": model_name,
|
| 203 |
"task_count": len(TASK_IDS),
|
| 204 |
"dry_run": dry_run,
|
| 205 |
+
"max_attempts": MAX_ATTEMPTS,
|
| 206 |
},
|
| 207 |
)
|
| 208 |
|
| 209 |
+
try:
|
| 210 |
+
results: list[TaskRun] = []
|
| 211 |
+
for index, task_id in enumerate(TASK_IDS, start=1):
|
| 212 |
+
result = _run_task(env_base_url, client, model_name, task_id, dry_run)
|
| 213 |
+
results.append(result)
|
| 214 |
+
_emit(
|
| 215 |
+
"STEP",
|
| 216 |
+
{
|
| 217 |
+
"event": "task_completed",
|
| 218 |
+
"index": index,
|
| 219 |
+
"task_id": result.task_id,
|
| 220 |
+
"score": result.score,
|
| 221 |
+
"rationale": result.rationale,
|
| 222 |
+
"latency_s": result.latency_s,
|
| 223 |
+
},
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
avg_score = round(sum(item.score for item in results) / len(results), 4)
|
| 227 |
+
total_latency = round(sum(item.latency_s for item in results), 3)
|
| 228 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
_emit(
|
| 230 |
+
"END",
|
| 231 |
{
|
| 232 |
+
"event": "run_finished",
|
| 233 |
+
"timestamp": int(time.time()),
|
| 234 |
+
"average_score": avg_score,
|
| 235 |
+
"total_latency_s": total_latency,
|
| 236 |
+
"tasks": [{"task_id": r.task_id, "score": r.score} for r in results],
|
| 237 |
+
"status": "ok",
|
| 238 |
},
|
| 239 |
)
|
| 240 |
+
except Exception as exc:
|
| 241 |
+
_emit(
|
| 242 |
+
"END",
|
| 243 |
+
{
|
| 244 |
+
"event": "run_finished",
|
| 245 |
+
"timestamp": int(time.time()),
|
| 246 |
+
"status": "error",
|
| 247 |
+
"error": str(exc),
|
| 248 |
+
},
|
| 249 |
+
)
|
| 250 |
+
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
|
| 253 |
if __name__ == "__main__":
|