|
20 | 20 | import tempfile |
21 | 21 | import time |
22 | 22 | from dataclasses import dataclass |
| 23 | +from textwrap import dedent |
23 | 24 | from uuid import uuid4 |
24 | 25 |
|
25 | 26 | from openai import OpenAI |
@@ -96,39 +97,39 @@ def __init__(self, timeout: int = 30, memory_limit_mb: int = 512): |
96 | 97 | self.memory_limit_mb = memory_limit_mb |
97 | 98 |
|
98 | 99 | def execute(self, code: str, stdin: str = "") -> ProcessExecuteResult: |
99 | | - pre_template = f""" |
100 | | -import signal |
101 | | -import resource |
102 | | -import os |
103 | | -import sys |
| 100 | + pre_template = dedent(f"""\ |
| 101 | + import signal |
| 102 | + import resource |
| 103 | + import os |
| 104 | + import sys |
104 | 105 |
|
105 | | -os.environ['OPENBLAS_NUM_THREADS'] = '1' |
| 106 | + os.environ['OPENBLAS_NUM_THREADS'] = '1' |
106 | 107 |
|
107 | | -def _exec_set_alarm_timeout(timeout): |
108 | | - signal.signal(signal.SIGALRM, _exec_time_exceeded) |
109 | | - signal.alarm(timeout) |
| 108 | + def _exec_set_alarm_timeout(timeout): |
| 109 | + signal.signal(signal.SIGALRM, _exec_time_exceeded) |
| 110 | + signal.alarm(timeout) |
110 | 111 |
|
111 | | -def _exec_time_exceeded(*_): |
112 | | - print('Suicide from timeout.', flush=True) |
113 | | - try: |
114 | | - os.killpg(0, 9) |
115 | | - except Exception: |
116 | | - pass |
117 | | - os._exit({TIMEOUT_EXIT_CODE}) |
| 112 | + def _exec_time_exceeded(*_): |
| 113 | + print('Suicide from timeout.', flush=True) |
| 114 | + try: |
| 115 | + os.killpg(0, 9) |
| 116 | + except Exception: |
| 117 | + pass |
| 118 | + os._exit({TIMEOUT_EXIT_CODE}) |
118 | 119 |
|
119 | | -def _exec_set_max_runtime(seconds): |
120 | | - soft, hard = resource.getrlimit(resource.RLIMIT_CPU) |
121 | | - resource.setrlimit(resource.RLIMIT_CPU, (seconds, hard)) |
| 120 | + def _exec_set_max_runtime(seconds): |
| 121 | + soft, hard = resource.getrlimit(resource.RLIMIT_CPU) |
| 122 | + resource.setrlimit(resource.RLIMIT_CPU, (seconds, hard)) |
122 | 123 |
|
123 | | -_exec_set_alarm_timeout({self.timeout}) |
124 | | -_exec_set_max_runtime({self.timeout}) |
| 124 | + _exec_set_alarm_timeout({self.timeout}) |
| 125 | + _exec_set_max_runtime({self.timeout}) |
125 | 126 |
|
126 | | -_exec_time_start = time.perf_counter() |
127 | | -""" |
128 | | - post_template = f""" |
129 | | -_exec_time_end = time.perf_counter() |
130 | | -_exec_duration = _exec_time_end - _exec_time_start |
131 | | -""" |
| 127 | + _exec_time_start = time.perf_counter() |
| 128 | + """) |
| 129 | + post_template = dedent("""\ |
| 130 | + _exec_time_end = time.perf_counter() |
| 131 | + _exec_duration = _exec_time_end - _exec_time_start |
| 132 | + """) |
132 | 133 |
|
133 | 134 | with tempfile.TemporaryDirectory() as tmp_path: |
134 | 135 | source_path = f"{tmp_path}/source.py" |
@@ -307,23 +308,24 @@ async def run(self, messages: list[dict], sampling_params: dict) -> tuple[str, l |
307 | 308 | user_turns, assistant_turns = 0, 0 |
308 | 309 | all_response_content = [] |
309 | 310 |
|
310 | | - system_prompt = """You are an expert mathematician specialized in solving challenging math competition problems. |
| 311 | + system_prompt = dedent("""\ |
| 312 | + You are an expert mathematician specialized in solving challenging math competition problems. |
311 | 313 |
|
312 | | -You have access to a Python code execution tool. Use it to: |
313 | | -1. Perform calculations and verify your answers |
314 | | -2. Run code when you need precise computation |
315 | | -3. Test your hypotheses before giving final answers |
| 314 | + You have access to a Python code execution tool. Use it to: |
| 315 | + 1. Perform calculations and verify your answers |
| 316 | + 2. Run code when you need precise computation |
| 317 | + 3. Test your hypotheses before giving final answers |
316 | 318 |
|
317 | | -Instructions: |
318 | | -1. Think through the problem step by step |
319 | | -2. Use the python_code_with_standard_io tool when you need to execute code |
320 | | -3. Show your reasoning clearly |
321 | | -4. Put your final numerical answer inside \\boxed{} at the end |
| 319 | + Instructions: |
| 320 | + 1. Think through the problem step by step |
| 321 | + 2. Use the python_code_with_standard_io tool when you need to execute code |
| 322 | + 3. Show your reasoning clearly |
| 323 | + 4. Put your final numerical answer inside \\boxed{} at the end |
322 | 324 |
|
323 | | -For each function call, return a json object within <tool_call></tool_call> XML tags: |
324 | | -<tool_call> |
325 | | -{"name": "python_code_with_standard_io", "arguments": {"code": "your python code", "input": "stdin input if needed"}} |
326 | | -</tool_call>""" |
| 325 | + For each function call, return a json object within <tool_call></tool_call> XML tags: |
| 326 | + <tool_call> |
| 327 | + {"name": "python_code_with_standard_io", "arguments": {"code": "your python code", "input": "stdin input if needed"}} |
| 328 | + </tool_call>""") |
327 | 329 |
|
328 | 330 | formatted_messages = [msg for msg in messages if msg.get("role") != "system"] |
329 | 331 | if not any(msg.get("role") == "system" for msg in messages): |
|
0 commit comments