-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
534 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -36,4 +36,6 @@ out/ | |
### VS Code ### | ||
.vscode/ | ||
|
||
.DS_Store | ||
.DS_Store | ||
|
||
/src/main/resources/static/docs |
This file contains 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 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/kusitms/backend/chatbot/application/ChatbotService.java
This file contains 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
22 changes: 22 additions & 0 deletions
22
src/main/java/kusitms/backend/chatbot/application/ClovaRequestFactory.java
This file contains 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,22 @@ | ||
package kusitms.backend.chatbot.application; | ||
|
||
import kusitms.backend.chatbot.dto.request.ClovaRequest; | ||
import kusitms.backend.chatbot.dto.request.Message; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.ArrayList; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class ClovaRequestFactory { | ||
|
||
private final MessageFactory messageFactory; | ||
|
||
public ClovaRequest createClovaRequest() { | ||
ArrayList<Message> messages = new ArrayList<>(); | ||
messages.add(messageFactory.createSystemMessage()); | ||
|
||
return new ClovaRequest(messages, 0.8, 0.3, 256, 5.0); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/kusitms/backend/chatbot/application/ClovaService.java
This file contains 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,24 @@ | ||
package kusitms.backend.chatbot.application; | ||
|
||
import kusitms.backend.chatbot.dto.request.ClovaRequest; | ||
import kusitms.backend.chatbot.dto.response.GetClovaChatbotAnswerResponse; | ||
import kusitms.backend.chatbot.infrastructure.ClovaApiClient; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ClovaService { | ||
private final ClovaApiClient clovaApiClient; | ||
private final ClovaRequestFactory clovaRequestFactory; | ||
private final MessageFactory messageFactory; | ||
|
||
// Clova 챗봇 답변을 가져오는 메서드 | ||
public GetClovaChatbotAnswerResponse getClovaChatbotAnswer(String message) { | ||
ClovaRequest request = clovaRequestFactory.createClovaRequest(); | ||
request.messages().add(messageFactory.createUserMessage(message)); | ||
String answer = clovaApiClient.requestClova(request); | ||
|
||
return GetClovaChatbotAnswerResponse.of(answer); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/kusitms/backend/chatbot/application/MessageFactory.java
This file contains 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,30 @@ | ||
package kusitms.backend.chatbot.application; | ||
|
||
import kusitms.backend.chatbot.domain.enums.Role; | ||
import kusitms.backend.chatbot.dto.request.Message; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Base64; | ||
|
||
@Component | ||
public class MessageFactory { | ||
|
||
@Value("${clova.prompt.baseball}") | ||
private String baseballPrompt; | ||
|
||
// 사용자 메시지 생성 | ||
public Message createUserMessage(String content) { | ||
return new Message(Role.user, content); | ||
} | ||
|
||
// 시스템 메시지 생성 | ||
public Message createSystemMessage() { | ||
return new Message(Role.system, new String(Base64.getDecoder().decode(baseballPrompt))); | ||
} | ||
|
||
// 어시스턴트 메시지 생성 | ||
public Message createAssistantMessage(String content) { | ||
return new Message(Role.assistant, content); | ||
} | ||
} |
This file contains 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 kusitms.backend.chatbot.domain.enums; | ||
|
||
public enum Role { | ||
system, // 시스템 | ||
user, // 사용자 | ||
assistant; // 어시스턴트 | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/kusitms/backend/chatbot/dto/request/ClovaRequest.java
This file contains 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,12 @@ | ||
package kusitms.backend.chatbot.dto.request; | ||
|
||
import java.util.ArrayList; | ||
|
||
public record ClovaRequest( | ||
ArrayList<Message> messages, | ||
double topP, | ||
double temperature, | ||
int maxTokens, | ||
double repeatPenalty | ||
) { | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/kusitms/backend/chatbot/dto/request/GetClovaChatbotAnswerRequest.java
This file contains 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 kusitms.backend.chatbot.dto.request; | ||
|
||
|
||
import jakarta.validation.constraints.NotBlank; | ||
|
||
public record GetClovaChatbotAnswerRequest( | ||
@NotBlank(message = "사용자 메세지는 빈 값일 수 없습니다.") String message | ||
) { | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/kusitms/backend/chatbot/dto/request/Message.java
This file contains 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 kusitms.backend.chatbot.dto.request; | ||
|
||
import kusitms.backend.chatbot.domain.enums.Role; | ||
|
||
public record Message( | ||
Role role, | ||
String content | ||
) { | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/kusitms/backend/chatbot/dto/response/ClovaChatbotAnswer.java
This file contains 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 kusitms.backend.chatbot.dto.response; | ||
|
||
public record ClovaChatbotAnswer( | ||
Result result | ||
) { | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/kusitms/backend/chatbot/dto/response/GetClovaChatbotAnswerResponse.java
This file contains 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 kusitms.backend.chatbot.dto.response; | ||
|
||
public record GetClovaChatbotAnswerResponse( | ||
String answer | ||
) { | ||
public static GetClovaChatbotAnswerResponse of(String answer) { | ||
return new GetClovaChatbotAnswerResponse(answer); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ot/dto/GetGuideChatbotAnswerResponse.java → ...sponse/GetGuideChatbotAnswerResponse.java
This file contains 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
15 changes: 15 additions & 0 deletions
15
src/main/java/kusitms/backend/chatbot/dto/response/Result.java
This file contains 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,15 @@ | ||
package kusitms.backend.chatbot.dto.response; | ||
|
||
import kusitms.backend.chatbot.dto.request.Message; | ||
|
||
import java.util.List; | ||
|
||
public record Result( | ||
Message message, // 대화 메시지 | ||
String stopReason, // 결과 중단 이유 | ||
int inputLength, // 입력 토큰 수 | ||
int outputLength, // 응답 토큰 수 | ||
int seed, // 입력 seed 값 | ||
List<String> aiFilter // AI 필터 결과 (여기선 단순히 리스트로 표현) | ||
) { | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/kusitms/backend/chatbot/infrastructure/ClovaApiClient.java
This file contains 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,24 @@ | ||
package kusitms.backend.chatbot.infrastructure; | ||
|
||
import kusitms.backend.chatbot.dto.request.ClovaRequest; | ||
import kusitms.backend.chatbot.dto.response.ClovaChatbotAnswer; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class ClovaApiClient { | ||
private final WebClient webClient; | ||
|
||
// 외부 CLOVA API와 통신하는 메서드 | ||
public String requestClova(ClovaRequest request) { | ||
ClovaChatbotAnswer clovaChatbotAnswer = webClient.post() | ||
.bodyValue(request) | ||
.retrieve() | ||
.bodyToMono(ClovaChatbotAnswer.class) | ||
.block(); | ||
|
||
return clovaChatbotAnswer.result().message().content(); | ||
} | ||
} |
Oops, something went wrong.