Skip to content

Commit

Permalink
Feat: Chat-147-OpenAI API 연결 (#8)
Browse files Browse the repository at this point in the history
* Feat: OpenAI API 연결

* Chore: CI/CD 구성

* Fix: 프로젝트 구조 설정의 오류 수정
  • Loading branch information
Kjiw0n authored Jan 6, 2024
1 parent 5f30891 commit dd266c8
Show file tree
Hide file tree
Showing 16 changed files with 130 additions and 17 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ jobs:

- name: Build with Gradle
run: ./gradlew --warning-mode all build -i
env:
OPEN_AI_KEY: ${{ secrets.OPEN_AI_KEY }}

docker-build:
runs-on: ubuntu-latest
Expand All @@ -47,9 +49,9 @@ jobs:
- name: Docker Hub build & push
run: |
docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} -p ${{ secrets.DOCKER_HUB_PWD }}
docker build -t ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO_NAME }} .
docker build --build-arg OPEN_AI_KEY=${{ secrets.OPEN_AI_KEY }} -t chatdiary/chatdiary-image:latest .
docker images
docker push ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO_NAME }}
docker push chatdiary/chatdiary-image:latest
- name: deploy
uses: appleboy/ssh-action@master
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ out/

### VS Code ###
.vscode/
.gitsecret/keys/random_seed
!*.secret
Empty file added .gitsecret/paths/mapping.cfg
Empty file.
Binary file modified .gradle/8.5/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
4 changes: 3 additions & 1 deletion Dockerfile
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 not shown.
6 changes: 3 additions & 3 deletions build/resources/main/application-dev.yml
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: {DEV_DB_USER}
password: {DEV_DB_PASSWORD}
url: jdbc:mysql://${DEV_DB_HOST}:3306/chatdiary?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul
username: ${DEV_DB_USER}
password: ${DEV_DB_PASSWORD}
jpa:
hibernate:
ddl-auto: none
Expand Down
3 changes: 2 additions & 1 deletion build/resources/main/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ spring:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:test
username: root
password:
password:

6 changes: 3 additions & 3 deletions build/resources/main/application-prod.yml
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 modified build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
27 changes: 27 additions & 0 deletions src/main/java/com/kuit/chatdiary/controller/OpenAIController.java
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 src/main/java/com/kuit/chatdiary/service/OpenAIService.java
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);
}
}
}
6 changes: 3 additions & 3 deletions src/main/resources/application-dev.yml
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: {DEV_DB_USER}
password: {DEV_DB_PASSWORD}
url: jdbc:mysql://${DEV_DB_HOST}:3306/chatdiary?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul
username: ${DEV_DB_USER}
password: ${DEV_DB_PASSWORD}
jpa:
hibernate:
ddl-auto: none
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ spring:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:test
username: root
password:
password:

6 changes: 3 additions & 3 deletions src/main/resources/application-prod.yml
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

0 comments on commit dd266c8

Please sign in to comment.