-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Feat: OpenAI API 연결 * Chore: CI/CD 구성 * Fix: 프로젝트 구조 설정의 오류 수정
- Loading branch information
Showing
16 changed files
with
130 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,5 @@ out/ | |
|
||
### VS Code ### | ||
.vscode/ | ||
.gitsecret/keys/random_seed | ||
!*.secret |
Empty file.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
FROM openjdk:17-alpine | ||
ARG JAR_PATH=/build/libs/*.jar | ||
COPY ${JAR_PATH} /home/server.jar | ||
ENTRYPOINT ["java","-jar","/home/server.jar"] | ||
ENTRYPOINT ["java","-jar","/home/server.jar"] | ||
ARG OPEN_AI_KEY | ||
ENV OPEN_AI_KEY=${OPEN_AI_KEY} |
Binary file modified
BIN
-48 Bytes
(93%)
build/classes/java/main/com/kuit/chatdiary/controller/UserController.class
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ spring: | |
driver-class-name: org.h2.Driver | ||
url: jdbc:h2:mem:test | ||
username: root | ||
password: | ||
password: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
spring: | ||
datasource: | ||
driver-class-name: com.mysql.cj.jdbc.Driver | ||
url: jdbc:mysql://localhost:3306/chatdiary?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul | ||
username: {PROD_DB_USER} | ||
password: {PROD_DB_PASSWORD} | ||
url: jdbc:mysql://${PROD_DB_HOST}:3306/chatdiary?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul | ||
username: ${PROD_DB_USER} | ||
password: ${PROD_DB_PASSWORD} | ||
jpa: | ||
hibernate: | ||
ddl-auto: none |
Binary file not shown.
27 changes: 27 additions & 0 deletions
27
src/main/java/com/kuit/chatdiary/controller/OpenAIController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.kuit.chatdiary.controller; | ||
|
||
import com.kuit.chatdiary.service.OpenAIService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.client.HttpClientErrorException; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class OpenAIController { | ||
|
||
@Autowired | ||
private final OpenAIService openAIService; | ||
|
||
@GetMapping("/gpt") | ||
public ResponseEntity<?> openAI() { | ||
try { | ||
return ResponseEntity.ok().body(openAIService.getCompletion("안녕")); | ||
} catch (HttpClientErrorException e) { | ||
return ResponseEntity.badRequest().body(e.getMessage()); | ||
} | ||
} | ||
|
||
|
||
} |
78 changes: 78 additions & 0 deletions
78
src/main/java/com/kuit/chatdiary/service/OpenAIService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.kuit.chatdiary.service; | ||
|
||
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.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.http.client.ClientHttpRequestInterceptor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestClientException; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
@Service | ||
public class OpenAIService { | ||
|
||
@Value("${OPEN_AI_KEY}") | ||
private String OPEN_AI_KEY; | ||
private final RestTemplate restTemplate = restTemplate(); | ||
|
||
public String getCompletion(String prompt) { | ||
String url = "https://api.openai.com/v1/chat/completions"; | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
headers.setBearerAuth(OPEN_AI_KEY); | ||
|
||
Map<String, Object> requestBody = new HashMap<>(); | ||
|
||
List<Map<String, String>> messages = new ArrayList<>(); | ||
Map<String, String> message = new HashMap<>(); | ||
message.put("role", "user"); | ||
message.put("content", prompt); | ||
messages.add(message); | ||
|
||
requestBody.put("messages", messages); | ||
requestBody.put("model", "gpt-3.5-turbo"); | ||
requestBody.put("max_tokens", 500); | ||
|
||
HttpEntity<Map<String, Object>> request = new HttpEntity<>(requestBody, headers); | ||
|
||
try { | ||
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class); | ||
return response.getBody(); | ||
} catch (RestClientException e) { | ||
throw new OpenAIException("AI API 호출 중 오류 발생함", e); | ||
} | ||
} | ||
|
||
public RestTemplate restTemplate() { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
|
||
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); | ||
interceptors.add((request, body, execution) -> { | ||
request.getHeaders().setContentType(MediaType.APPLICATION_JSON); | ||
request.getHeaders().setBearerAuth(OPEN_AI_KEY); | ||
return execution.execute(request, body); | ||
}); | ||
restTemplate.setInterceptors(interceptors); | ||
|
||
return restTemplate; | ||
} | ||
|
||
public class OpenAIException extends RestClientException { | ||
public OpenAIException(String message) { | ||
super(message); | ||
} | ||
|
||
public OpenAIException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ spring: | |
driver-class-name: org.h2.Driver | ||
url: jdbc:h2:mem:test | ||
username: root | ||
password: | ||
password: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
spring: | ||
datasource: | ||
driver-class-name: com.mysql.cj.jdbc.Driver | ||
url: jdbc:mysql://localhost:3306/chatdiary?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul | ||
username: {PROD_DB_USER} | ||
password: {PROD_DB_PASSWORD} | ||
url: jdbc:mysql://${PROD_DB_HOST}:3306/chatdiary?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul | ||
username: ${PROD_DB_USER} | ||
password: ${PROD_DB_PASSWORD} | ||
jpa: | ||
hibernate: | ||
ddl-auto: none |