-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
428 lines (367 loc) · 12.6 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import asyncio
import multiprocessing
import os
import pathlib
from pathlib import Path
import queue
import re
import subprocess
import sys
import logging
import time
import traceback
from typing import Any, List
import math
import weave
import openai
import instructor
from pydantic import BaseModel, Field
from tree_sitter_languages import get_language, get_parser
# API params
BASE_URL = os.getenv("BASE_URL", None)
API_KEY = os.getenv("API_KEY", "dummy_key")
# params
MAX_TOKENS = int(os.getenv("MAX_TOKENS", 4096))
FAST_LLM = os.getenv("FAST_LLM", "open-mistral-nemo-2407")
STRONG_LLM = os.getenv("STRONG_LLM", "mistral-large-latest")
# API client
oai_client = openai.AsyncOpenAI(base_url=BASE_URL, api_key=API_KEY)
async_client = instructor.from_openai(oai_client, mode=instructor.Mode.JSON)
language = get_language("python")
tree_parser = get_parser("python")
import re
def maybe_remove_backticks(solution: str) -> str:
"Remove backticks from the solution"
solution = solution.strip()
solution = re.sub(r'^```python\s*', '', solution)
solution = re.sub(r'\s*```$', '', solution)
return solution
def remove_extra_newlines(text: str) -> str:
# Use regex to replace 2 or more newlines (with possible whitespace in between) with a single newline
text = re.sub(r"\n\s*\n+", "\n", text)
return text
def remove_comments_and_docstrings(code):
# Define queries to capture comments and docstrings
doc_str_pattern = """
(module . (expression_statement (string)) @module_doc_str)
(class_definition body: (block . (expression_statement (string)) @class_doc_str))
(function_definition body: (block . (expression_statement (string)) @function_doc_str))
"""
comment_pattern = "(comment) @comment"
# Parse the code
tree = tree_parser.parse(code.encode())
root_node = tree.root_node
# Query the tree for docstrings and comments
doc_str_query = language.query(doc_str_pattern)
doc_strs = doc_str_query.captures(root_node)
comment_query = language.query(comment_pattern)
comments = comment_query.captures(root_node)
# Get the start and end points of all docstrings and comments
doc_str_points = set((node.start_byte, node.end_byte) for node, _ in doc_strs)
comment_points = set((node.start_byte, node.end_byte) for node, _ in comments)
# Create a set of all points to remove
remove_points = doc_str_points.union(comment_points)
# Reconstruct the code, skipping over the parts to remove
cleaned_code = []
last_index = 0
for start, end in sorted(remove_points):
if last_index < start:
cleaned_code.append(code[last_index:start])
last_index = end
# Add any remaining code after the last comment/docstring
cleaned_code.append(code[last_index:])
return "".join(cleaned_code)
def clean_code_string(code: str) -> str:
code = remove_comments_and_docstrings(code)
code = remove_extra_newlines(code)
return code
class TestReport(BaseModel):
status: str
message: str
@property
def as_xml(self) -> str:
return f"""
<test_report>
<status>{self.status}</status>
<message>{self.message}</message>
</test_report>
"""
def compare_lines_with_tolerance(expected: str, actual: str, tolerance: float = 1e-9) -> bool:
"""
Compare two lines of output with a tolerance for floating point numbers.
"""
expected_lines = expected.strip().split('\n')
actual_lines = actual.strip().split('\n')
if len(expected_lines) != len(actual_lines):
return False
for expected_line, actual_line in zip(expected_lines, actual_lines):
expected_match = re.match(r"Case #\d+: (.+)", expected_line)
actual_match = re.match(r"Case #\d+: (.+)", actual_line)
if not expected_match or not actual_match:
return False
expected_values = expected_match.group(1).split()
actual_values = actual_match.group(1).split()
if len(expected_values) != len(actual_values):
return False
for expected_value, actual_value in zip(expected_values, actual_values):
try:
expected_float = float(expected_value)
actual_float = float(actual_value)
if not math.isclose(expected_float, actual_float, rel_tol=tolerance):
return False
except ValueError:
if expected_value != actual_value:
return False
return True
async def exec_program(program, input_data, expected_output, timeout):
try:
process = await asyncio.create_subprocess_exec(
sys.executable, "-c", program,
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
try:
stdout, stderr = await asyncio.wait_for(process.communicate(input=input_data.encode()), timeout=timeout)
except asyncio.TimeoutError:
process.kill()
return TestReport(
status="timeout",
message=f"Took too long! Your program timed out after {timeout} seconds of execution."
)
if process.returncode != 0:
return TestReport(
status="error", message=f"Program execution failed: {stderr.decode()}"
)
else:
if compare_lines_with_tolerance(expected_output, stdout.decode()):
return TestReport(
status="passed", message="Yay! Your program ran successfully"
)
else:
return TestReport(
status="failed",
message=f"<expected>\n{expected_output}</expected>\n---\n<got>\n{stdout.decode()}</got>",
)
except Exception:
return TestReport(
status="error", message=f"An error occurred: {traceback.format_exc()}"
)
@weave.op
async def check_correctness(
program: str, input_data: str, expected_output: str, timeout: float
) -> TestReport:
return await exec_program(program, input_data, expected_output, timeout)
@weave.op
async def format_response(text: str, model: Any, temperature: float = 0.1) -> Any:
formatted_response = await async_client.chat.completions.create(
model=FAST_LLM,
# Instructor adds a system message by default about how to format the response given the response model.
messages=[
{
"role": "user",
"content": f"Extract the relevant information from the following document and return it in valid JSON\n\n{text}",
}
],
temperature=temperature,
response_model=model,
max_retries=2,
max_tokens=MAX_TOKENS,
)
return formatted_response
class Problem(BaseModel):
problem_dir: pathlib.Path = Field(
..., description="The path to the problem directory"
)
problem_name: str = Field(..., description="The name of the problem")
problem_description: str = Field(..., description="The description of the problem")
sample_input: str = Field(..., description="The sample input of the problem")
sample_output: str = Field(..., description="The sample output of the problem")
problem_input: pathlib.Path = Field(..., description="The path to the input file")
problem_output: pathlib.Path = Field(..., description="The path to the output file")
@property
def as_xml(self) -> str:
return f"""
<problem>
<problem_statement>
{remove_extra_newlines(self.problem_description)}
</problem_statement>
<sample_test_cases>
<sample_input>
{self.sample_input}
</sample_input>
<sample_output>
{self.sample_output}
</sample_output>
</sample_test_cases>
</problem>
"""
@classmethod
def from_name(cls, problem_name: str, folder_path: Path):
description_path = folder_path / f"{problem_name}.md"
sample_input_path = folder_path / f"{problem_name}_sample_input.txt"
sample_output_path = folder_path / f"{problem_name}_sample_output.txt"
input_path = folder_path / f"{problem_name}.in"
output_path = folder_path / f"{problem_name}.out"
return cls.from_files(
problem_name=problem_name,
description_path=description_path,
sample_input_path=sample_input_path,
sample_output_path=sample_output_path,
input_path=input_path,
output_path=output_path,
)
@classmethod
def from_files(
cls,
problem_name: str,
description_path: Path,
sample_input_path: Path,
sample_output_path: Path,
input_path: Path,
output_path: Path = None,
):
return cls(
problem_name=problem_name,
problem_description=description_path.read_text(),
sample_input=sample_input_path.read_text(),
sample_output=sample_output_path.read_text(),
problem_input=input_path,
problem_output=output_path if output_path else input_path.with_suffix('.out'),
problem_dir=input_path.parent,
)
def find_problems(folder: Path) -> list[dict]:
"""
Find all the problems in the given folder.
"""
problems = []
# search for all files ending in .in
problem_names = [file.stem for file in folder.glob("**/*.in")]
for problem_name in problem_names:
try:
problems.append(Problem.from_name(problem_name, folder))
except Exception as e:
logging.error(f"Error loading problem {problem_name}: {e}")
logging.info(f"Found {len(problems)} problems")
return problems
class Analysis(BaseModel):
core_question: str = Field(..., description="Core question of the problem")
problem_solving_info: List[str] = Field(
..., description="Problem-solving information related to the core question"
)
algorithm: str = Field(..., description="Algorithm to solve the problem")
tutorial: str = Field(..., description="Tutorial on the algorithm")
plan: str = Field(..., description="Step by step plan to solve the problem")
pseudocode: str = Field(..., description="Pseudocode to solve the problem")
@property
def as_xml(self) -> str:
return f"""
<core_question>
{self.core_question}
</core_question>
<problem_solving_info>
{self.problem_solving_info}
</problem_solving_info>
<algorithm>
{self.algorithm}
</algorithm>
<tutorial>
{self.tutorial}
</tutorial>
<plan>
{self.plan}
</plan>
<pseudocode>
{self.pseudocode}
</pseudocode>
"""
class Solution(Analysis):
source_code: str = Field(
..., description="Valid Python3 sourcecode to solve the problem."
)
@property
def as_xml(self) -> str:
return f"""
<root>
{super().as_xml}
<source_code>
{self.source_code}
</source_code>
</root>
"""
class Reflection(BaseModel):
reflection: str = Field(
...,
description="Reflection on the problem, your solution, and the correct answer.",
)
keywords: List[str] = Field(
...,
description="Keywords that describe the type of your errors from most general to most specific.",
)
step_by_step_solution: str = Field(
...,
description="Step by step solution to the problem based on your knowledge of the correct answer.",
)
instructions: List[str] = Field(
...,
description="Detailed instructions to help you correctly solve this problem in the future.",
)
general_advice: List[str] = Field(
...,
description="General advice to help you solve similar types of problems in the future.",
)
@property
def as_xml(self) -> str:
return f"""
<root>
<reflection>
{self.reflection}
</reflection>
<keywords>
{self.keywords}
</keywords>
<step_by_step_solution>
{self.step_by_step_solution}
</step_by_step_solution>
<instructions>
{self.instructions}
</instructions>
<general_advice>
{self.general_advice}
</general_advice>
</root>
"""
def format_example(example: dict) -> str:
formatted_doc = f"""
<problem>
<problem_statement>
{example['description']}
</problem_statement>
</problem>
<solution>
{example['code']}
</solution>
"""
return formatted_doc
def format_examples(examples: List[dict], analyses: List[Analysis]) -> str:
def format_question(example: dict) -> str:
return f"""
<problem>
<problem_statement>
{example['description']}
</problem_statement>
</problem>
"""
def format_solution(analysis: Analysis, example: dict) -> str:
return f"""
<root>
{analysis.as_xml}
<source_code>
{example['code']}
</source_code>
</root>
"""
messages = ""
for example, analysis in zip(examples, analyses):
messages += f"\n<example>{format_question(example)}\n{format_solution(analysis, example)}</example>\n"
return messages.strip()