Spaces:
Runtime error
Runtime error
stream rollout messages per-turn via env_response hook (no more end-of-rollout replay)
Browse files- dashboard/backend/app.py +88 -39
dashboard/backend/app.py
CHANGED
|
@@ -191,11 +191,14 @@ async def _run_stream(
|
|
| 191 |
judge_model: str,
|
| 192 |
api_key: str,
|
| 193 |
) -> AsyncIterator[dict[str, Any]]:
|
| 194 |
-
"""Run the rollout and emit SSE events.
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
| 199 |
"""
|
| 200 |
yield {"event": "info", "data": json.dumps({"status": "starting", "task_id": task_id})}
|
| 201 |
|
|
@@ -221,42 +224,88 @@ async def _run_stream(
|
|
| 221 |
|
| 222 |
yield {"event": "info", "data": json.dumps({"status": "running", "tutor_model": tutor_model, "student_model": student_model, "judge_model": judge_model})}
|
| 223 |
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
)
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
-
|
| 243 |
-
messages = _normalize_messages((r.get("prompt") or []) + (r.get("completion") or []))
|
| 244 |
-
for m in messages:
|
| 245 |
-
yield {"event": "message", "data": json.dumps(m)}
|
| 246 |
-
await asyncio.sleep(0.05)
|
| 247 |
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
|
| 262 |
# --- helpers ---------------------------------------------------------------
|
|
|
|
| 191 |
judge_model: str,
|
| 192 |
api_key: str,
|
| 193 |
) -> AsyncIterator[dict[str, Any]]:
|
| 194 |
+
"""Run the rollout and emit SSE events with **real per-message streaming.**
|
| 195 |
+
|
| 196 |
+
Strategy: we let `env.evaluate` drive the rollout, but monkey-patch
|
| 197 |
+
`env.env_response` so each time it's called (between turns) it pushes the
|
| 198 |
+
newly-arrived messages onto an asyncio.Queue. The SSE generator consumes
|
| 199 |
+
from that queue and yields `message` events as they land — the user sees
|
| 200 |
+
the tutor's reply appear as soon as the LLM call returns, then the
|
| 201 |
+
student's reply, etc., instead of waiting for the whole rollout.
|
| 202 |
"""
|
| 203 |
yield {"event": "info", "data": json.dumps({"status": "starting", "task_id": task_id})}
|
| 204 |
|
|
|
|
| 224 |
|
| 225 |
yield {"event": "info", "data": json.dumps({"status": "running", "tutor_model": tutor_model, "student_model": student_model, "judge_model": judge_model})}
|
| 226 |
|
| 227 |
+
# --- per-message streaming hook ---
|
| 228 |
+
queue: asyncio.Queue = asyncio.Queue()
|
| 229 |
+
last_emitted = [0] # mutable so the closure can update it
|
| 230 |
+
original_env_response = env.env_response
|
| 231 |
+
|
| 232 |
+
def _emit_new(messages: list[Any]) -> None:
|
| 233 |
+
new = list(messages)[last_emitted[0]:]
|
| 234 |
+
for m in _normalize_messages(new):
|
| 235 |
+
if m["role"] == "system":
|
| 236 |
+
continue
|
| 237 |
+
queue.put_nowait(("message", m))
|
| 238 |
+
last_emitted[0] = len(messages)
|
| 239 |
+
|
| 240 |
+
async def patched_env_response(messages, state, **kw):
|
| 241 |
+
# Emit the tutor's just-landed reply (and the user seed on the first call).
|
| 242 |
+
_emit_new(messages)
|
| 243 |
+
result = await original_env_response(messages, state, **kw)
|
| 244 |
+
# Emit any messages the env appended (student reply, etc.).
|
| 245 |
+
if result:
|
| 246 |
+
for m in _normalize_messages(list(result)):
|
| 247 |
+
if m["role"] != "system":
|
| 248 |
+
queue.put_nowait(("message", m))
|
| 249 |
+
last_emitted[0] += len(result)
|
| 250 |
+
return result
|
| 251 |
+
|
| 252 |
+
env.env_response = patched_env_response
|
| 253 |
+
|
| 254 |
+
async def run_eval():
|
| 255 |
+
try:
|
| 256 |
+
outputs = await env.evaluate(
|
| 257 |
+
client=tutor_client,
|
| 258 |
+
model=tutor_model,
|
| 259 |
+
num_examples=1,
|
| 260 |
+
rollouts_per_example=1,
|
| 261 |
+
state_columns=["judge_breakdown"],
|
| 262 |
+
)
|
| 263 |
+
await queue.put(("__done", outputs))
|
| 264 |
+
except Exception as e:
|
| 265 |
+
logger.exception("Rollout failed")
|
| 266 |
+
await queue.put(("__error", str(e)))
|
| 267 |
|
| 268 |
+
eval_task = asyncio.create_task(run_eval())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
+
try:
|
| 271 |
+
while True:
|
| 272 |
+
etype, payload = await queue.get()
|
| 273 |
+
if etype == "__error":
|
| 274 |
+
yield {"event": "error", "data": json.dumps({"error": payload})}
|
| 275 |
+
return
|
| 276 |
+
if etype == "__done":
|
| 277 |
+
outputs = payload
|
| 278 |
+
rollouts = outputs.get("outputs", []) if isinstance(outputs, dict) else []
|
| 279 |
+
if not rollouts:
|
| 280 |
+
yield {"event": "error", "data": json.dumps({"error": "No rollout produced"})}
|
| 281 |
+
return
|
| 282 |
+
r = rollouts[0]
|
| 283 |
+
# Flush any messages that arrived after the final env_response call
|
| 284 |
+
# (e.g. the last tutor turn doesn't trigger env_response on its way out).
|
| 285 |
+
all_msgs = (r.get("prompt") or []) + (r.get("completion") or [])
|
| 286 |
+
final_new = all_msgs[last_emitted[0]:]
|
| 287 |
+
for m in _normalize_messages(final_new):
|
| 288 |
+
if m["role"] != "system":
|
| 289 |
+
yield {"event": "message", "data": json.dumps(m)}
|
| 290 |
+
await asyncio.sleep(0.05)
|
| 291 |
+
breakdown = r.get("judge_breakdown") or {}
|
| 292 |
+
yield {
|
| 293 |
+
"event": "done",
|
| 294 |
+
"data": json.dumps(
|
| 295 |
+
{
|
| 296 |
+
"reward": r.get("reward", 0.0),
|
| 297 |
+
"judge_breakdown": breakdown,
|
| 298 |
+
"stop_condition": r.get("stop_condition"),
|
| 299 |
+
"metrics": r.get("metrics"),
|
| 300 |
+
}
|
| 301 |
+
),
|
| 302 |
+
}
|
| 303 |
+
return
|
| 304 |
+
# ("message", payload)
|
| 305 |
+
yield {"event": etype, "data": json.dumps(payload)}
|
| 306 |
+
finally:
|
| 307 |
+
if not eval_task.done():
|
| 308 |
+
eval_task.cancel()
|
| 309 |
|
| 310 |
|
| 311 |
# --- helpers ---------------------------------------------------------------
|