-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathutils.py
More file actions
327 lines (279 loc) · 12.6 KB
/
utils.py
File metadata and controls
327 lines (279 loc) · 12.6 KB
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
import re
import json
import yaml
import os
import difflib
from pathlib import Path
class PromptLoader:
def __init__(self, config_path):
with open(config_path, 'r') as config_file:
self.config = yaml.safe_load(config_file)
def load_prompt(self, prompt_name):
prompt_path = Path(self.config['prompt_directory']) / self.config['prompts'][prompt_name]
with open(prompt_path, 'r') as prompt_file:
return prompt_file.read()
def format_prompt(self, prompt_name, **kwargs):
prompt = self.load_prompt(prompt_name)
return prompt.format(**kwargs)
def generate_diff(original_code, edited_code):
original_lines = original_code.splitlines()
edited_lines = edited_code.splitlines()
diff = difflib.unified_diff(
original_lines, edited_lines,
fromfile='original_code', tofile='edited_code',
lineterm=''
)
return '\n'.join(diff)
class OutputParser:
def __init__(self):
pass
def detect_quote_type(self, string):
string = string.strip()
if string.startswith('"""'):
return '"""'
elif string.startswith("'''"):
return "'''"
elif string.startswith('"'):
return '"'
elif string.startswith("'"):
return "'"
else:
return None
def balance_braces(self, s: str):
count = 0
for i, char in enumerate(s):
if char == '{':
count += 1
elif char == '}':
count -= 1
if count == 0:
return s[:i+1]
return None
def json_parser(self, text):
start = text.find('{')
if start == -1:
raise ValueError("No JSON-like structure found in the text")
json_str = self.balance_braces(text[start:])
if json_str is None:
raise ValueError("Unbalanced braces in the JSON-like structure")
# Remove trailing commas before closing braces or square brackets
self.json_str = re.sub(r',\s*([}\]])', r'\1', json_str)
# data = json.loads(text)
if '"Feedback":' in self.json_str:
a = self.json_str.split('"Feedback": ')[-1].strip()
quote = self.detect_quote_type(a)
a = a.replace(quote, "'''")
data = eval(self.json_str)
return data
def problem_code_parser(self, text):
try:
data = self.json_parser(text)
if 'problem' not in data or 'code' not in data:
raise ValueError("Parsed JSON is missing 'problem' or 'code' keys")
return data
except Exception as e:
print(f"JSON parsing error: {e}")
print(f"Attempting to salvage data from malformed JSON...")
problem = self.json_str.split('"problem":')[-1].split('"code":')[0].strip()
start_quote = self.detect_quote_type(problem)
if start_quote is not None:
problem = problem.replace(start_quote, "")[:-1]
code = self.json_str.split('"code":')[-1].strip()
if code.endswith("}"):
code = code.strip()[:-1].strip()
start_quote = self.detect_quote_type(code)
if start_quote is not None:
code = code.replace(start_quote, "")
code = code if code != "" else None
# code = code_match.group(2) if code_match else None
if problem or code:
return {
"problem": problem,
"code": code
}
else:
raise ValueError("Failed to extract problem or code from malformed JSON")
def code_edit_parser(self, text):
try:
data = self.json_parser(text)
if 'improvement_areas' not in data or 'edited_code' not in data:
raise ValueError("Parsed JSON is missing 'problem' or 'code' keys")
return data
except Exception as e:
print(f"JSON parsing error: {e}")
print(f"Attempting to salvage data from malformed JSON...")
problem = self.json_str.split('"improvement_areas":')[1].split('"edited_code":')[0].strip()
start_quote = self.detect_quote_type(problem)
if start_quote is not None:
problem = problem.replace(start_quote, "")[:-1] # removing ,
code = self.json_str.split('"edited_code":')[1].strip()
if code.endswith("}"):
code = code.strip()[:-1].strip()
start_quote = self.detect_quote_type(code)
if start_quote is not None:
code = code.replace(start_quote, "")
code = code if code != "" else None
# code = code_match.group(2) if code_match else None
if problem or code:
return {
"improvement_areas": problem,
"edited_code": code
}
else:
raise ValueError("Failed to extract problem or code from malformed JSON")
def get_start_idx(path: str, reverse=False) -> int:
with open(path, 'r') as f:
lines = [l for l in f.readlines() if l.strip()]
if not lines:
return 0
first_line = lines[0]
last_line = lines[-1]
start_index = int(json.loads(first_line)['seed_code_id'])
last_index = int(json.loads(last_line)['seed_code_id'])
if reverse:
return last_index - 1
else:
return last_index - start_index + 1
class CodeExtractor:
def __init__(self):
self.PROBLEM_START = "###PROBLEM_STATEMENT###"
self.PROBLEM_END = "###END_PROBLEM_STATEMENT###"
self.CODE_START = "###ORIGINAL_CODE###"
self.CODE_END = "###END_ORIGINAL_CODE###"
self.METADATA_START = "###METADATA###"
self.METADATA_END = "###END_METADATA###"
# New delimiters for edits
self.PREF1_START = "###PREFERRED_SOLUTION_1###"
self.PREF1_END = "###END_PREFERRED_SOLUTION_1###"
self.PREF2_START = "###PREFERRED_SOLUTION_2###"
self.PREF2_END = "###END_PREFERRED_SOLUTION_2###"
self.NON_PREF_START = "###NON_PREFERRED_SOLUTION###"
self.NON_PREF_END = "###END_NON_PREFERRED_SOLUTION###"
self.DIFF_START = "###DIFFERENCES_EXPLAINED###"
self.DIFF_END = "###END_DIFFERENCES_EXPLAINED###"
# New delimiters for instructions
self.DETAILED_START = "###DETAILED_INSTRUCTION###"
self.DETAILED_END = "###END_DETAILED_INSTRUCTION###"
self.CONCISE_START = "###CONCISE_INSTRUCTION###"
self.CONCISE_END = "###END_CONCISE_INSTRUCTION###"
self.HUMAN_START = "###HUMAN_INSTRUCTION###"
self.HUMAN_END = "###END_HUMAN_INSTRUCTION###"
self.CONV_START = "###CONVERSATIONAL_INSTRUCTION###"
self.CONV_END = "###END_CONVERSATIONAL_INSTRUCTION###"
# New delimiters for quality check
self.COHERENCE_START = "###COHERENCE_CHECK###"
self.COHERENCE_END = "###END_COHERENCE_CHECK###"
self.QUALITY_START = "###QUALITY_CHECK###"
self.QUALITY_END = "###END_QUALITY_CHECK###"
self.VERDICT_START = "###FINAL_VERDICT###"
self.VERDICT_END = "###END_FINAL_VERDICT###"
def _extract_section(self, text, start_delimiter, end_delimiter):
"""Helper method to extract content between delimiters"""
try:
start_idx = text.index(start_delimiter) + len(start_delimiter)
end_idx = text.index(end_delimiter)
return text[start_idx:end_idx].strip()
except ValueError as e:
raise ValueError(f"Failed to find delimiter: {str(e)}")
def problem_code_extractor(self, text):
try:
problem_statement = self._extract_section(text, self.PROBLEM_START, self.PROBLEM_END)
original_code = self._extract_section(text, self.CODE_START, self.CODE_END)
metadata = self._extract_section(text, self.METADATA_START, self.METADATA_END)
return {
'problem_statement': problem_statement,
'original_code': original_code,
'metadata': metadata
}
except Exception as e:
print(f"Error extracting content: {str(e)}")
return None
def code_edit_extractor(self, text):
try:
preferred_1 = self._extract_section(text, self.PREF1_START, self.PREF1_END)
preferred_2 = self._extract_section(text, self.PREF2_START, self.PREF2_END)
non_preferred = self._extract_section(text, self.NON_PREF_START, self.NON_PREF_END)
differences = self._extract_section(text, self.DIFF_START, self.DIFF_END)
return {
'preferred_edit_a': preferred_1,
'preferred_edit_b': preferred_2,
'non_preferred_edit': non_preferred,
'explanation': differences
}
except Exception as e:
print(f"Error extracting edits: {str(e)}")
return None
def instructions_extractor(self, text):
try:
detailed = self._extract_section(text, self.DETAILED_START, self.DETAILED_END)
concise = self._extract_section(text, self.CONCISE_START, self.CONCISE_END)
human = self._extract_section(text, self.HUMAN_START, self.HUMAN_END)
conversational = self._extract_section(text, self.CONV_START, self.CONV_END)
return {
'detailed_instruction': detailed,
'concise_instruction': concise,
'human_instruction': human,
'conversational_instruction': conversational
}
except Exception as e:
print(f"Error extracting instructions: {str(e)}")
return None
def _extract_score_explanation(self, section_text, index):
"""Helper to extract score and explanation from a section"""
lines = section_text.split('\n')
for i, line in enumerate(lines):
if line.startswith(f"{index}."):
# Find score line
for j in range(i, len(lines)):
if lines[j].startswith("Score:"):
score = float(lines[j].split(':')[1].strip().strip('[]'))
# Find explanation line
for k in range(j, len(lines)):
if lines[k].startswith("Explanation:"):
explanation = lines[k].split(':')[1].strip().strip('[]')
return score, explanation
return None, None
def quality_check_extractor(self, text):
try:
coherence = self._extract_section(text, self.COHERENCE_START, self.COHERENCE_END)
quality = self._extract_section(text, self.QUALITY_START, self.QUALITY_END)
verdict = self._extract_section(text, self.VERDICT_START, self.VERDICT_END)
scores = {}
for i in range(1, 3):
score, explanation = self._extract_score_explanation(coherence, i)
scores[f'coherence_{i}'] = {
'score': score,
'explanation': explanation
}
for i in range(1, 4):
score, explanation = self._extract_score_explanation(quality, i)
scores[f'quality_{i}'] = {
'score': score,
'explanation': explanation
}
verdict_lines = verdict.split('\n')
strengths = []
weaknesses = []
recommendations = []
current_section = None
for line in verdict_lines:
line = line.strip()
if line.startswith('Strengths:'):
current_section = strengths
elif line.startswith('Weaknesses:'):
current_section = weaknesses
elif line.startswith('Recommendations:'):
current_section = recommendations
elif line.startswith('-') and current_section is not None:
current_section.append(line[1:].strip())
return {
'scores': scores,
'verdict': {
'strengths': strengths,
'weaknesses': weaknesses,
'recommendations': recommendations
}
}
except Exception as e:
print(f"Error extracting quality check: {str(e)}")
return None