-
Notifications
You must be signed in to change notification settings - Fork 0
[✨Feat] 키워드 분석 OpenAI worker 구현 #61
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ebddb04
:sparkles: feat: 키워드 분석 OpenAI worker 구현
yong203 f13dec1
:bug: fix: 키워드 분석 결과 상위 20개 제한 적용
yong203 72d3b19
:bug: fix: 키워드 분석 job 예외 실패 처리 보강
yong203 1ab2534
:test_tube: test: 키워드 분석 응답 검증 케이스 정리
yong203 ac41014
:test_tube: test: 키워드 분석 job 재실행 방지 검증 보강
yong203 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
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
9 changes: 9 additions & 0 deletions
9
src/main/java/com/daon/rewrite/keywordanalysis/client/KeywordAnalysisAnswer.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,9 @@ | ||
| package com.daon.rewrite.keywordanalysis.client; | ||
|
|
||
| public record KeywordAnalysisAnswer( | ||
| String questionId, | ||
| int questionOrder, | ||
| String question, | ||
| String finalAnswer | ||
| ) { | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/daon/rewrite/keywordanalysis/client/KeywordAnalysisClient.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.keywordanalysis.client; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface KeywordAnalysisClient { | ||
|
|
||
| List<KeywordAnalysisResult> analyze(KeywordAnalysisRequest request); | ||
| } |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/daon/rewrite/keywordanalysis/client/KeywordAnalysisClientException.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.keywordanalysis.client; | ||
|
|
||
| public class KeywordAnalysisClientException extends RuntimeException { | ||
|
|
||
| private final Reason reason; | ||
|
|
||
| private KeywordAnalysisClientException(Reason reason, String message, Throwable cause) { | ||
| super(message, cause); | ||
| this.reason = reason; | ||
| } | ||
|
|
||
| public static KeywordAnalysisClientException outputValidationFailed() { | ||
| return new KeywordAnalysisClientException( | ||
| Reason.OUTPUT_VALIDATION_FAILED, | ||
| "키워드 분석 결과 구조가 올바르지 않습니다.", | ||
| null | ||
| ); | ||
| } | ||
|
|
||
| public static KeywordAnalysisClientException outputValidationFailed(Throwable cause) { | ||
| return new KeywordAnalysisClientException( | ||
| Reason.OUTPUT_VALIDATION_FAILED, | ||
| "키워드 분석 결과를 변환할 수 없습니다.", | ||
| cause | ||
| ); | ||
| } | ||
|
|
||
| public static KeywordAnalysisClientException providerError(Throwable cause) { | ||
| return new KeywordAnalysisClientException( | ||
| Reason.PROVIDER_ERROR, | ||
| "키워드 분석 provider 호출에 실패했습니다.", | ||
| cause | ||
| ); | ||
| } | ||
|
|
||
| public Reason getReason() { | ||
| return reason; | ||
| } | ||
|
|
||
| public enum Reason { | ||
| PROVIDER_ERROR, | ||
| OUTPUT_VALIDATION_FAILED | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/daon/rewrite/keywordanalysis/client/KeywordAnalysisRequest.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,16 @@ | ||
| package com.daon.rewrite.keywordanalysis.client; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public record KeywordAnalysisRequest( | ||
| String title, | ||
| String companyName, | ||
| String positionTitle, | ||
| String preferences, | ||
| List<KeywordAnalysisAnswer> answers | ||
| ) { | ||
|
|
||
| public KeywordAnalysisRequest { | ||
| answers = List.copyOf(answers); | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/daon/rewrite/keywordanalysis/client/KeywordAnalysisResult.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,7 @@ | ||
| package com.daon.rewrite.keywordanalysis.client; | ||
|
|
||
| public record KeywordAnalysisResult( | ||
| String keyword, | ||
| int importance | ||
| ) { | ||
| } |
100 changes: 100 additions & 0 deletions
100
src/main/java/com/daon/rewrite/keywordanalysis/client/OpenAiKeywordAnalysisClient.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,100 @@ | ||
| package com.daon.rewrite.keywordanalysis.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.Comparator; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| @Component | ||
| public class OpenAiKeywordAnalysisClient implements KeywordAnalysisClient { | ||
|
|
||
| private static final int MAX_KEYWORD_COUNT = 20; | ||
| private static final int MIN_IMPORTANCE = 1; | ||
| private static final int MAX_IMPORTANCE = 100; | ||
| private static final String SYSTEM_PROMPT = """ | ||
| 당신은 한국어 자기소개서의 핵심 키워드를 분석하는 전문가입니다. | ||
| 자기소개서의 문항과 최종 작성본을 함께 읽고 워드클라우드에 사용할 핵심 키워드를 추출하세요. | ||
|
|
||
| 다음 기준을 반드시 지키세요. | ||
| - 핵심 키워드는 중요도 기준 상위 20개를 반환합니다. | ||
| - importance는 1~100 범위의 정수입니다. | ||
| - keyword는 짧은 명사 또는 명사구로 작성합니다. | ||
| - 같은 의미의 키워드를 중복 반환하지 않습니다. | ||
| - 응답은 JSON 객체 하나만 반환합니다. | ||
| """; | ||
|
|
||
| private static final JsonMapper JSON_MAPPER = JsonMapper.builder().build(); | ||
|
|
||
| private final ChatClient chatClient; | ||
|
|
||
| public OpenAiKeywordAnalysisClient(ChatClient.Builder chatClientBuilder) { | ||
| this.chatClient = chatClientBuilder.build(); | ||
| } | ||
|
|
||
| @Override | ||
| public List<KeywordAnalysisResult> analyze(KeywordAnalysisRequest request) { | ||
| OpenAiKeywordAnalysisResponse response; | ||
| try { | ||
| response = chatClient.prompt() | ||
| .system(SYSTEM_PROMPT) | ||
| .user(buildUserPrompt(request)) | ||
| .call() | ||
| .entity(OpenAiKeywordAnalysisResponse.class); | ||
| } catch (JacksonException exception) { | ||
| throw KeywordAnalysisClientException.outputValidationFailed(exception); | ||
| } catch (Exception exception) { | ||
| throw KeywordAnalysisClientException.providerError(exception); | ||
| } | ||
|
|
||
| return validateAndNormalize(response); | ||
| } | ||
|
|
||
| private String buildUserPrompt(KeywordAnalysisRequest request) { | ||
| return "다음 자기소개서 최종 작성본의 핵심 키워드를 분석하세요.\n입력 JSON:\n" | ||
| + JSON_MAPPER.writeValueAsString(request); | ||
| } | ||
|
|
||
| private List<KeywordAnalysisResult> validateAndNormalize(OpenAiKeywordAnalysisResponse response) { | ||
| if (response == null || response.keywords() == null | ||
| || response.keywords().isEmpty()) { | ||
| throw KeywordAnalysisClientException.outputValidationFailed(); | ||
| } | ||
|
|
||
| Set<String> seenKeywords = new HashSet<>(); | ||
| List<KeywordAnalysisResult> normalizedResults = response.keywords().stream() | ||
| .map(result -> normalize(result, seenKeywords)) | ||
| .toList(); | ||
|
|
||
| return normalizedResults.stream() | ||
| .sorted(Comparator.comparingInt(KeywordAnalysisResult::importance).reversed()) | ||
| .limit(MAX_KEYWORD_COUNT) | ||
| .toList(); | ||
| } | ||
|
|
||
| private KeywordAnalysisResult normalize(KeywordAnalysisResult result, Set<String> seenKeywords) { | ||
| if (result == null) { | ||
| throw KeywordAnalysisClientException.outputValidationFailed(); | ||
| } | ||
| String keyword = normalizeRequired(result.keyword()); | ||
| if (keyword == null | ||
| || result.importance() < MIN_IMPORTANCE | ||
| || result.importance() > MAX_IMPORTANCE | ||
| || !seenKeywords.add(keyword)) { | ||
| throw KeywordAnalysisClientException.outputValidationFailed(); | ||
| } | ||
| return new KeywordAnalysisResult(keyword, result.importance()); | ||
| } | ||
|
|
||
| private String normalizeRequired(String value) { | ||
| if (value == null) { | ||
| return null; | ||
| } | ||
| String normalized = value.strip(); | ||
| return normalized.isEmpty() ? null : normalized; | ||
| } | ||
| } | ||
6 changes: 6 additions & 0 deletions
6
src/main/java/com/daon/rewrite/keywordanalysis/client/OpenAiKeywordAnalysisResponse.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.keywordanalysis.client; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| record OpenAiKeywordAnalysisResponse(List<KeywordAnalysisResult> keywords) { | ||
| } |
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
31 changes: 31 additions & 0 deletions
31
src/main/java/com/daon/rewrite/keywordanalysis/service/KeywordAnalysisJobEventListener.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,31 @@ | ||
| package com.daon.rewrite.keywordanalysis.service; | ||
|
|
||
| import com.daon.rewrite.llmjob.entity.LlmJob; | ||
| import com.daon.rewrite.llmjob.entity.LlmJobType; | ||
| import com.daon.rewrite.llmjob.repository.LlmJobRepository; | ||
| import com.daon.rewrite.llmjob.service.LlmJobCreatedEvent; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.scheduling.annotation.Async; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.event.TransactionPhase; | ||
| import org.springframework.transaction.event.TransactionalEventListener; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| class KeywordAnalysisJobEventListener { | ||
|
|
||
| private final LlmJobRepository llmJobRepository; | ||
| private final KeywordAnalysisJobWorker worker; | ||
|
|
||
| @Async | ||
| @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) | ||
| public void handle(LlmJobCreatedEvent event) { | ||
| llmJobRepository.findById(event.jobId()).ifPresent(this::dispatch); | ||
| } | ||
|
yong203 marked this conversation as resolved.
|
||
|
|
||
| private void dispatch(LlmJob job) { | ||
| if (job.getType() == LlmJobType.KEYWORD_ANALYSIS) { | ||
| worker.execute(job.getId()); | ||
| } | ||
| } | ||
| } | ||
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.