Skip to content

Commit 521ef17

Browse files
authored
Merge pull request #8 from kangwonlee/feature/refactor-prompt-parts
Feature/refactor prompt parts
2 parents e5b0953 + 1f1de94 commit 521ef17

File tree

3 files changed

+113
-79
lines changed

3 files changed

+113
-79
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
87
## [Unreleased] - YYYY-MM-DD
98

109
### Added
11-
* Bahasa Indonesia, Nederands, and Vietnamese support
1210

1311

1412
### Changed
13+
* move README before pytest longrepr
1514

1615

1716
### Deprecated

ai_tutor.py

Lines changed: 73 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def gemini_qna(
105105
logging.info(f"Student files: {student_files}")
106106
logging.info(f"Readme file: {readme_file}")
107107

108-
consolidated_question = get_the_question(
108+
consolidated_question = get_prompt(
109109
report_paths,
110110
student_files,
111111
readme_file,
@@ -117,22 +117,13 @@ def gemini_qna(
117117
return answers
118118

119119

120-
def get_the_question(
120+
def get_prompt(
121121
report_paths:Tuple[pathlib.Path],
122122
student_files:Tuple[pathlib.Path],
123123
readme_file:pathlib.Path,
124124
explanation_in:str,
125125
) -> str:
126-
questions = []
127-
128-
# Process each report file
129-
for report_path in report_paths:
130-
logging.info(f"Processing report file: {report_path}")
131-
data = json.loads(report_path.read_text())
132-
133-
longrepr_list = collect_longrepr(data)
134-
135-
questions += longrepr_list
126+
pytest_longrepr_list = collect_longrepr_from_multiple_reports(report_paths, explanation_in)
136127

137128
def get_initial_instruction(questions:List[str],language:str) -> str:
138129
# Add the main directive or instruction based on whether there are failed tests
@@ -146,18 +137,42 @@ def get_initial_instruction(questions:List[str],language:str) -> str:
146137
return initial_instruction
147138

148139

149-
questions = (
140+
prompt_list = (
150141
# Add the initial instruction
151-
[get_initial_instruction(questions, explanation_in), get_report_header(explanation_in)]
152-
+ questions
142+
[
143+
get_initial_instruction(pytest_longrepr_list, explanation_in),
144+
get_instruction_block(readme_file, explanation_in,),
145+
]
146+
+ pytest_longrepr_list
153147
# Add the code and instructions
154-
+ [get_report_footer(explanation_in), get_code_instruction(student_files, readme_file, explanation_in)]
148+
+ [
149+
get_student_code_block(student_files, explanation_in,),
150+
]
155151
)
156152

157153
# Join all questions into a single string
158-
consolidated_question = "\n\n".join(questions)
154+
prompt_str = "\n\n".join(prompt_list)
155+
156+
return prompt_str
157+
158+
159+
def collect_longrepr_from_multiple_reports(pytest_json_report_paths:Tuple[pathlib.Path], explanation_in:str) -> List[str]:
160+
questions = []
161+
162+
# Process each report file
163+
for pytest_json_report_path in pytest_json_report_paths:
164+
logging.info(f"Processing report file: {pytest_json_report_path}")
165+
data = json.loads(pytest_json_report_path.read_text())
166+
167+
longrepr_list = collect_longrepr(data)
168+
169+
questions += longrepr_list
170+
171+
if questions:
172+
questions.insert(0, get_report_header(explanation_in))
173+
questions.append(get_report_footer(explanation_in))
159174

160-
return consolidated_question
175+
return questions
161176

162177

163178
@functools.lru_cache
@@ -190,13 +205,6 @@ def collect_longrepr(data:Dict[str, str]) -> List[str]:
190205
return longrepr_list
191206

192207

193-
@functools.lru_cache
194-
def get_question(longrepr:str, explanation_in:str,) -> str:
195-
return (
196-
get_report_header(explanation_in) + f"{longrepr}\n" + get_report_footer(explanation_in)
197-
)
198-
199-
200208
@functools.lru_cache
201209
def get_report_header(explanation_in:str) -> str:
202210
d = {
@@ -239,42 +247,7 @@ def get_report_footer(explanation_in:str) -> str:
239247
)
240248

241249

242-
@functools.lru_cache
243-
def get_code_instruction(
244-
student_files:Tuple[pathlib.Path],
245-
readme_file:pathlib.Path,
246-
explanation_in:str,
247-
) -> str:
248-
249-
d_homework_start = {
250-
'Korean': "숙제 제출 코드 시작",
251-
'English': "Homework Submission Code Start",
252-
'Bahasa Indonesia': "Kode Pengumpulan Tugas Dimulai",
253-
'Chinese': "作业提交代码开始",
254-
'French': '''Début du code de soumission des devoirs''',
255-
'German': "Code für die Einreichung von Hausaufgaben von hier aus",
256-
'Italian': "Inizio del codice di invio dei compiti",
257-
'Japanese': "宿題提出コード開始",
258-
'Nederlands': "Huiswerk inzendcode begint",
259-
'Spanish': "Inicio del código de envío de tareas",
260-
'Thai': "การส่งงานเริ่มต้น",
261-
'Vietnamese': "Bắt đầu mã nộp bài tập",
262-
}
263-
264-
d_homework_end = {
265-
'Korean': "숙제 제출 코드 끝",
266-
'English': "Homework Submission Code End",
267-
'Bahasa Indonesia': "Kode Pengumpulan Tugas Berakhir",
268-
'Chinese': "作业提交代码结束",
269-
'French': '''Fin du code de soumission des devoirs''',
270-
'German': "Ende der Hausaufgaben-Einreichungscodes",
271-
'Italian': "Fine del codice di invio dei compiti",
272-
'Japanese': "宿題提出コード終わり",
273-
'Nederlands': "Huiswerk inzendcode eindigt",
274-
'Spanish': "Fin del código de envío de tareas",
275-
'Thai': "การส่งงานสิ้นสุด",
276-
'Vietnamese': "Mã nộp bài tập kết thúc",
277-
}
250+
def get_instruction_block(readme_file:pathlib.Path, explanation_in:str='Korean',) -> str:
278251

279252
d_instruction_start = {
280253
'Korean': "과제 지침 시작",
@@ -307,15 +280,51 @@ def get_code_instruction(
307280
}
308281

309282
return (
310-
f"\n\n## {d_homework_start[explanation_in]}\n"
311-
f"{assignment_code(student_files)}\n"
312-
f"## {d_homework_end[explanation_in]}\n"
313283
f"## {d_instruction_start[explanation_in]}\n"
314284
f"{assignment_instruction(readme_file)}\n"
315285
f"## {d_instruction_end[explanation_in]}\n"
316286
)
317287

318288

289+
def get_student_code_block(student_files:Tuple[pathlib.Path], explanation_in:str) -> str:
290+
291+
d_homework_start = {
292+
'Korean': "숙제 제출 코드 시작",
293+
'English': "Homework Submission Code Start",
294+
'Bahasa Indonesia': "Kode Pengumpulan Tugas Dimulai",
295+
'Chinese': "作业提交代码开始",
296+
'French': '''Début du code de soumission des devoirs''',
297+
'German': "Code für die Einreichung von Hausaufgaben von hier aus",
298+
'Italian': "Inizio del codice di invio dei compiti",
299+
'Japanese': "宿題提出コード開始",
300+
'Nederlands': "Huiswerk inzendcode begint",
301+
'Spanish': "Inicio del código de envío de tareas",
302+
'Thai': "เริ่มส่งรหัสการบ้าน",
303+
'Vietnamese': "Bắt đầu mã nộp bài tập",
304+
}
305+
306+
d_homework_end = {
307+
'Korean': "숙제 제출 코드 끝",
308+
'English': "Homework Submission Code End",
309+
'Bahasa Indonesia': "Kode Pengumpulan Tugas Berakhir",
310+
'Chinese': "作业提交代码结束",
311+
'French': '''Fin du code de soumission des devoirs''',
312+
'German': "Ende der Hausaufgaben-Einreichungscodes",
313+
'Italian': "Fine del codice di invio dei compiti",
314+
'Japanese': "宿題提出コード終わり",
315+
'Nederlands': "Huiswerk inzendcode eindigt",
316+
'Spanish': "Fin del código de envío de tareas",
317+
'Thai': "จบรหัสส่งการบ้าน",
318+
'Vietnamese': "Mã nộp bài tập kết thúc",
319+
}
320+
321+
return (
322+
f"\n\n## {d_homework_start[explanation_in]}\n"
323+
f"{assignment_code(student_files)}\n"
324+
f"## {d_homework_end[explanation_in]}\n"
325+
)
326+
327+
319328
@functools.lru_cache
320329
def assignment_code(student_files:Tuple[pathlib.Path]) -> str:
321330
return '\n\n'.join([f"# begin : {str(f.name)} ======\n{f.read_text()}\n# end : {str(f.name)} ======\n" for f in student_files])

tests/test_ai_tutor.py

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def json_dict(sample_report_path) -> Dict[str, Union[str, List]]:
3333
return result
3434

3535

36-
def test_collect_longrepr(json_dict):
36+
def test_collect_longrepr__returns_non_empty(json_dict):
3737
result = ai_tutor.collect_longrepr(json_dict)
3838

3939
assert result
@@ -51,12 +51,29 @@ def json_dict_div_zero_try_except(div_zero_report_path:pathlib.Path) -> Dict[str
5151
return result
5252

5353

54-
def test_collect_longrepr(json_dict_div_zero_try_except:Dict):
54+
def test_collect_longrepr_div_zero_dict__returns_non_empty(json_dict_div_zero_try_except:Dict):
5555
result = ai_tutor.collect_longrepr(json_dict_div_zero_try_except)
5656

5757
assert result
5858

5959

60+
def test_collect_longrepr_from_multiple_reports__returns_non_empty(
61+
sample_report_path:pathlib.Path,
62+
div_zero_report_path:pathlib.Path,
63+
explanation_in:str
64+
):
65+
multiple_reports = (
66+
sample_report_path,
67+
div_zero_report_path
68+
)
69+
result = ai_tutor.collect_longrepr_from_multiple_reports(
70+
multiple_reports,
71+
explanation_in=explanation_in,
72+
)
73+
74+
assert result
75+
76+
6077
@pytest.fixture(params=('Korean', 'English', 'Bahasa Indonesia', 'Chinese', 'French', 'German', 'Italian', 'Japanese', 'Nederlands', 'Spanish', 'Thai', 'Vietnamese'))
6178
def explanation_in(request) -> str:
6279
return request.param
@@ -173,36 +190,45 @@ def instruction(explanation_in:str) -> str:
173190
)
174191

175192

176-
def test_get_code_instruction(
177-
sample_student_code_path:pathlib.Path,
193+
def test_get_instruction_block(
178194
sample_readme_path:pathlib.Path,
179195
explanation_in:str,
180-
homework:Tuple[str],
181196
instruction:str,
182197
):
183198

184-
result = ai_tutor.get_code_instruction(
185-
student_files = (sample_student_code_path,),
199+
result = ai_tutor.get_instruction_block(
186200
readme_file = sample_readme_path,
187201
explanation_in=explanation_in
188202
).lower()
189203

190204
assert any(
191205
map(
192206
lambda x: x in result,
193-
homework
207+
instruction
194208
)
195-
)
209+
), f"Could not find instruction: {instruction} in result: {result}."
210+
211+
212+
def test_get_student_code_block(
213+
sample_student_code_path:pathlib.Path,
214+
explanation_in:str,
215+
homework:Tuple[str],
216+
):
217+
218+
result = ai_tutor.get_student_code_block(
219+
student_files = (sample_student_code_path,),
220+
explanation_in=explanation_in
221+
).lower()
196222

197223
assert any(
198224
map(
199225
lambda x: x in result,
200-
instruction
226+
homework
201227
)
202-
), f"Could not find instruction: {instruction} in result: {result}."
228+
)
203229

204230

205-
def test_get_the_question(
231+
def test_get_prompt__has__homework__msg__instruction(
206232
sample_report_path:pathlib.Path,
207233
div_zero_report_path:pathlib.Path,
208234
sample_student_code_path:pathlib.Path,
@@ -212,7 +238,7 @@ def test_get_the_question(
212238
msg:str,
213239
instruction:str,
214240
):
215-
result = ai_tutor.get_the_question(
241+
result = ai_tutor.get_prompt(
216242
report_paths=(sample_report_path,div_zero_report_path),
217243
student_files=(sample_student_code_path,),
218244
readme_file=sample_readme_path,

0 commit comments

Comments
 (0)