Spaces:
Sleeping
Sleeping
Update inference.py
Browse files123456789101112131415161720 unmodified lines383940414243449 unmodified lines5455565758596061626364656667686936 unmodified lines1061071081091101111121131141151161171181191201211221233 unmodified lines12712812913013113210 unmodified lines143144145146147148149150151152153154155156157158159160161162163164165166"""Inference Script for Code Review Environment===================================MANDATORY- Before submitting, ensure the following variables are defined in your environment configuration:API_BASE_URL The API endpoint for the LLM.MODEL_NAME The model identifier to use for inference.HF_TOKEN Your Hugging Face / API key.- The inference script must be named `inference.py` and placed in the root directory of the project- Participants must use OpenAI Client for all LLM calls using above variables- Participants must emit structured stdout logs strictly following the [START], [STEP], and [END] format."""import osimport jsonimport textwrap20 unmodified lines- propose a fixed code snippet (prefixed with "propose_fix:")- skip if you cannot help (just "skip")- end the episode if the code is perfect (just "done")Be constructive, specific, and focus on improving code quality.Do not add any other text."""9 unmodified linesStep: {step}PR Title: {obs.pr_title}Description: {obs.pr_description}Code to review:{obs.code_snippet}Conversation so far:{comments_str}Previous actions:{history_str}Your response (choose one of the following formats):- write_comment: [your comment]- ask_question: [your question]36 unmodified linesdef main() -> None:# Check mandatory environment variablesif not API_BASE_URL or not API_KEY or not MODEL_NAME:print("Error: API_BASE_URL, HF_TOKEN/API_KEY, and MODEL_NAME must be set.")returnclient = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)env = CodeReviewEnv()tasks = ["easy", "medium", "hard", "harder", "hardest"]scores = {}# Emit START logprint("[START] code_review_env")for task in tasks:env.set_task(task)3 unmodified linesstep = 0final_reward = 0.0while not done and step < MAX_STEPS:step += 1user_prompt = build_user_prompt(step, obs, history)10 unmodified lines)response_text = completion.choices[0].message.content or ""except Exception as exc:print(f" Request failed: {exc}. Using fallback.")response_text = FALLBACK_ACTIONaction = parse_model_action(response_text)obs, reward, done, info = env.step(action)final_reward = reward.value# Emit STEP log with required fields: step, action_type, reward, done# Using a simple space-separated format: [STEP] <step> <action_type> <reward> <done>print(f"[STEP] {step} {action.action_type} {final_reward:.3f} {int(done)}")# Optionally store history for prompt (not part of log)history.append(f"Step {step}: {action.action_type}")scores[task] = final_reward# Emit END log with JSON scoresprint("[END] " + json.dumps(scores, separators=(',', ':')))if __name__ == "__main__":12320 unmodified lines2425262728299 unmodified lines39404142434445464748495036 unmodified lines878889909192939495969798991001011023 unmodified lines10610710810911011111211311410 unmodified lines125126127128129130131132133134135136137138139140141142143144145146import osimport jsonimport textwrap20 unmodified lines- propose a fixed code snippet (prefixed with "propose_fix:")- skip if you cannot help (just "skip")- end the episode if the code is perfect (just "done")Be constructive, specific, and focus on improving code quality.Do not add any other text."""9 unmodified linesStep: {step}PR Title: {obs.pr_title}Description: {obs.pr_description}Code to review:{obs.code_snippet}Conversation so far:{comments_str}Previous actions:{history_str}Your response (choose one of the following formats):- write_comment: [your comment]- ask_question: [your question]36 unmodified linesdef main() -> None:if not API_BASE_URL or not API_KEY or not MODEL_NAME:# This error message is printed to stderr? It must not appear on stdout.# Use sys.stderr to avoid polluting structured output.import sysprint("Error: API_BASE_URL, HF_TOKEN/API_KEY, and MODEL_NAME must be set.", file=sys.stderr)returnclient = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)env = CodeReviewEnv()tasks = ["easy", "medium", "hard", "harder", "hardest"]for task in tasks:env.set_task(task)3 unmodified linesstep = 0final_reward = 0.0# START markerprint(f"[START] task={task}", flush=True)while not done and step < MAX_STEPS:step += 1user_prompt = build_user_prompt(step, obs, history)10 unmodified lines)response_text = completion.choices[0].message.content or ""except Exception as exc:# Print error to stderr onlyimport sysprint(f"Request failed: {exc}. Using fallback.", file=sys.stderr)response_text = FALLBACK_ACTIONaction = parse_model_action(response_text)obs, reward, done, info = env.step(action)final_reward = reward.value# STEP marker with required key=value formatprint(f"[STEP] step={step} reward={final_reward:.3f}", flush=True)history.append(f"Step {step}: {action.action_type}")# END markerprint(f"[END] task={task} score={final_reward:.3f} steps={step}", flush=True)if __name__ == "__main__":
inference.py+14-34