Skip to content

Commit

Permalink
Merge pull request #52 from edu-pi/develop
Browse files Browse the repository at this point in the history
main <- develop
  • Loading branch information
ujkkk authored Nov 19, 2024
2 parents 81c2a79 + cf3f8f1 commit d40152b
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel


class HintRequest(BaseModel):
class AssistRequest(BaseModel):
line: int
source_code: str
5 changes: 0 additions & 5 deletions app/route/advice/models/correct_request.py

This file was deleted.

12 changes: 3 additions & 9 deletions app/route/advice/models/correct_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
11 changes: 5 additions & 6 deletions app/route/advice/router.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
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

router = APIRouter()


@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",
Expand All @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions app/route/advice/service/ai_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions app/route/advice/service/prompts/correct_template.txt
Original file line number Diff line number Diff line change
@@ -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}```
line: {line}
code: {code}
7 changes: 7 additions & 0 deletions app/route/advice/service/prompts/correct_template2.txt
Original file line number Diff line number Diff line change
@@ -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

8 changes: 8 additions & 0 deletions app/route/execute/service/execute_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion app/web/models/success_reponse.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit d40152b

Please sign in to comment.