Spaces:
Sleeping
Sleeping
databoysu commited on
Commit ·
a1e4e94
1
Parent(s): b4f37fd
fix reward hacking
Browse files45 unmodified lines464748495051521 unmodified line54555657585960616251 unmodified lines1141151161171181191201211221231241251261271281291301311321331341351361371381396 unmodified lines1461471481491501511527 unmodified lines16016116216316416537 unmodified lines2032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322338 unmodified lines2422432442452462472486 unmodified lines25525625725825926026111 unmodified lines27327427527627727827945 unmodified linesBENCHMARK = os.getenv("BENCHMARK", "tracefix_rl")MAX_STEPS = int(os.getenv("MAX_STEPS", "50"))SUCCESS_SCORE_THRESHOLD = float(os.getenv("SUCCESS_SCORE_THRESHOLD", "0.99"))THINKING_TOKEN_LIMIT = int(os.getenv("THINKING_TOKEN_LIMIT", "512"))MAX_PARSE_RETRIES = 3SYSTEM_PROMPT = (1 unmodified line"Return only JSON for one action.\n"'Allowed action_type values: VIEW_CODE, RUN_TESTS, REPLACE_LINES, UNDO_EDIT, RESET_TO_ORIGINAL, SUBMIT.\n'"For REPLACE_LINES include start_line, end_line, new_code_block.\n""Prefer RUN_TESTS after edits and SUBMIT only when all tests pass.")def log_start(task: str, env: str, model: str) -> None:print(f"[START] task={task} env={env} model={model}", flush=True)51 unmodified lines)def _get_model_action(client: OpenAI, observation: Any, history: list[str]) -> tuple[dict[str, Any], str]:obs_text = _build_observation_text(observation)user_prompt = ("Pick the single best next action and return only JSON.\n\n"f"{obs_text}\n\n"f"history:\n{chr(10).join(history[-5:]) if history else 'none'}")completion = client.chat.completions.create(model=MODEL_NAME,messages=[{"role": "system", "content": SYSTEM_PROMPT},{"role": "user", "content": user_prompt},],temperature=0.0,max_tokens=THINKING_TOKEN_LIMIT,stream=False,)response_text = (completion.choices[0].message.content or "").strip()action = _extract_json(response_text)if action.get("action_type") not in {6 unmodified lines}:raise ValueError("Invalid action_type in model response.")return action, response_textdef _to_code_action(action_dict: dict[str, Any]) -> CodeAction:7 unmodified linesreturn CodeAction(**payload)def _compute_score(step_result: Any, rewards: list[float]) -> float:meta = step_result.observation.metadata or {}raw = meta.get("final_score")37 unmodified linesaction: Optional[CodeAction] = Nonemodel_response = ""for attempt in range(1, MAX_PARSE_RETRIES + 1):try:action_dict, model_response = _get_model_action(client, result.observation, history)action = _to_code_action(action_dict)if show_thought:history.append(f"thought={action.thought}")breakexcept Exception as exc:cause = str(exc).replace("\n", " ")history.append((f"parse_failure attempt={attempt} cause={cause}. ""Error: Invalid JSON or schema. Return a complete valid JSON object ""with fields: thought, action_type, start_line, end_line, new_code_block."))if model_response:history.append(f"raw_response={model_response[:500]}")if action is None:action = CodeAction(action_type="RUN_TESTS",thought="Fallback after repeated invalid JSON/schema responses.",)result = await env.step(action)8 unmodified linesrewards.append(reward)steps_taken = stephistory.append(f"step={step} action={action_str} reward={reward:.2f} error={error or 'null'}")log_step(step=step, action=action_str, reward=reward, done=done, error=error)if done:6 unmodified linesif not started:log_start(task=TASK_NAME, env=BENCHMARK, model=MODEL_NAME)started = Truemsg = str(exc).replace("\n", " ")score = 0.0success = Falsefinally:11 unmodified linesgroup.add_argument("--easy", action="store_true", help="Run on easy curriculum tier.")group.add_argument("--medium", action="store_true", help="Run on medium curriculum tier.")group.add_argument("--hard", action="store_true", help="Run on hard curriculum tier.")parser.add_argument("--thought", action="store_true", help="Include model thought traces in internal history.")args = parser.parse_args()difficulty: Optional[str] = None45 unmodified lines464748495051521 unmodified line5455565758596061626364656667686970717273747576777879808182838485868788899091929351 unmodified lines1451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791806 unmodified lines1871881891901911921937 unmodified lines20120220320420520620720820921021121221337 unmodified lines2512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892908 unmodified lines2993003013023033043053063073083096 unmodified lines31631731831932032111 unmodified lines33333433533633733833945 unmodified linesBENCHMARK = os.getenv("BENCHMARK", "tracefix_rl")MAX_STEPS = int(os.getenv("MAX_STEPS", "50"))SUCCESS_SCORE_THRESHOLD = float(os.getenv("SUCCESS_SCORE_THRESHOLD", "0.99"))THINKING_TOKEN_LIMIT = int(os.getenv("THINKING_TOKEN_LIMIT", "1000"))MAX_PARSE_RETRIES = 3SYSTEM_PROMPT = (1 unmodified line"Return only JSON for one action.\n"'Allowed action_type values: VIEW_CODE, RUN_TESTS, REPLACE_LINES, UNDO_EDIT, RESET_TO_ORIGINAL, SUBMIT.\n'"For REPLACE_LINES include start_line, end_line, new_code_block.\n""If available, include a 'thought' field explaining what you observed and why this is the next best action.\n""Prefer RUN_TESTS after edits and SUBMIT only when all tests pass.")ACTION_JSON_SCHEMA: dict[str, Any] = {"type": "json_schema","json_schema": {"name": "CodeAction","strict": True,"schema": {"type": "object","properties": {"thought": {"type": ["string", "null"]},"action_type": {"type": "string","enum": ["VIEW_CODE","RUN_TESTS","REPLACE_LINES","UNDO_EDIT","RESET_TO_ORIGINAL","SUBMIT",],},"start_line": {"type": ["integer", "null"]},"end_line": {"type": ["integer", "null"]},"new_code_block": {"type": ["string", "null"]},},"required": ["action_type"],"additionalProperties": False,},},}def log_start(task: str, env: str, model: str) -> None:print(f"[START] task={task} env={env} model={model}", flush=True)51 unmodified lines)def _get_model_response(client: OpenAI, observation: Any, history: list[str]) -> str:obs_text = _build_observation_text(observation)user_prompt = ("Pick the single best next action and return only JSON.\n\n"f"{obs_text}\n\n"f"history:\n{chr(10).join(history[-5:]) if history else 'none'}")request_kwargs = {"model": MODEL_NAME,"messages": [{"role": "system", "content": SYSTEM_PROMPT},{"role": "user", "content": user_prompt},],"temperature": 0.0,"max_tokens": THINKING_TOKEN_LIMIT,"stream": False,}try:completion = client.chat.completions.create(**request_kwargs,response_format=ACTION_JSON_SCHEMA,)except Exception:completion = client.chat.completions.create(**request_kwargs)return (completion.choices[0].message.content or "").strip()def _parse_model_action(response_text: str) -> dict[str, Any]:action = _extract_json(response_text)if action.get("action_type") not in {6 unmodified lines}:raise ValueError("Invalid action_type in model response.")return actiondef _to_code_action(action_dict: dict[str, Any]) -> CodeAction:7 unmodified linesreturn CodeAction(**payload)def _print_thought(action_dict: dict[str, Any], raw_response: str) -> None:thought = action_dict.get("thought")thought_text = thought.strip() if isinstance(thought, str) else ""print("[THOUGHT]", file=sys.stderr, flush=True)print(thought_text if thought_text else raw_response, file=sys.stderr, flush=True)def _compute_score(step_result: Any, rewards: list[float]) -> float:meta = step_result.observation.metadata or {}raw = meta.get("final_score")37 unmodified linesaction: Optional[CodeAction] = Nonemodel_response = ""if step == 1:action = CodeAction(action_type="VIEW_CODE",thought="First step policy: inspect source before testing or editing.",)if show_thought:print("[THOUGHT]", file=sys.stderr, flush=True)print(action.thought, file=sys.stderr, flush=True)else:for attempt in range(1, MAX_PARSE_RETRIES + 1):try:model_response = _get_model_response(client, result.observation, history)action_dict = _parse_model_action(model_response)if show_thought:_print_thought(action_dict, model_response)action = _to_code_action(action_dict)breakexcept Exception as exc:cause = str(exc).replace("\n", " ")history.append((f"parse_failure attempt={attempt} cause={cause}. ""Error: Invalid JSON or schema. Return a complete valid JSON object ""with fields: thought, action_type, start_line, end_line, new_code_block."))if model_response:history.append(f"raw_response={model_response[:500]}")if action is None:action = CodeAction(action_type="RUN_TESTS",thought="Fallback after repeated invalid JSON/schema responses.",)result = await env.step(action)8 unmodified linesrewards.append(reward)steps_taken = stepaction_thought = (action.thought or "").strip()history.append(f"Action {action_str}; reward {reward:.2f}; error {error or 'null'}."+ (f" Thought: {action_thought}" if action_thought else ""))log_step(step=step, action=action_str, reward=reward, done=done, error=error)if done:6 unmodified linesif not started:log_start(task=TASK_NAME, env=BENCHMARK, model=MODEL_NAME)started = Truescore = 0.0success = Falsefinally:11 unmodified linesgroup.add_argument("--easy", action="store_true", help="Run on easy curriculum tier.")group.add_argument("--medium", action="store_true", help="Run on medium curriculum tier.")group.add_argument("--hard", action="store_true", help="Run on hard curriculum tier.")parser.add_argument("--thought", action="store_true", help="Print LLM thought trace to stderr only.")args = parser.parse_args()difficulty: Optional[str] = None
inference.py+98-38