Skip to content

Commit

Permalink
fix: if 문 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
osoohynn committed Nov 28, 2024
1 parent fd23a63 commit fb6ff17
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.project.oof.gpt.controller;

import com.project.oof.gpt.service.ChatGPTService;
import com.project.oof.storage.service.ChatHistoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
Expand All @@ -16,6 +17,7 @@
@RequiredArgsConstructor
@RequestMapping(value = "/api")
public class ChatGPTController {
private final ChatHistoryService chatHistoryService;

private final ChatGPTService chatGPTService;

Expand All @@ -25,5 +27,10 @@ public ResponseEntity<String> selectPrompt(@RequestBody String message) {
return new ResponseEntity<>(result, HttpStatus.OK);
}

@PostMapping("/clear")
public ResponseEntity<String> clearChatHistory() {
chatHistoryService.clearChatHistory(); // 대화 기록 초기화
return ResponseEntity.ok("Chat history cleared.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ public class ChatGPTServiceImpl implements ChatGPTService {

@Override
public String prompt(String userMessage) {
log.debug("[+] 신규 프롬프트를 수행합니다.");
System.out.println(chatHistoryService.getChatHistory());
String processedMessage;
if (chatHistoryService.getChatHistory().isEmpty()) {
processedMessage = "'" + userMessage + "' 를 번역해줘";
} else {
processedMessage = "좀만 다르게 번역해줘";
}

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

List<Map<String, String>> messages = chatHistoryService.getChatHistory();
Map<String, Object> requestBody = Map.of(
Expand Down Expand Up @@ -59,11 +63,9 @@ public String prompt(String userMessage) {
.map(message -> (String) message.get("content"))
.orElse("No response from assistant.");

System.out.println("Assistant response: " + assistantMessage);
chatHistoryService.addMessage("assistant", assistantMessage);
return assistantMessage;
} catch (Exception e) {
System.err.println("Error processing assistant response: " + e.getMessage());
return "An error occurred while processing the response.";
}
}
Expand Down

0 comments on commit fb6ff17

Please sign in to comment.