-
Notifications
You must be signed in to change notification settings - Fork 0
[✨Feat] OpenAI 최초 첨삭 client 구현 #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/com/daon/rewrite/reviewversion/client/FirstReviewClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.daon.rewrite.reviewversion.client; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface FirstReviewClient { | ||
|
|
||
| List<FirstReviewResult> review(FirstReviewRequest request); | ||
| } |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/daon/rewrite/reviewversion/client/FirstReviewClientException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package com.daon.rewrite.reviewversion.client; | ||
|
|
||
| public class FirstReviewClientException extends RuntimeException { | ||
|
|
||
| private final Reason reason; | ||
|
|
||
| private FirstReviewClientException(Reason reason, String message, Throwable cause) { | ||
| super(message, cause); | ||
| this.reason = reason; | ||
| } | ||
|
|
||
| static FirstReviewClientException outputValidationFailed() { | ||
| return new FirstReviewClientException( | ||
| Reason.OUTPUT_VALIDATION_FAILED, | ||
| "최초 첨삭 결과 구조가 올바르지 않습니다.", | ||
| null | ||
| ); | ||
| } | ||
|
|
||
| static FirstReviewClientException outputValidationFailed(Throwable cause) { | ||
| return new FirstReviewClientException( | ||
| Reason.OUTPUT_VALIDATION_FAILED, | ||
| "최초 첨삭 결과를 변환할 수 없습니다.", | ||
| cause | ||
| ); | ||
| } | ||
|
|
||
| static FirstReviewClientException providerError(Throwable cause) { | ||
| return new FirstReviewClientException( | ||
| Reason.PROVIDER_ERROR, | ||
| "최초 첨삭 provider 호출에 실패했습니다.", | ||
| cause | ||
| ); | ||
| } | ||
|
|
||
| public Reason getReason() { | ||
| return reason; | ||
| } | ||
|
|
||
| public enum Reason { | ||
| PROVIDER_ERROR, | ||
| OUTPUT_VALIDATION_FAILED | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/daon/rewrite/reviewversion/client/FirstReviewQuestion.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.daon.rewrite.reviewversion.client; | ||
|
|
||
| public record FirstReviewQuestion( | ||
| String questionId, | ||
| int questionOrder, | ||
| String question, | ||
| int maxAnswerLength, | ||
| String originalAnswer | ||
| ) { | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/daon/rewrite/reviewversion/client/FirstReviewRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.daon.rewrite.reviewversion.client; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public record FirstReviewRequest( | ||
| String title, | ||
| String companyName, | ||
| String positionTitle, | ||
| String jobPostingUrl, | ||
| String preferences, | ||
| List<FirstReviewQuestion> questions | ||
| ) { | ||
|
|
||
| public FirstReviewRequest { | ||
| questions = List.copyOf(questions); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/daon/rewrite/reviewversion/client/FirstReviewResult.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.daon.rewrite.reviewversion.client; | ||
|
|
||
| public record FirstReviewResult( | ||
| String questionId, | ||
| String aiReport, | ||
| String rewrittenAnswer | ||
| ) { | ||
| } |
118 changes: 118 additions & 0 deletions
118
src/main/java/com/daon/rewrite/reviewversion/client/OpenAiFirstReviewClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package com.daon.rewrite.reviewversion.client; | ||
|
|
||
| import org.springframework.ai.chat.client.ChatClient; | ||
| import org.springframework.stereotype.Component; | ||
| import tools.jackson.core.JacksonException; | ||
| import tools.jackson.databind.json.JsonMapper; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| @Component | ||
| public class OpenAiFirstReviewClient implements FirstReviewClient { | ||
|
|
||
| private static final String SYSTEM_PROMPT = """ | ||
| 당신은 한국어 자기소개서를 첨삭하는 전문 리뷰어입니다. | ||
| 모든 문항을 함께 읽고 각 문항별 AI 리포트와 수정본을 작성하세요. | ||
|
|
||
| 다음 기준을 반드시 반영하세요. | ||
| - STAR 구성이 명확한지 평가합니다. | ||
| - 내용의 구체성을 평가합니다. | ||
| - 채용 우대사항과 직무에 적합한지 평가합니다. | ||
| - 맞춤법과 문장이 자연스러운지 평가합니다. | ||
| - 중복 표현을 줄입니다. | ||
| - 각 문항의 최대 글자 수를 준수합니다. | ||
| - 원문에 없는 경험이나 사실을 임의로 만들지 않습니다. | ||
|
|
||
| 응답의 questionId는 입력값을 그대로 사용하고 모든 문항의 결과를 정확히 한 번씩 반환하세요. | ||
| """; | ||
|
|
||
| private static final JsonMapper JSON_MAPPER = JsonMapper.builder().build(); | ||
|
|
||
| private final ChatClient chatClient; | ||
|
|
||
| public OpenAiFirstReviewClient(ChatClient.Builder chatClientBuilder) { | ||
| this.chatClient = chatClientBuilder.build(); | ||
| } | ||
|
|
||
| @Override | ||
| public List<FirstReviewResult> review(FirstReviewRequest request) { | ||
| OpenAiFirstReviewResponse response; | ||
| try { | ||
| response = chatClient.prompt() | ||
| .system(SYSTEM_PROMPT) | ||
| .user(buildUserPrompt(request)) | ||
| .call() | ||
| .entity(OpenAiFirstReviewResponse.class); | ||
| } catch (JacksonException exception) { | ||
| throw FirstReviewClientException.outputValidationFailed(exception); | ||
| } catch (Exception exception) { | ||
| throw FirstReviewClientException.providerError(exception); | ||
| } | ||
|
|
||
| return validateAndNormalize(request, response); | ||
| } | ||
|
|
||
| private String buildUserPrompt(FirstReviewRequest request) { | ||
| return "다음 자기소개서 전체를 첨삭하세요.\n입력 JSON:\n" | ||
| + JSON_MAPPER.writeValueAsString(request); | ||
| } | ||
|
|
||
| private List<FirstReviewResult> validateAndNormalize( | ||
| FirstReviewRequest request, | ||
| OpenAiFirstReviewResponse response | ||
| ) { | ||
| if (response == null || response.results() == null | ||
| || response.results().size() != request.questions().size()) { | ||
| throw FirstReviewClientException.outputValidationFailed(); | ||
| } | ||
|
|
||
| Map<String, FirstReviewQuestion> questionById = new HashMap<>(); | ||
| for (FirstReviewQuestion question : request.questions()) { | ||
| questionById.put(question.questionId(), question); | ||
| } | ||
|
|
||
| Map<String, FirstReviewResult> normalizedResultById = new HashMap<>(); | ||
| for (FirstReviewResult result : response.results()) { | ||
| if (result == null || result.questionId() == null || result.questionId().isBlank() | ||
| || normalizedResultById.containsKey(result.questionId())) { | ||
| throw FirstReviewClientException.outputValidationFailed(); | ||
| } | ||
|
|
||
| FirstReviewQuestion question = questionById.get(result.questionId()); | ||
| String aiReport = normalizeRequired(result.aiReport()); | ||
| String rewrittenAnswer = normalizeRequired(result.rewrittenAnswer()); | ||
| if (question == null || aiReport == null || rewrittenAnswer == null | ||
| || countCodePoints(rewrittenAnswer) > question.maxAnswerLength()) { | ||
| throw FirstReviewClientException.outputValidationFailed(); | ||
| } | ||
|
|
||
| normalizedResultById.put(result.questionId(), new FirstReviewResult( | ||
| result.questionId(), | ||
| aiReport, | ||
| rewrittenAnswer | ||
| )); | ||
| } | ||
|
|
||
| if (!normalizedResultById.keySet().equals(questionById.keySet())) { | ||
| throw FirstReviewClientException.outputValidationFailed(); | ||
| } | ||
|
|
||
| return request.questions().stream() | ||
| .map(question -> normalizedResultById.get(question.questionId())) | ||
| .toList(); | ||
| } | ||
|
|
||
| private String normalizeRequired(String value) { | ||
| if (value == null) { | ||
| return null; | ||
| } | ||
| String normalized = value.strip(); | ||
| return normalized.isEmpty() ? null : normalized; | ||
| } | ||
|
|
||
| private int countCodePoints(String value) { | ||
| return value.codePointCount(0, value.length()); | ||
| } | ||
| } | ||
6 changes: 6 additions & 0 deletions
6
src/main/java/com/daon/rewrite/reviewversion/client/OpenAiFirstReviewResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package com.daon.rewrite.reviewversion.client; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| record OpenAiFirstReviewResponse(List<FirstReviewResult> results) { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,8 @@ spring: | |
| path: /h2-console | ||
|
|
||
| ai: | ||
| retry: | ||
| max-attempts: 1 | ||
| openai: | ||
| api-key: ${OPENAI_API_KEY} | ||
| chat: | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.