-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
9 changed files
with
180 additions
and
73 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
11 changes: 11 additions & 0 deletions
11
src/main/java/corecord/dev/domain/chat/application/ChatAIService.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,11 @@ | ||
package corecord.dev.domain.chat.application; | ||
|
||
import corecord.dev.domain.chat.domain.entity.Chat; | ||
import corecord.dev.domain.chat.domain.dto.response.ChatSummaryAiResponse; | ||
|
||
import java.util.List; | ||
|
||
public interface ChatAIService { | ||
String generateChatResponse(List<Chat> chatHistory, String userContent); | ||
ChatSummaryAiResponse generateChatSummaryResponse(List<Chat> chatHistory); | ||
} |
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/corecord/dev/domain/chat/domain/converter/ChatConverter.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
8 changes: 4 additions & 4 deletions
8
...a/dto/response/ChatSummaryAiResponse.java → ...n/dto/response/ChatSummaryAiResponse.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
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
80 changes: 80 additions & 0 deletions
80
src/main/java/corecord/dev/domain/chat/infra/openai/application/OpenAiChatService.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,80 @@ | ||
package corecord.dev.domain.chat.infra.openai.application; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import corecord.dev.common.util.ResourceLoader; | ||
import corecord.dev.domain.chat.application.ChatAIService; | ||
import corecord.dev.domain.chat.domain.dto.response.ChatSummaryAiResponse; | ||
import corecord.dev.domain.chat.domain.entity.Chat; | ||
import corecord.dev.domain.chat.exception.ChatException; | ||
import corecord.dev.domain.chat.status.ChatErrorStatus; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.ai.openai.OpenAiChatModel; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Primary | ||
@Service | ||
@RequiredArgsConstructor | ||
public class OpenAiChatService implements ChatAIService { | ||
private final OpenAiChatModel chatModel; | ||
private static final String CHAT_SYSTEM_CONTENT = ResourceLoader.getResourceContent("chat-prompt.txt"); | ||
private static final String SUMMARY_SYSTEM_CONTENT = ResourceLoader.getResourceContent("chat-summary-prompt.txt"); | ||
|
||
@Override | ||
public String generateChatResponse(List<Chat> chatHistory, String userContent) { | ||
List<Map<String, String>> messages = new ArrayList<>(); | ||
|
||
// 시스템 메시지 추가 | ||
messages.add(Map.of( | ||
"role", "system", | ||
"content", CHAT_SYSTEM_CONTENT | ||
)); | ||
|
||
// 기존 채팅 내역 추가 | ||
for (Chat chat : chatHistory) { | ||
String role = chat.getAuthor() == 0 ? "assistant" : "user"; | ||
messages.add(Map.of("role", role, "content", chat.getContent())); | ||
} | ||
|
||
// 사용자 입력 추가 | ||
messages.add(Map.of("role", "user", "content", userContent)); | ||
|
||
return chatModel.call(String.valueOf(messages)); | ||
} | ||
|
||
@Override | ||
public ChatSummaryAiResponse generateChatSummaryResponse(List<Chat> chatHistory) { | ||
List<Map<String, String>> messages = new ArrayList<>(); | ||
|
||
// 시스템 메시지 추가 | ||
messages.add(Map.of( | ||
"role", "system", | ||
"content", SUMMARY_SYSTEM_CONTENT | ||
)); | ||
|
||
// 기존 채팅 내역 추가 | ||
for (Chat chat : chatHistory) { | ||
String role = chat.getAuthor() == 0 ? "assistant" : "user"; | ||
messages.add(Map.of("role", role, "content", chat.getContent())); | ||
} | ||
|
||
String response = chatModel.call(String.valueOf(messages)); | ||
|
||
return parseChatSummaryResponse(response); | ||
} | ||
|
||
private ChatSummaryAiResponse parseChatSummaryResponse(String aiResponse) { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
try { | ||
return objectMapper.readValue(aiResponse, ChatSummaryAiResponse.class); | ||
} catch (JsonProcessingException e) { | ||
throw new ChatException(ChatErrorStatus.INVALID_CHAT_SUMMARY); | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.