Skip to content

Commit

Permalink
feat: gpt answer list 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
osoohynn committed Nov 29, 2024
1 parent 46d36d4 commit 8f0f325
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
9 changes: 7 additions & 2 deletions src/main/java/com/project/oof/gpt/service/ChatGPTService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.project.oof.gpt.service;

import com.project.oof.gpt.dto.MessageDto;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public interface ChatGPTService {

String translateMessage(String userMessage);
MessageDto translateMessage(String userMessage);

MessageDto refreshResult();

String refreshResult();
List<String> getAnswers();
}
22 changes: 13 additions & 9 deletions src/main/java/com/project/oof/gpt/service/ChatGPTServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -27,21 +28,18 @@ public class ChatGPTServiceImpl implements ChatGPTService {


@Override
public String translateMessage(String userMessage) {
public MessageDto translateMessage(String userMessage) {
chatHistoryService.clearChatHistory();

String processedMessage = "'" + userMessage + "' 를 번역해줘";
// if (!chatHistoryService.getChatHistory().isEmpty()) {
// throw new RuntimeException("");
// }
String processedMessage = "'" + userMessage + "' 를 번역해줘, 다른 말 없이 결과만 말해줘";

chatHistoryService.addMessage("user", processedMessage);

return getAnswer();
}

@Override
public String refreshResult() {
public MessageDto refreshResult() {
if (chatHistoryService.getChatHistory().isEmpty()) {
throw new RuntimeException("입력된 데이터가 없습니다");
}
Expand All @@ -51,7 +49,7 @@ public String refreshResult() {
return getAnswer();
}

private String getAnswer() {
private MessageDto getAnswer() {
List<Map<String, String>> messages = chatHistoryService.getChatHistory();
Map<String, Object> requestBody = Map.of(
"model", "gpt-3.5-turbo",
Expand All @@ -75,9 +73,15 @@ private String getAnswer() {
.orElse("No response from assistant.");

chatHistoryService.addMessage("assistant", assistantMessage);
return assistantMessage;
chatHistoryService.answerHistory.add(assistantMessage);
return MessageDto.of(assistantMessage);
} catch (Exception e) {
return "An error occurred while processing the response.";
return MessageDto.of("An error occurred while processing the response.");
}
}

@Override
public List<String> getAnswers() {
return chatHistoryService.answerHistory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ public void addMessage(String role, String content) {
}

public List<Map<String, String>> getChatHistory() {
return new ArrayList<>(chatHistory); // 복사본 반환
return new ArrayList<>(chatHistory);
}

// 대화 기록 초기화 (옵션)
public void clearChatHistory() {
chatHistory.clear();
answerHistory.clear();
}

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

}

0 comments on commit 8f0f325

Please sign in to comment.