Skip to content

Commit

Permalink
refactor: 안 쓰는 기능 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
osoohynn committed Nov 28, 2024
1 parent 75932e1 commit cfc101c
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 137 deletions.
15 changes: 0 additions & 15 deletions src/main/java/com/project/oof/service/ChatGPTService.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
package com.project.oof.service;

import com.project.oof.dto.ChatCompletionDto;
import com.project.oof.dto.CompletionDto;
import org.springframework.stereotype.Service;

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

/**
* ChatGPT 서비스 인터페이스
*
* @author : lee
* @fileName : ChatGPTService
* @since : 12/29/23
*/

@Service
public interface ChatGPTService {

List<Map<String, Object>> modelList();

Map<String, Object> isValidModel(String modelName);

Map<String, Object> legacyPrompt(CompletionDto completionDto);

Map<String, Object> prompt(ChatCompletionDto chatCompletionDto);
}
123 changes: 1 addition & 122 deletions src/main/java/com/project/oof/service/ChatGPTServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.project.oof.config.ChatGPTConfig;
import com.project.oof.dto.ChatCompletionDto;
import com.project.oof.dto.CompletionDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
Expand All @@ -16,7 +14,6 @@
import org.springframework.stereotype.Service;

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

@Slf4j
Expand All @@ -29,129 +26,10 @@ public ChatGPTServiceImpl(ChatGPTConfig chatGPTConfig) {
this.chatGPTConfig = chatGPTConfig;
}

@Value("${openai.url.model}")
private String modelUrl;

@Value("${openai.url.model-list}")
private String modelListUrl;

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

@Value("${openai.url.legacy-prompt}")
private String legacyPromptUrl;

/**
* 사용 가능한 모델 리스트를 조회하는 비즈니스 로직
*
* @return List<Map < String, Object>>
*/
@Override
public List<Map<String, Object>> modelList() {
log.debug("[+] 모델 리스트를 조회합니다.");
List<Map<String, Object>> resultList = null;

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

// [STEP2] 통신을 위한 RestTemplate을 구성합니다.
ResponseEntity<String> response = chatGPTConfig
.restTemplate()
.exchange(modelUrl, HttpMethod.GET, new HttpEntity<>(headers), String.class);
try {
// [STEP3] Jackson을 기반으로 응답값을 가져옵니다.
ObjectMapper om = new ObjectMapper();
Map<String, Object> data = om.readValue(response.getBody(), new TypeReference<>() {
});

// [STEP4] 응답 값을 결과값에 넣고 출력을 해봅니다.
resultList = (List<Map<String, Object>>) data.get("data");
for (Map<String, Object> object : resultList) {
log.debug("ID: " + object.get("id"));
log.debug("Object: " + object.get("object"));
log.debug("Created: " + object.get("created"));
log.debug("Owned By: " + object.get("owned_by"));
}
} catch (JsonMappingException e) {
log.debug("JsonMappingException :: " + e.getMessage());
} catch (JsonProcessingException e) {
log.debug("JsonProcessingException :: " + e.getMessage());
} catch (RuntimeException e) {
log.debug("RuntimeException :: " + e.getMessage());
}
return resultList;
}

/**
* 모델이 유효한지 확인하는 비즈니스 로직
*
* @param modelName {}
* @return Map<String, Object>
*/
@Override
public Map<String, Object> isValidModel(String modelName) {
log.debug("[+] 모델이 유효한지 조회합니다. 모델 : " + modelName);
Map<String, Object> result = new HashMap<>();

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

// [STEP2] 통신을 위한 RestTemplate을 구성합니다.
ResponseEntity<String> response = chatGPTConfig
.restTemplate()
.exchange(modelListUrl + "/" + modelName, HttpMethod.GET, new HttpEntity<>(headers), String.class);
try {
// [STEP3] Jackson을 기반으로 응답값을 가져옵니다.
ObjectMapper om = new ObjectMapper();
result = om.readValue(response.getBody(), new TypeReference<>() {
});
} catch (JsonProcessingException e) {
log.debug("JsonMappingException :: " + e.getMessage());
} catch (RuntimeException e) {
log.debug("RuntimeException :: " + e.getMessage());
}
return result;
}

/**
* ChatGTP 프롬프트 검색
*
* @param completionDto completionDto
* @return Map<String, Object>
*/
@Override
public Map<String, Object> legacyPrompt(CompletionDto completionDto) {
log.debug("[+] 레거시 프롬프트를 수행합니다.");

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

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

Map<String, Object> resultMap = new HashMap<>();
try {
ObjectMapper om = new ObjectMapper();
// [STEP6] String -> HashMap 역직렬화를 구성합니다.
resultMap = om.readValue(response.getBody(), new TypeReference<>() {
});
} catch (JsonProcessingException e) {
log.debug("JsonMappingException :: " + e.getMessage());
} catch (RuntimeException e) {
log.debug("RuntimeException :: " + e.getMessage());
}
return resultMap;
}

/**
* 신규 모델에 대한 프롬프트
*
* @param chatCompletionDto {}
* @return chatCompletionDto
*/
@Override
public Map<String, Object> prompt(ChatCompletionDto chatCompletionDto) {
log.debug("[+] 신규 프롬프트를 수행합니다.");
Expand All @@ -161,6 +39,7 @@ public Map<String, Object> prompt(ChatCompletionDto chatCompletionDto) {
// [STEP1] 토큰 정보가 포함된 Header를 가져옵니다.
HttpHeaders headers = chatGPTConfig.httpHeaders();


// [STEP5] 통신을 위한 RestTemplate을 구성합니다.
HttpEntity<ChatCompletionDto> requestEntity = new HttpEntity<>(chatCompletionDto, headers);
ResponseEntity<String> response = chatGPTConfig
Expand Down

0 comments on commit cfc101c

Please sign in to comment.