(executor, completion)
| 135 | |
| 136 | |
| 137 | def postprocess_completion(executor, completion): |
| 138 | # solution = re.findall(r"<solution>(.*?)</solution>", completion, re.DOTALL) |
| 139 | # assert len(solution) <= 1 |
| 140 | # has_solution = bool(len(solution) > 0) |
| 141 | |
| 142 | executions = ["!" + code for code in re.findall(r"```bash(.*?)```", completion, re.DOTALL) if "!" not in code] |
| 143 | executions.extend(re.findall(r"```python(.*?)```", completion, re.DOTALL)) |
| 144 | |
| 145 | if len(executions) == 0: # directly return cot result |
| 146 | return completion |
| 147 | else: |
| 148 | |
| 149 | ### Jupyter |
| 150 | # # put code into notebook |
| 151 | # execution_outputs = [executor(code) for code in executions] |
| 152 | # # extract answer |
| 153 | # extracted_outputs = [(int(match.group(1)), match.group(2)) for match in (re.match(r'Out\[(\d+)\]: (.+)', s) for s in execution_outputs) if match] |
| 154 | |
| 155 | ### Python |
| 156 | execution_outputs = [] |
| 157 | for code in executions: |
| 158 | try: |
| 159 | success, output = executor(code) |
| 160 | except TimeoutError: |
| 161 | print("time out") |
| 162 | # success = False |
| 163 | output = "" |
| 164 | else: |
| 165 | output = output if success else "" |
| 166 | execution_outputs.append(output) |
| 167 | extracted_outputs = execution_outputs |
| 168 | |
| 169 | # if len(extracted_outputs) < 1: |
| 170 | # return "" |
| 171 | |
| 172 | for index in range(1, len(extracted_outputs) + 1): |
| 173 | # if len(extracted_outputs[-index]) < 2: |
| 174 | # return None |
| 175 | # extracted_solution = str(extracted_outputs[-index][1]).strip() # Jupyter |
| 176 | extracted_solution = str(extracted_outputs[-index]).strip() # Python |
| 177 | break |
| 178 | |
| 179 | return extracted_solution |
| 180 | |
| 181 | |
| 182 | def postprocess_completions(completion_list): |
no outgoing calls
no test coverage detected