Skip to content

Commit

Permalink
feat: UUID 로 personal chat history 관리
Browse files Browse the repository at this point in the history
  • Loading branch information
osoohynn committed Nov 29, 2024
1 parent 511bf64 commit 5af3de8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;


@Slf4j
Expand All @@ -39,7 +38,7 @@ public ResponseEntity<MessageDto> refresh(@RequestBody RefreshRequest request) {
}

@GetMapping()
public ResponseEntity<List<String>> getAnswers() {
return new ResponseEntity<>(chatGPTService.getAnswers(), HttpStatus.OK);
public ResponseEntity<List<String>> getAnswers(@RequestBody RefreshRequest request) {
return new ResponseEntity<>(chatGPTService.getAnswers(request.userId()), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public interface ChatGPTService {

MessageDto refreshResult(UUID userId);

List<String> getAnswers();
List<String> getAnswers(UUID userId);
}
63 changes: 38 additions & 25 deletions src/main/java/com/project/oof/gpt/service/ChatGPTServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.project.oof.gpt.service;

import com.project.oof.gpt.config.ChatGPTConfig;
import com.project.oof.gpt.dto.MessageDto;
import com.project.oof.storage.service.ChatHistoryService;
import com.project.oof.gpt.dto.request.TranslateRequest;
import com.project.oof.gpt.dto.response.MessageDto;
import com.project.oof.user.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -14,6 +15,7 @@

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Service
Expand All @@ -22,69 +24,80 @@ public class ChatGPTServiceImpl implements ChatGPTService {

private final ChatGPTConfig chatGPTConfig;
private final ChatHistoryService chatHistoryService;
private final UserService userService;

@Value("${openai.url.prompt}")
private String promptUrl;


@Override
public MessageDto translateMessage(String userMessage) {
chatHistoryService.clearChatHistory();
public MessageDto translateMessage(TranslateRequest request) {
UUID userId = request.userId();

userService.addUser(userId);

ChatHistoryService personalPromptList = userService.getChatHistory(userId);
personalPromptList.clearChatHistory();

String processedMessage = "'" + userMessage + "' 를 번역해줘, 다른 말 없이 결과만 말해줘";
String userMessage = request.message();
String processedMessage = "'" + userMessage + "'를 번역해줘, 다른 말 없이 결과만 말해줘";

chatHistoryService.addMessage("user", processedMessage);
personalPromptList.addMessage("user", processedMessage);

return getAnswer();
return getAnswer(personalPromptList);
}

@Override
public MessageDto refreshResult() {
if (chatHistoryService.getChatHistory().isEmpty()) {
public MessageDto refreshResult(UUID userId) {
ChatHistoryService personalPromptList = userService.getChatHistory(userId);

if (personalPromptList.answerHistory.isEmpty()) {
throw new RuntimeException("입력된 데이터가 없습니다");
}

chatHistoryService.addMessage("user", "좀만 다르게 번역해줘");
personalPromptList.addMessage("user", "좀만 다르게 번역해줘, 역시나 다른 말 빼고 결과만");

return getAnswer();
return getAnswer(personalPromptList);
}

private MessageDto getAnswer() {
List<Map<String, String>> messages = chatHistoryService.getChatHistory();
private MessageDto getAnswer(ChatHistoryService personalPromptList) {

List<Map<String, String>> messages = personalPromptList.getChatHistory();

Map<String, Object> requestBody = Map.of(
"model", "gpt-3.5-turbo",
"messages", messages
);

HttpHeaders headers = chatGPTConfig.httpHeaders();

HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
ResponseEntity<Map> response = chatGPTConfig
.restTemplate()
.exchange(promptUrl, HttpMethod.POST, requestEntity, Map.class);

try {
ResponseEntity<Map> response = chatGPTConfig
.restTemplate()
.exchange(promptUrl, HttpMethod.POST, requestEntity, Map.class);

String assistantMessage = ((List<Map<String, Object>>) response.getBody().get("choices"))
.stream()
.findFirst()
.map(choice -> (Map<String, Object>) choice.get("message"))
.map(message -> (String) message.get("content"))
.map(content -> content.replaceAll("^\"|\"$", "")) // 문자열 양쪽의 따옴표 제거
.map(content -> content.replaceAll("^\"|\"$", ""))
.orElse("No response from assistant.");

// 대화 기록 저장
chatHistoryService.addMessage("assistant", assistantMessage);
chatHistoryService.answerHistory.add(assistantMessage);
personalPromptList.addMessage("assistant", assistantMessage);
personalPromptList.answerHistory.add(assistantMessage);

// 메시지 DTO 반환
return MessageDto.of(assistantMessage);

} catch (Exception e) {
return MessageDto.of("An error occurred while processing the response.");
}
}

@Override
public List<String> getAnswers() {
return chatHistoryService.answerHistory;
public List<String> getAnswers(UUID userId) {
ChatHistoryService personalPromptList = userService.getChatHistory(userId);

return personalPromptList.answerHistory;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.project.oof.storage.service;
package com.project.oof.gpt.service;

import org.springframework.stereotype.Service;

Expand All @@ -25,5 +25,4 @@ public void clearChatHistory() {
}

public List<String> answerHistory = new ArrayList<>();

}

0 comments on commit 5af3de8

Please sign in to comment.