-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompts.py
289 lines (276 loc) · 17.5 KB
/
prompts.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
from utils import *
import tutorcode_api
type_name = {
"AC": "run correctly",
"WA": "produced a wrong output",
"TL": "exceeded the runtime limit",
"RE": "encountered a runtime error",
"CE": "has compilication errors",
"CTL": "exceeded the compile time limit",
"ML": "exceeded the memory limit",
'SEGV': 'encountered a segmentation fault',
'ABRT': 'encountered an abnormal abort signal',
'PE': 'produced an incorrect output format',
'FPE': 'encountered a floating point exception',
'OL': 'exceeded the output limit',
'FE': 'has not output data to the correct file',
'BE': 'produced binary data',
}
append_prompt = "The code you replied is still incorrect.\n\n"
def buildOneReplyPrompt(judge_result, nanti_status_id, description, code_to_fix, one_reply):
if description is not None:
prompt = "This is a programming problem description:\n" + description + "\n"
if judge_result.get('fileName', None) is not None:
prompt += "Your program should use file input and output. Read the input from a file named '" + judge_result['fileName'] + ".in' and write the output to a file named '" + judge_result['fileName'] + ".out'.\n\n"
prompt += "### Time Limit\n" + str(judge_result['timeLimit']) + "ms\n\n"
prompt += "### Memory Limit\n" + str(judge_result['memoryLimit']) + "KB\n\n"
prompt += "This is an incorrect code to the problem:\n```c++\n" + code_to_fix + "```\n\n"
else:
prompt = append_prompt
prompt += 'You are a software engineer.\n\n'
prompt += one_reply
prompt += "\n\nCan you repair the incorrect code?"
return prompt
def buildDefaultPrompt(judge_result, description, code_to_fix):
if description is not None:
prompt = "This is a programming problem description:\n" + description + "\n"
if judge_result.get('fileName', None) is not None:
prompt += "Your program should use file input and output. Read the input from a file named '" + judge_result['fileName'] + ".in' and write the output to a file named '" + judge_result['fileName'] + ".out'.\n\n"
prompt += "### Time Limit\n" + str(judge_result['timeLimit']) + "ms\n\n"
prompt += "### Memory Limit\n" + str(judge_result['memoryLimit']) + "KB\n\n"
prompt += "This is an incorrect code to the problem:\n```c++\n" + code_to_fix + "```\n\n"
else:
prompt = append_prompt
prompt += "You are a software engineer. Can you repair the incorrect code?\n"
return prompt
def buildSolutionPrompt(judge_result, description, code_to_fix, solution):
if description is not None:
prompt = "This is a programming problem description:\n" + description + "\n"
if judge_result.get('fileName', None) is not None:
prompt += "Your program should use file input and output. Read the input from a file named '" + judge_result['fileName'] + ".in' and write the output to a file named '" + judge_result['fileName'] + ".out'.\n\n"
prompt += "### Time Limit\n" + str(judge_result['timeLimit']) + "ms\n\n"
prompt += "### Memory Limit\n" + str(judge_result['memoryLimit']) + "KB\n\n"
prompt += "This is an incorrect code to the problem:\n```c++\n" + code_to_fix + "```\n\n"
else:
prompt = append_prompt
prompt += "This is a solution to the problem:\n\n" + solution + "\n\n"
prompt += "You are a software engineer. Can you repair the incorrect code?\n"
return prompt
def buildOneReplyAndTestcasePrompt(judge_result, nanti_status_id, description, code_to_fix, status, one_reply):
prompt = "This is a programming problem description:\n" + description + "\n"
if judge_result.get('fileName', None) is not None:
prompt += "Your program should use file input and output. Read the input from a file named '" + judge_result['fileName'] + ".in' and write the output to a file named '" + judge_result['fileName'] + ".out'.\n\n"
prompt += "### Time Limit\n" + str(judge_result['timeLimit']) + "ms\n\n"
prompt += "### Memory Limit\n" + str(judge_result['memoryLimit']) + "KB\n\n"
prompt += "This is an incorrect code to the problem:\n```c++\n" + code_to_fix + "```\n\n"
input_test, output_test = [], []
problem_id = judge_result['problemId']
judge_item = None
for item in judge_result['notac']:
if item['nantiStatusId'] == nanti_status_id:
judge_item = item
break
for i in range(len(judge_item['extra']['statuses'])):
if (status == 'CE' or judge_item['extra']['statuses'][i] != 4):
input_test.append(add_eoln(tutorcode_api.get_testcase(problem_id, i + 1)['input']))
output_test.append(add_eoln(tutorcode_api.get_testcase(problem_id, i + 1)['output']))
if status == "CE":
compile_log = parse_warning(judge_item['compileErrorLog']).strip()
prompt += "There are some compilation errors of the incorrect code:\n\n" + compile_log + '\n\n'
elif len(input_test) > 0:
prompt += 'The incorrect code failed to pass the following test cases.\n'
for i in range(len(input_test)):
prompt += 'For the input:\n\n' + input_test[i] + '\n\nthe output should be:\n\n' + output_test[i] + '\n\n'
prompt += 'You are a software engineer.\n\n'
prompt += one_reply
prompt += "\n\nCan you repair the incorrect code?"
return prompt
def buildTestcasePrompt(judge_result, nanti_status_id, description, code_to_fix, status, user_out):
if description is not None:
prompt = "This is a programming problem description:\n" + description + "\n"
if judge_result.get('fileName', None) is not None:
prompt += "Your program should use file input and output. Read the input from a file named '" + judge_result['fileName'] + ".in' and write the output to a file named '" + judge_result['fileName'] + ".out'.\n\n"
prompt += "### Time Limit\n" + str(judge_result['timeLimit']) + "ms\n\n"
prompt += "### Memory Limit\n" + str(judge_result['memoryLimit']) + "KB\n\n"
prompt += "\n\nThis is an incorrect code to the problem:\n```c++\n" + code_to_fix + "```\n\n"
else:
prompt = append_prompt
input_test, output_test = [], []
problem_id = judge_result['problemId']
judge_item = None
for item in judge_result['notac']:
if item['nantiStatusId'] == nanti_status_id:
judge_item = item
break
if user_out == '未找到对应输出文件。':
status = 'FE'
elif user_out == '无法显示,输出包含二进制数据':
status = 'BE'
for i in range(len(judge_item['extra']['statuses'])):
if (status == 'CE' or judge_item['extra']['statuses'][i] != 4):
input_test.append(add_eoln(tutorcode_api.get_testcase(problem_id, i + 1)['input']))
output_test.append(add_eoln(tutorcode_api.get_testcase(problem_id, i + 1)['output']))
if status == "CE":
compile_log = parse_warning(judge_item['compileErrorLog']).strip()
prompt += "There are some compilation errors of the incorrect code:\n\n" + compile_log + '\n\n'
elif len(input_test) > 0:
prompt += 'The incorrect code failed to pass the following test cases.\n'
for i in range(len(input_test)):
prompt += 'For the input:\n\n' + input_test[i] + '\n\nthe output should be:\n\n' + output_test[i] + '\n\n'
prompt += "You are a software engineer. Can you repair the incorrect code?"
return prompt
def buildOneReplyAndSolutionAndTestcaseImprovedNewPrompt(judge_result, nanti_status_id, description, code_to_fix, solution, status, user_out, one_reply):
prompt = "# Problem Description\n" + description + "\n"
if judge_result.get('fileName', None) is not None:
prompt += "Your program should use file input and output. Read the input from a file named '" + judge_result['fileName'] + ".in' and write the output to a file named '" + judge_result['fileName'] + ".out'.\n\n"
prompt += "### Time Limit\n" + str(judge_result['timeLimit']) + "ms\n\n"
prompt += "### Memory Limit\n" + str(judge_result['memoryLimit']) + "KB\n\n"
input_test, output_test = [], []
problem_id = judge_result['problemId']
judge_item = None
for item in judge_result['notac']:
if item['nantiStatusId'] == nanti_status_id:
judge_item = item
break
if user_out == '未找到对应输出文件。':
status = 'FE'
elif user_out == '无法显示,输出包含二进制数据':
status = 'BE'
for i in range(len(judge_item['extra']['statuses'])):
if (status == 'CE' or judge_item['extra']['statuses'][i] != 4):
input_test.append(add_eoln(tutorcode_api.get_testcase(problem_id, i + 1)['input']))
output_test.append(add_eoln(tutorcode_api.get_testcase(problem_id, i + 1)['output']))
prompt += "\n\n# Incorrect Code\n```c++\n" + code_to_fix + "```\n\n"
prompt += "# Instructor Reply\n\n"
prompt += one_reply
prompt += "\n\n# Solution Document\n\n" + solution + "\n\n"
prompt += "# Failing Test Cases\n\n"
if status == "CE":
compile_log = parse_warning(judge_item['compileErrorLog']).strip()
prompt += "There are some compilation errors of the incorrect code:\n\n" + compile_log + '\n\n'
elif len(input_test) > 0:
prompt += 'The incorrect code failed to pass the following test cases.\n'
for i in range(len(input_test)):
prompt += 'For the input:\n\n' + input_test[i] + '\n\nthe output should be:\n\n' + output_test[i] + '\n\n'
prompt += '\nYou are a software engineer. Can you repair the incorrect code?'
return prompt
def buildOneReplyAndSolutionAndTestcaseArray2Prompt(judge_result, nanti_status_id, description, code_to_fix, solution, status, user_out, one_reply):
prompts = []
prompt = "This is a programming problem description:\n" + description + "\n"
if judge_result.get('fileName', None) is not None:
prompt += "Your program should use file input and output. Read the input from a file named '" + judge_result['fileName'] + ".in' and write the output to a file named '" + judge_result['fileName'] + ".out'.\n\n"
prompt += "### Time Limit\n" + str(judge_result['timeLimit']) + "ms\n\n"
prompt += "### Memory Limit\n" + str(judge_result['memoryLimit']) + "KB\n\n"
input_test, output_test = [], []
problem_id = judge_result['problemId']
judge_item = None
for item in judge_result['notac']:
if item['nantiStatusId'] == nanti_status_id:
judge_item = item
break
if user_out == '未找到对应输出文件。':
status = 'FE'
elif user_out == '无法显示,输出包含二进制数据':
status = 'BE'
for i in range(len(judge_item['extra']['statuses'])):
if (status == 'CE' or judge_item['extra']['statuses'][i] != 4):
input_test.append(add_eoln(tutorcode_api.get_testcase(problem_id, i + 1)['input']))
output_test.append(add_eoln(tutorcode_api.get_testcase(problem_id, i + 1)['output']))
prompt += "\n\nThis is an incorrect code to the problem:\n```c++\n" + code_to_fix + "```\n\n"
prompt += one_reply
prompts.append(prompt)
prompts.append("This is a solution to the problem:\n\n" + solution + "\n\n")
if status == "CE":
compile_log = parse_warning(judge_item['compileErrorLog']).strip()
prompt = "There are some compilation errors of the incorrect code:\n\n" + compile_log + '\n\n'
elif len(input_test) > 0:
prompt = 'The incorrect code failed to pass the following test cases.\n'
for i in range(len(input_test)):
prompt += 'For the input:\n\n' + input_test[i] + '\n\nthe output should be:\n\n' + output_test[i] + '\n\n'
prompt += 'You are a software engineer. Can you repair the incorrect code?'
prompts.append(prompt)
return prompts
def buildOneReplyAndSolutionPrompt(judge_result, nanti_status_id, description, code_to_fix, solution, one_reply):
prompt = "This is a programming problem description:\n" + description + "\n"
if judge_result.get('fileName', None) is not None:
prompt += "Your program should use file input and output. Read the input from a file named '" + judge_result['fileName'] + ".in' and write the output to a file named '" + judge_result['fileName'] + ".out'.\n\n"
prompt += "### Time Limit\n" + str(judge_result['timeLimit']) + "ms\n\n"
prompt += "### Memory Limit\n" + str(judge_result['memoryLimit']) + "KB\n\n"
prompt += "This is an incorrect code to the problem:\n```markdown\n" + code_to_fix + "```\n\n"
prompt += "This is a solution to the problem:\n" + solution + "\n\n"
prompt += 'You are a software engineer.\n\n'
prompt += one_reply
prompt += "\n\nCan you repair the incorrect code?"
return prompt
def buildSolutionAndTestcasePrompt(judge_result, nanti_status_id, description, code_to_fix, solution, status, user_out):
if description is not None:
prompt = "This is a programming problem description:\n" + description + "\n"
if judge_result.get('fileName', None) is not None:
prompt += "Your program should use file input and output. Read the input from a file named '" + judge_result['fileName'] + ".in' and write the output to a file named '" + judge_result['fileName'] + ".out'.\n\n"
prompt += "### Time Limit\n" + str(judge_result['timeLimit']) + "ms\n\n"
prompt += "### Memory Limit\n" + str(judge_result['memoryLimit']) + "KB\n\n"
prompt += "This is an incorrect code to the problem:\n```c++\n" + code_to_fix + "```\n\n"
else:
prompt = append_prompt
input_test, output_test = [], []
problem_id = judge_result['problemId']
judge_item = None
for item in judge_result['notac']:
if item['nantiStatusId'] == nanti_status_id:
judge_item = item
break
if user_out == '未找到对应输出文件。':
status = 'FE'
elif user_out == '无法显示,输出包含二进制数据':
status = 'BE'
for i in range(len(judge_item['extra']['statuses'])):
if (status == 'CE' or judge_item['extra']['statuses'][i] != 4):
input_test.append(add_eoln(tutorcode_api.get_testcase(problem_id, i + 1)['input']))
output_test.append(add_eoln(tutorcode_api.get_testcase(problem_id, i + 1)['output']))
if status == "CE":
compile_log = parse_warning(judge_item['compileErrorLog']).strip()
prompt += "There are some compilation errors of the incorrect code:\n\n" + compile_log + '\n\n'
elif len(input_test) > 0:
prompt += 'The incorrect code failed to pass the following test cases.\n'
for i in range(len(input_test)):
prompt += 'For the input:\n\n' + input_test[i] + '\n\nthe output should be:\n\n' + output_test[i] + '\n\n'
prompt += "This is a solution to the problem:\n\n" + solution + "\n\n"
prompt += "You are a software engineer. Can you repair the incorrect code?"
return prompt
def buildAppendTestcasePrompt(judge_item, problem_id, status):
prompt = append_prompt
input_test, output_test = [], []
for i in range(len(judge_item['extra']['statuses'])):
if (status == 'CE' or judge_item['extra']['statuses'][i] != 4):
input_test.append(add_eoln(tutorcode_api.get_testcase(problem_id, i + 1)['input']))
output_test.append(add_eoln(tutorcode_api.get_testcase(problem_id, i + 1)['output']))
if status == "CE":
compile_log = parse_warning(judge_item['compileErrorLog']).strip()
prompt += "There are some compilation errors of the incorrect code:\n\n" + compile_log + '\n\n'
elif len(input_test) > 0:
prompt += 'The incorrect code failed to pass the following test cases.\n'
for i in range(len(input_test)):
prompt += 'For the input:\n\n' + input_test[i] + '\n\nthe output should be:\n\n' + output_test[i] + '\n\n'
prompt += "You are a software engineer. Can you repair the incorrect code?"
return prompt
def buildPrompt(judge_result, nanti_status_id, description, code_to_fix, solution, status, user_out, one_reply, prompt_type):
if prompt_type == "default":
return buildDefaultPrompt(judge_result, description, code_to_fix)
elif prompt_type == "reply":
return buildOneReplyPrompt(judge_result, nanti_status_id, description, code_to_fix, one_reply)
elif prompt_type == "solution":
return buildSolutionPrompt(judge_result, description, code_to_fix, solution)
elif prompt_type == "reply_and_testcase":
return buildOneReplyAndTestcasePrompt(judge_result, nanti_status_id, description, code_to_fix, status, one_reply)
elif prompt_type == "testcase":
return buildTestcasePrompt(judge_result, nanti_status_id, description, code_to_fix, status, user_out)
elif prompt_type == "reply_and_solution_and_testcase4": # For Most LLMs
return buildOneReplyAndSolutionAndTestcaseArray2Prompt(judge_result, nanti_status_id, description, code_to_fix, solution, status, user_out, one_reply)
elif prompt_type == "reply_and_solution_and_testcase7": # Only For Bard and Claude, Due to Limitation of Usage
return buildOneReplyAndSolutionAndTestcaseImprovedNewPrompt(judge_result, nanti_status_id, description, code_to_fix, solution, status, user_out, one_reply)
elif prompt_type == "reply_and_solution":
return buildOneReplyAndSolutionPrompt(judge_result, nanti_status_id, description, code_to_fix, solution, one_reply)
elif prompt_type == "solution_and_testcase":
return buildSolutionAndTestcasePrompt(judge_result, nanti_status_id, description, code_to_fix, solution, status, user_out)
elif prompt_type == "append_testcase":
return buildAppendTestcasePrompt(judge_result['item'], judge_result['problemId'], OJ_STATUSES[judge_result['status']])