diff --git a/app/route/advice/models/hint_request.py b/app/route/advice/models/assist_request.py similarity index 68% rename from app/route/advice/models/hint_request.py rename to app/route/advice/models/assist_request.py index 1776729..8bbe2ff 100644 --- a/app/route/advice/models/hint_request.py +++ b/app/route/advice/models/assist_request.py @@ -1,6 +1,6 @@ from pydantic import BaseModel -class HintRequest(BaseModel): +class AssistRequest(BaseModel): line: int source_code: str diff --git a/app/route/advice/models/correct_request.py b/app/route/advice/models/correct_request.py deleted file mode 100644 index 56bfc43..0000000 --- a/app/route/advice/models/correct_request.py +++ /dev/null @@ -1,5 +0,0 @@ -from pydantic import BaseModel - - -class CorrectRequest(BaseModel): - source_code: str \ No newline at end of file diff --git a/app/route/advice/models/correct_response.py b/app/route/advice/models/correct_response.py index 114ffbf..62a3fff 100644 --- a/app/route/advice/models/correct_response.py +++ b/app/route/advice/models/correct_response.py @@ -11,13 +11,7 @@ class CorrectResponse(BaseModel): modified_codes: list[ModifiedCode] @classmethod - async def of(cls, response_data: dict): - modified_codes_data = response_data.get("modified_codes", []) - modified_codes = [ - ModifiedCode(line=item.get("line", 0), code=item.get("code", "")) - for item in modified_codes_data - ] - + async def of(cls, response_data: dict, line: int): + modified_codes_data = response_data.get("code", "") reason = response_data.get("reason", "틀린 원인을 찾을 수 없습니다.") - - return CorrectResponse(reason=reason, modified_codes= modified_codes) + return CorrectResponse(reason=reason, modified_codes=[ModifiedCode(line=line, code=modified_codes_data)]) diff --git a/app/route/advice/router.py b/app/route/advice/router.py index 8636cd9..1ccb788 100644 --- a/app/route/advice/router.py +++ b/app/route/advice/router.py @@ -1,8 +1,7 @@ from fastapi import APIRouter from starlette.responses import JSONResponse -from app.route.advice.models.correct_request import CorrectRequest -from app.route.advice.models.hint_request import HintRequest +from app.route.advice.models.assist_request import AssistRequest from app.web.models.success_reponse import SuccessResponse from app.route.advice.service import ai_service @@ -10,8 +9,8 @@ @router.post("/v1/advice/correction") -async def correct(correct_request: CorrectRequest): - correct_response = await ai_service.correct(code=correct_request.source_code) +async def correct(assist_request: AssistRequest): + correct_response = await ai_service.correct(line=assist_request.line, code=assist_request.source_code) success_response = SuccessResponse( detail="success correct", @@ -25,8 +24,8 @@ async def correct(correct_request: CorrectRequest): @router.post("/v1/advice/hint") -async def hint(hint_request: HintRequest): - hint_response = await ai_service.hint(line=hint_request.line, code=hint_request.source_code) +async def hint(assist_request: AssistRequest): + hint_response = await ai_service.hint(line=assist_request.line, code=assist_request.source_code) success_response = SuccessResponse( detail="success hint", diff --git a/app/route/advice/service/ai_service.py b/app/route/advice/service/ai_service.py index a79995b..5588df1 100644 --- a/app/route/advice/service/ai_service.py +++ b/app/route/advice/service/ai_service.py @@ -13,14 +13,15 @@ from app.web.logger import logger -async def correct(code: str) -> CorrectResponse: +async def correct(line: int, code: str) -> CorrectResponse: template_path = path.join(path.dirname(__file__), 'prompts', 'correct_template.txt') template = await _load_template(template_path) - prompt = template.format(code=code) + print(line) + prompt = template.format(line=line, code=code) response_data = await _call_openai_api(prompt) - return await CorrectResponse.of(response_data) + return await CorrectResponse.of(response_data, line) async def hint(line: int, code: str) -> HintResponse: diff --git a/app/route/advice/service/prompts/correct_template.txt b/app/route/advice/service/prompts/correct_template.txt index 60e5ccf..7013e10 100644 --- a/app/route/advice/service/prompts/correct_template.txt +++ b/app/route/advice/service/prompts/correct_template.txt @@ -1,8 +1,8 @@ -Here’s the broken code. Explain the error briefly and give the corrected code in the following JSON format: +Here’s the broken python code and line. Give the corrected code in the following JSON format +Reflect the original code's indentation properly in the code -reason: In 1 sentences, explain why the error occurred (in honorifics Korean). Use '.' as a sentence delimiter. -modified_codes : Reflect the original code's indentation properly in the corrected lines, using the format: - line: "uncorrected line number", - code: "corrected code"'. +reason: In 1 sentences, explain why the error occurred (in honorifics Korean) in code. Use '.' as a sentence delimiter. +code: only one line fixed to the given {line} line. For your information, the line is \n It's divided into.You have to give me one line -Code: ```python {code}``` \ No newline at end of file +line: {line} +code: {code} \ No newline at end of file diff --git a/app/route/advice/service/prompts/correct_template2.txt b/app/route/advice/service/prompts/correct_template2.txt new file mode 100644 index 0000000..52798b0 --- /dev/null +++ b/app/route/advice/service/prompts/correct_template2.txt @@ -0,0 +1,7 @@ +Problematic Python code. {code} +From an educator's point of view, you should answer to the student by correcting the reason why this code is wrong and the wrong code. +Please follow the following json format for the answer. + +reason: Wrong reason +code: corrected code + diff --git a/app/route/execute/service/execute_service.py b/app/route/execute/service/execute_service.py index 41f42a1..eafe177 100644 --- a/app/route/execute/service/execute_service.py +++ b/app/route/execute/service/execute_service.py @@ -29,6 +29,9 @@ def execute_code(source_code: str, user_input: str): # 프로세스 실행 중 비정상 종료 except subprocess.CalledProcessError as e: line_number = _get_error_line_number(e.stderr) + if _is_input_missmatch(e.stderr): + raise CodeExecuteError(ErrorEnum.INPUT_SIZE_MATCHING_ERROR) + raise CodeSyntaxError(ErrorEnum.CODE_SYNTAX_ERROR, {"lineNumber": line_number, "errorMessage": e.stderr}) except subprocess.TimeoutExpired as e: @@ -39,6 +42,11 @@ def execute_code(source_code: str, user_input: str): raise TaskFailException(ErrorEnum.CODE_EXEC_SERVER_ERROR, e.args[0]) +def _is_input_missmatch(error_msg): + if "EOF when reading" in error_msg: + return True + return False + def _get_error_line_number(error_msg): matches = re.findall(r'line (\d+), in', error_msg) if matches: diff --git a/app/web/models/success_reponse.py b/app/web/models/success_reponse.py index 01e02bd..b644575 100644 --- a/app/web/models/success_reponse.py +++ b/app/web/models/success_reponse.py @@ -1,6 +1,6 @@ class SuccessResponse: def __init__(self, detail: str, result: dict = None): - self.code = "CS-200000" + self.code = "CA-200000" self.detail = detail self.result = result