-
Notifications
You must be signed in to change notification settings - Fork 0
#25 [Feat] 관점 생성, 수정 시 GPT 검수 #31
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
Open
HYH0804
wants to merge
2
commits into
dev
Choose a base branch
from
feat/#25
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+270
−49
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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
2 changes: 1 addition & 1 deletion
2
src/main/java/com/swyp/app/domain/perspective/entity/PerspectiveStatus.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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| package com.swyp.app.domain.perspective.entity; | ||
|
|
||
| public enum PerspectiveStatus { | ||
| PENDING, PUBLISHED, REJECTED | ||
| PENDING, PUBLISHED, REJECTED, MODERATION_FAILED | ||
| } |
107 changes: 107 additions & 0 deletions
107
src/main/java/com/swyp/app/domain/perspective/service/GptModerationService.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,107 @@ | ||
| package com.swyp.app.domain.perspective.service; | ||
|
|
||
| import com.swyp.app.domain.perspective.entity.PerspectiveStatus; | ||
| import com.swyp.app.domain.perspective.repository.PerspectiveRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.http.client.SimpleClientHttpRequestFactory; | ||
| import org.springframework.scheduling.annotation.Async; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.web.client.RestClient; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class GptModerationService { | ||
|
|
||
| // 프롬프트는 추후 결정 | ||
| private static final String SYSTEM_PROMPT = | ||
| "당신은 콘텐츠 검수 AI입니다. 입력된 텍스트에 욕설, 혐오 발언, 폭력적 표현, 성적 표현, 특정인을 향한 공격적 내용이 포함되어 있는지 판단하세요. " + | ||
| "문제가 있으면 'REJECT', 없으면 'APPROVE' 딱 한 단어만 응답하세요."; | ||
|
|
||
| private static final int MAX_ATTEMPTS = 2; | ||
| private static final int CONNECT_TIMEOUT_MS = 5000; | ||
| private static final int READ_TIMEOUT_MS = 10000; | ||
|
|
||
| private final PerspectiveRepository perspectiveRepository; | ||
|
|
||
| @Value("${openai.api-key}") | ||
| private String apiKey; | ||
|
|
||
| @Value("${openai.url}") | ||
| private String openaiUrl; | ||
|
|
||
| @Value("${openai.model}") | ||
| private String model; | ||
|
|
||
| @Async | ||
| public void moderate(UUID perspectiveId, String content) { | ||
| Exception lastException = null; | ||
| for (int attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) { | ||
| try { | ||
| String result = callGpt(content); | ||
| PerspectiveStatus newStatus = result.contains("APPROVE") | ||
| ? PerspectiveStatus.PUBLISHED | ||
| : PerspectiveStatus.REJECTED; | ||
|
|
||
| perspectiveRepository.findById(perspectiveId).ifPresent(p -> { | ||
| if (p.getStatus() == PerspectiveStatus.PENDING) { | ||
| if (newStatus == PerspectiveStatus.PUBLISHED) p.publish(); | ||
| else p.reject(); | ||
| perspectiveRepository.save(p); | ||
| } | ||
| }); | ||
| return; | ||
| } catch (Exception e) { | ||
| lastException = e; | ||
| if (attempt < MAX_ATTEMPTS) { | ||
| try { Thread.sleep(2000); } catch (InterruptedException ie) { | ||
| Thread.currentThread().interrupt(); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| log.error("GPT 검수 최종 실패 (재시도 소진). perspectiveId={}", perspectiveId, lastException); | ||
| perspectiveRepository.findById(perspectiveId).ifPresent(p -> { | ||
| if (p.getStatus() == PerspectiveStatus.PENDING) { | ||
| p.updateStatus(PerspectiveStatus.MODERATION_FAILED); | ||
| perspectiveRepository.save(p); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private String callGpt(String content) { | ||
| SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); | ||
| factory.setConnectTimeout(CONNECT_TIMEOUT_MS); | ||
| factory.setReadTimeout(READ_TIMEOUT_MS); | ||
| RestClient restClient = RestClient.builder().requestFactory(factory).build(); | ||
|
|
||
| Map<String, Object> requestBody = Map.of( | ||
| "model", model, | ||
| "messages", List.of( | ||
| Map.of("role", "system", "content", SYSTEM_PROMPT), | ||
| Map.of("role", "user", "content", content) | ||
| ), | ||
| "max_tokens", 10 | ||
| ); | ||
|
|
||
| Map response = restClient.post() | ||
| .uri(openaiUrl) | ||
| .header("Authorization", "Bearer " + apiKey) | ||
| .header("Content-Type", "application/json") | ||
| .body(requestBody) | ||
| .retrieve() | ||
| .body(Map.class); | ||
|
|
||
| List choices = (List) response.get("choices"); | ||
| Map choice = (Map) choices.get(0); | ||
| Map message = (Map) choice.get("message"); | ||
| return ((String) message.get("content")).trim().toUpperCase(); | ||
| } | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
요친구도 상수화 해서 관리하면 좋을 것 같아요!