Skip to content

Commit

Permalink
feat: gpt 프롬프트 반환
Browse files Browse the repository at this point in the history
  • Loading branch information
osoohynn committed Nov 28, 2024
1 parent 04e0f77 commit 165cfe2
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.project.oof.api.controller;
package com.project.oof.gpt.controller;

import com.project.oof.dto.ChatCompletionDto;
import com.project.oof.service.ChatGPTService;
import com.project.oof.gpt.service.ChatGPTService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -10,8 +9,6 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@Slf4j
@RestController
Expand All @@ -25,9 +22,10 @@ public ChatGPTController(ChatGPTService chatGPTService) {
}

@PostMapping("/prompt")
public ResponseEntity<Map<String, Object>> selectPrompt(@RequestBody ChatCompletionDto chatCompletionDto) {
log.debug("param :: " + chatCompletionDto.toString());
Map<String, Object> result = chatGPTService.prompt(chatCompletionDto);
public ResponseEntity<String> selectPrompt(@RequestBody String message) {
String result = chatGPTService.prompt(message);
return new ResponseEntity<>(result, HttpStatus.OK);
}


}
10 changes: 10 additions & 0 deletions src/main/java/com/project/oof/gpt/service/ChatGPTService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.project.oof.gpt.service;

import org.springframework.stereotype.Service;


@Service
public interface ChatGPTService {

String prompt(String userMessage);
}
66 changes: 66 additions & 0 deletions src/main/java/com/project/oof/gpt/service/ChatGPTServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.project.oof.gpt.service;

import com.project.oof.gpt.config.ChatGPTConfig;
import com.project.oof.storage.service.ChatHistoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
@RequiredArgsConstructor
public class ChatGPTServiceImpl implements ChatGPTService {


private final ChatGPTConfig chatGPTConfig;
private final ChatHistoryService chatHistoryService;

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


@Override
public String prompt(String userMessage) {
log.debug("[+] 신규 프롬프트를 수행합니다.");
chatHistoryService.addMessage("user", userMessage);

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의 응답을 처리
Map<String, Object> responseBody = response.getBody();

List<Map<String, Object>> choices = (List<Map<String, Object>>) responseBody.get("choices");

Map<String, Object> firstChoice = choices.get(0);
Map<String, Object> message = (Map<String, Object>) firstChoice.get("message");

String assistantMessage = message.get("content").toString();

System.out.println("Assistant response: " + assistantMessage);
chatHistoryService.addMessage("assistant", assistantMessage);
return assistantMessage;
}

}

0 comments on commit 165cfe2

Please sign in to comment.