Skip to content

Commit

Permalink
feat: gpt 모델별로 사용할 수 있음. 가공 x
Browse files Browse the repository at this point in the history
  • Loading branch information
osoohynn committed Nov 28, 2024
1 parent 21367b9 commit 744895f
Show file tree
Hide file tree
Showing 7 changed files with 407 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.project.oof.api.controller;

import com.project.oof.dto.ChatCompletionDto;
import com.project.oof.dto.CompletionDto;
import com.project.oof.service.ChatGPTService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
* ChatGPT API
*
* @author : lee
* @fileName : ChatGPTController
* @since : 12/29/23
*/
@Slf4j
@RestController
@RequestMapping(value = "/api/v1/chatGpt")
public class ChatGPTController {

private final ChatGPTService chatGPTService;

public ChatGPTController(ChatGPTService chatGPTService) {
this.chatGPTService = chatGPTService;
}

/**
* [API] ChatGPT 모델 리스트를 조회합니다.
*/
@GetMapping("/modelList")
public ResponseEntity<List<Map<String, Object>>> selectModelList() {
List<Map<String, Object>> result = chatGPTService.modelList();
return new ResponseEntity<>(result, HttpStatus.OK);
}

/**
* [API] ChatGPT 유효한 모델인지 조회합니다.
*
* @param modelName
* @return
*/
@GetMapping("/model")
public ResponseEntity<Map<String, Object>> isValidModel(@RequestParam(name = "modelName") String modelName) {
Map<String, Object> result = chatGPTService.isValidModel(modelName);
return new ResponseEntity<>(result, HttpStatus.OK);
}

/**
* [API] Legacy ChatGPT 프롬프트 명령을 수행합니다. : gpt-3.5-turbo-instruct, babbage-002, davinci-002
*
* @param completionDto {}
* @return ResponseEntity<Map < String, Object>>
*/
@PostMapping("/legacyPrompt")
public ResponseEntity<Map<String, Object>> selectLegacyPrompt(@RequestBody CompletionDto completionDto) {
log.debug("param :: " + completionDto.toString());
Map<String, Object> result = chatGPTService.legacyPrompt(completionDto);
return new ResponseEntity<>(result, HttpStatus.OK);
}

/**
* [API] 최신 ChatGPT 프롬프트 명령어를 수행합니다. : gpt-4, gpt-4 turbo, gpt-3.5-turbo
*
* @param chatCompletionDto
* @return
*/
@PostMapping("/prompt")
public ResponseEntity<Map<String, Object>> selectPrompt(@RequestBody ChatCompletionDto chatCompletionDto) {
log.debug("param :: " + chatCompletionDto.toString());
Map<String, Object> result = chatGPTService.prompt(chatCompletionDto);
return new ResponseEntity<>(result, HttpStatus.OK);
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/project/oof/config/ChatGPTConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.project.oof.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ChatGPTConfig {

@Value("${openai.secret-key}")
private String secretKey;

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}

@Bean
public HttpHeaders httpHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(secretKey);
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/project/oof/dto/ChatCompletionDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.project.oof.dto;

import com.project.oof.dto.request.ChatRequestMsgDto;
import lombok.*;

import java.util.List;

/**
* 새로운 모델에 대한 요청 객체를 관리합니다. : gpt-4, gpt-4 turbo, gpt-3.5-turbo
*
* @author : lee
* @fileName : ChatCompletionDto
* @since : 1/18/24
*/
@Getter
@ToString
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ChatCompletionDto {

// 사용할 모델
private String model;

private List<ChatRequestMsgDto> messages;

@Builder
public ChatCompletionDto(String model, List<ChatRequestMsgDto> messages) {
this.model = model;
this.messages = messages;
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/project/oof/dto/CompletionDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.project.oof.dto;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Getter
@ToString
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class CompletionDto {

// 사용할 모델
private String model;

// 사용할 프롬프트 명령어
private String prompt;

// 프롬프트의 다양성을 조절할 명령어(default : 1)
private float temperature = 1;

// 최대 사용할 토큰(default : 16)
private int max_tokens = 16;

@Builder
public CompletionDto(String model, String prompt, float temperature, int max_tokens) {
this.model = model;
this.prompt = prompt;
this.temperature = temperature;
this.max_tokens = max_tokens;
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/project/oof/dto/request/ChatRequestMsgDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.project.oof.dto.request;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Getter
@ToString
@NoArgsConstructor(access = AccessLevel.PROTECTED)

public class ChatRequestMsgDto {

private String role;

private String content;

@Builder
public ChatRequestMsgDto(String role, String content) {
this.role = role;
this.content = content;
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/project/oof/service/ChatGPTService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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);
}
Loading

0 comments on commit 744895f

Please sign in to comment.