Skip to content

Commit

Permalink
fix: translate, refresh 기능 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
osoohynn committed Nov 29, 2024
1 parent fb6ff17 commit e1a187f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ public class ChatGPTController {

private final ChatGPTService chatGPTService;

@PostMapping("/prompt")
@PostMapping("/translate")
public ResponseEntity<String> selectPrompt(@RequestBody String message) {
String result = chatGPTService.prompt(message);
String result = chatGPTService.translateMessage(message);
return new ResponseEntity<>(result, HttpStatus.OK);
}

@PostMapping("/refresh")
public ResponseEntity<String> refresh() {
String result = chatGPTService.refreshResult();
return new ResponseEntity<>(result, HttpStatus.OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
@Service
public interface ChatGPTService {

String prompt(String userMessage);
String translateMessage(String userMessage);

String refreshResult();
}
34 changes: 22 additions & 12 deletions src/main/java/com/project/oof/gpt/service/ChatGPTServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,44 @@ public class ChatGPTServiceImpl implements ChatGPTService {


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

String processedMessage = "'" + userMessage + "' 를 번역해줘";
// if (!chatHistoryService.getChatHistory().isEmpty()) {
// throw new RuntimeException("");
// }

chatHistoryService.addMessage("user", processedMessage);

return getAnswer();
}

@Override
public String refreshResult() {
if (chatHistoryService.getChatHistory().isEmpty()) {
processedMessage = "'" + userMessage + "' 를 번역해줘";
} else {
processedMessage = "좀만 다르게 번역해줘";
throw new RuntimeException("입력된 데이터가 없습니다");
}

chatHistoryService.addMessage("user", processedMessage);
chatHistoryService.addMessage("user", "좀만 다르게 번역해줘");

return getAnswer();
}

private String getAnswer() {
List<Map<String, String>> messages = chatHistoryService.getChatHistory();
Map<String, Object> requestBody = Map.of(
"model", "gpt-3.5-turbo",
"messages", messages
);

// [STEP1] 토큰 정보가 포함된 Header를 가져옵니다.
HttpHeaders headers = chatGPTConfig.httpHeaders();


// [STEP5] 통신을 위한 RestTemplate을 구성합니다.
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
ResponseEntity<Map> response = chatGPTConfig
.restTemplate()
.exchange(promptUrl, HttpMethod.POST, requestEntity, Map.class);

// OpenAI API의 응답을 처리
try {
Map<String, Object> responseBody = response.getBody();
String assistantMessage = ((List<Map<String, Object>>) responseBody.get("choices"))
Expand All @@ -69,5 +80,4 @@ public String prompt(String userMessage) {
return "An error occurred while processing the response.";
}
}

}
}

0 comments on commit e1a187f

Please sign in to comment.