From 4c3e767a3e6db0bab8d56053f245975c1ed93728 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=EC=9A=B0=ED=98=81?= Date: Sun, 5 Jan 2025 14:30:12 +0900 Subject: [PATCH 1/7] =?UTF-8?q?feat:=20Clova=20API=20=EC=97=B0=EB=8F=99?= =?UTF-8?q?=EC=9D=84=20=EC=9C=84=ED=95=9C=20yml=20=ED=99=98=EA=B2=BD?= =?UTF-8?q?=EB=B3=80=EC=88=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application-local.yml | 3 +++ src/main/resources/application.yml | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 3d835ab..028c95f 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -58,3 +58,6 @@ ncp: clova: invoke-url : ${INVOKE_URL} clova-secret-key : ${CLOVA_SECRET_KEY} + api-url : ${CLOVA_URL} + api-key : ${CLOVA_API_KEY} + gateway-key : ${CLOVA_GATEWAY_KEY} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 7d628b0..b0f5c79 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -53,6 +53,9 @@ ncp: clova: invoke-url : ${INVOKE_URL} clova-secret-key : ${CLOVA_SECRET_KEY} + api-url : ${CLOVA_URL} + api-key : ${CLOVA_API_KEY} + gateway-key : ${CLOVA_GATEWAY_KEY} server: port: 443 From bcc6e824793f32c805c3f714a686645c1c233d6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=EC=9A=B0=ED=98=81?= Date: Sun, 5 Jan 2025 14:30:39 +0900 Subject: [PATCH 2/7] =?UTF-8?q?feat:=20=EC=97=B0=EB=8F=99=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=EB=A5=BC=20=EC=9C=84=ED=95=9C=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC=20?= =?UTF-8?q?=EC=84=A4=EA=B3=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/controller/clova/ClovaController.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/phu/backend/controller/clova/ClovaController.java b/src/main/java/com/phu/backend/controller/clova/ClovaController.java index 887ad9b..2a237ff 100644 --- a/src/main/java/com/phu/backend/controller/clova/ClovaController.java +++ b/src/main/java/com/phu/backend/controller/clova/ClovaController.java @@ -1,23 +1,29 @@ package com.phu.backend.controller.clova; import com.fasterxml.jackson.core.JsonProcessingException; -import com.phu.backend.dto.clova.ClovaSpeechResponseList; +import com.phu.backend.dto.clova.reqeust.ClovaMessage; +import com.phu.backend.dto.clova.response.ClovaSpeechResponseList; import com.phu.backend.service.clova.ClovaService; import io.swagger.v3.oas.annotations.Operation; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @RestController @RequiredArgsConstructor public class ClovaController { private final ClovaService clovaService; + @GetMapping("/pt/clova/{member-id}/{file-id}") @Operation(summary = "음성메모 텍스트 추출", description = "음성메모의 텍스트를 분석해 추출한다.") public ResponseEntity uploadVoiceFile(@PathVariable(name = "file-id") Long fileId, @PathVariable(name = "member-id") Long memberId) throws JsonProcessingException { return ResponseEntity.ok().body(clovaService.clovaSpeech(fileId, memberId)); } + + @PostMapping("pt/clova/test") + @Operation(summary = "챗봇 테스팅", description = "테스트") + public ResponseEntity test(@RequestBody ClovaMessage request) throws JsonProcessingException { + return ResponseEntity.ok(clovaService.test(request)); + } } From fa2d686b6a6fd3eb9194c70fd53249adfe61a48b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=EC=9A=B0=ED=98=81?= Date: Sun, 5 Jan 2025 14:30:53 +0900 Subject: [PATCH 3/7] =?UTF-8?q?feat:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EC=8B=9C=20=EB=A9=94=EC=8B=9C=EC=A7=80=20=EC=A0=84=EC=86=A1?= =?UTF-8?q?=EC=9D=84=20=EC=9C=84=ED=95=9C=20dto=20=EC=84=A4=EA=B3=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/dto/clova/reqeust/ClovaMessage.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/main/java/com/phu/backend/dto/clova/reqeust/ClovaMessage.java diff --git a/src/main/java/com/phu/backend/dto/clova/reqeust/ClovaMessage.java b/src/main/java/com/phu/backend/dto/clova/reqeust/ClovaMessage.java new file mode 100644 index 0000000..628e9bb --- /dev/null +++ b/src/main/java/com/phu/backend/dto/clova/reqeust/ClovaMessage.java @@ -0,0 +1,15 @@ +package com.phu.backend.dto.clova.reqeust; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +public class ClovaMessage { + private String content; + @Builder + public ClovaMessage(String content, String role) { + this.content = content; + } +} From d475d05241ec99d27002a20abe3b2d647c69ae10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=EC=9A=B0=ED=98=81?= Date: Sun, 5 Jan 2025 14:31:07 +0900 Subject: [PATCH 4/7] =?UTF-8?q?feat:=20=ED=8C=A8=ED=82=A4=EC=A7=80=20?= =?UTF-8?q?=EA=B5=AC=EC=A1=B0=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../phu/backend/dto/clova/{ => reqeust}/ClovaSpeechRequest.java | 2 +- .../backend/dto/clova/{ => response}/ClovaSpeechResponse.java | 2 +- .../dto/clova/{ => response}/ClovaSpeechResponseList.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename src/main/java/com/phu/backend/dto/clova/{ => reqeust}/ClovaSpeechRequest.java (89%) rename src/main/java/com/phu/backend/dto/clova/{ => response}/ClovaSpeechResponse.java (88%) rename src/main/java/com/phu/backend/dto/clova/{ => response}/ClovaSpeechResponseList.java (94%) diff --git a/src/main/java/com/phu/backend/dto/clova/ClovaSpeechRequest.java b/src/main/java/com/phu/backend/dto/clova/reqeust/ClovaSpeechRequest.java similarity index 89% rename from src/main/java/com/phu/backend/dto/clova/ClovaSpeechRequest.java rename to src/main/java/com/phu/backend/dto/clova/reqeust/ClovaSpeechRequest.java index 4b0c172..780ac0f 100644 --- a/src/main/java/com/phu/backend/dto/clova/ClovaSpeechRequest.java +++ b/src/main/java/com/phu/backend/dto/clova/reqeust/ClovaSpeechRequest.java @@ -1,4 +1,4 @@ -package com.phu.backend.dto.clova; +package com.phu.backend.dto.clova.reqeust; import lombok.Builder; import lombok.Getter; diff --git a/src/main/java/com/phu/backend/dto/clova/ClovaSpeechResponse.java b/src/main/java/com/phu/backend/dto/clova/response/ClovaSpeechResponse.java similarity index 88% rename from src/main/java/com/phu/backend/dto/clova/ClovaSpeechResponse.java rename to src/main/java/com/phu/backend/dto/clova/response/ClovaSpeechResponse.java index a7077e3..ab562f6 100644 --- a/src/main/java/com/phu/backend/dto/clova/ClovaSpeechResponse.java +++ b/src/main/java/com/phu/backend/dto/clova/response/ClovaSpeechResponse.java @@ -1,4 +1,4 @@ -package com.phu.backend.dto.clova; +package com.phu.backend.dto.clova.response; import lombok.Builder; import lombok.Getter; diff --git a/src/main/java/com/phu/backend/dto/clova/ClovaSpeechResponseList.java b/src/main/java/com/phu/backend/dto/clova/response/ClovaSpeechResponseList.java similarity index 94% rename from src/main/java/com/phu/backend/dto/clova/ClovaSpeechResponseList.java rename to src/main/java/com/phu/backend/dto/clova/response/ClovaSpeechResponseList.java index 1df9c5c..f1d9b34 100644 --- a/src/main/java/com/phu/backend/dto/clova/ClovaSpeechResponseList.java +++ b/src/main/java/com/phu/backend/dto/clova/response/ClovaSpeechResponseList.java @@ -1,4 +1,4 @@ -package com.phu.backend.dto.clova; +package com.phu.backend.dto.clova.response; import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Builder; From 712d76b85a5490eef669e4f9ee2c4758dec0030e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=EC=9A=B0=ED=98=81?= Date: Sun, 5 Jan 2025 14:31:46 +0900 Subject: [PATCH 5/7] =?UTF-8?q?feat:=20=ED=81=B4=EB=A1=9C=EB=B0=94=20API?= =?UTF-8?q?=20=EC=97=B0=EB=8F=99=EC=9D=84=20=EC=9C=84=ED=95=9C=20dto=20?= =?UTF-8?q?=EC=84=A4=EA=B3=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/clova/reqeust/ClovaXRequest.java | 17 +++++++++++++++++ .../dto/clova/reqeust/ClovaXRequestList.java | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/main/java/com/phu/backend/dto/clova/reqeust/ClovaXRequest.java create mode 100644 src/main/java/com/phu/backend/dto/clova/reqeust/ClovaXRequestList.java diff --git a/src/main/java/com/phu/backend/dto/clova/reqeust/ClovaXRequest.java b/src/main/java/com/phu/backend/dto/clova/reqeust/ClovaXRequest.java new file mode 100644 index 0000000..ee58adf --- /dev/null +++ b/src/main/java/com/phu/backend/dto/clova/reqeust/ClovaXRequest.java @@ -0,0 +1,17 @@ +package com.phu.backend.dto.clova.reqeust; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +public class ClovaXRequest { + private String content; + private String role; + @Builder + public ClovaXRequest(String content, String role) { + this.content = content; + this.role = role; + } +} diff --git a/src/main/java/com/phu/backend/dto/clova/reqeust/ClovaXRequestList.java b/src/main/java/com/phu/backend/dto/clova/reqeust/ClovaXRequestList.java new file mode 100644 index 0000000..8b30a0e --- /dev/null +++ b/src/main/java/com/phu/backend/dto/clova/reqeust/ClovaXRequestList.java @@ -0,0 +1,17 @@ +package com.phu.backend.dto.clova.reqeust; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Getter +@NoArgsConstructor +public class ClovaXRequestList { + private List messages; + @Builder + public ClovaXRequestList(List messages) { + this.messages = messages; + } +} From ffe57e70aa77aa15099e7c5ffb6ef9d2da3e6b14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=EC=9A=B0=ED=98=81?= Date: Sun, 5 Jan 2025 14:32:50 +0900 Subject: [PATCH 6/7] =?UTF-8?q?feat:=20ClovaService=20Clova=20API=EB=A5=BC?= =?UTF-8?q?=20=EC=9C=84=ED=95=9C=20=EB=82=B4=EB=B6=80=20=EB=A1=9C=EC=A7=81?= =?UTF-8?q?=20=EC=84=A4=EA=B3=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../voicefile/VoiceFileController.java | 2 +- .../backend/service/clova/ClovaService.java | 56 ++++++++++++++++++- .../service/voicefile/VoiceFileService.java | 2 +- 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/phu/backend/controller/voicefile/VoiceFileController.java b/src/main/java/com/phu/backend/controller/voicefile/VoiceFileController.java index e0b8676..27f12ee 100644 --- a/src/main/java/com/phu/backend/controller/voicefile/VoiceFileController.java +++ b/src/main/java/com/phu/backend/controller/voicefile/VoiceFileController.java @@ -1,6 +1,6 @@ package com.phu.backend.controller.voicefile; -import com.phu.backend.dto.clova.ClovaSpeechResponseList; +import com.phu.backend.dto.clova.response.ClovaSpeechResponseList; import com.phu.backend.dto.voicefile.response.VoiceFileListResponse; import com.phu.backend.dto.voicefile.response.VoiceFileResponse; import com.phu.backend.service.voicefile.VoiceFileService; diff --git a/src/main/java/com/phu/backend/service/clova/ClovaService.java b/src/main/java/com/phu/backend/service/clova/ClovaService.java index 5e03a45..4dea8e5 100644 --- a/src/main/java/com/phu/backend/service/clova/ClovaService.java +++ b/src/main/java/com/phu/backend/service/clova/ClovaService.java @@ -6,9 +6,12 @@ import com.phu.backend.domain.clovavoicetext.ClovaVoiceText; import com.phu.backend.domain.member.Member; import com.phu.backend.domain.voicefile.VoiceFile; -import com.phu.backend.dto.clova.ClovaSpeechRequest; -import com.phu.backend.dto.clova.ClovaSpeechResponse; -import com.phu.backend.dto.clova.ClovaSpeechResponseList; +import com.phu.backend.dto.clova.reqeust.ClovaMessage; +import com.phu.backend.dto.clova.reqeust.ClovaSpeechRequest; +import com.phu.backend.dto.clova.reqeust.ClovaXRequestList; +import com.phu.backend.dto.clova.response.ClovaSpeechResponse; +import com.phu.backend.dto.clova.response.ClovaSpeechResponseList; +import com.phu.backend.dto.clova.reqeust.ClovaXRequest; import com.phu.backend.exception.dailychart.NotFoundChartMemberException; import com.phu.backend.exception.member.NotFoundMemberException; import com.phu.backend.exception.voicefile.NotFoundFileException; @@ -39,6 +42,7 @@ public class ClovaService { private final VoiceFileRepository voiceFileRepository; private final RestTemplate restTemplate; private final MemberRepository memberRepository; + private final ObjectMapper objectMapper; private final MemberListRepository memberListRepository; private final ClovaVoiceTextRepository clovaVoiceTextRepository; private final MemberService memberService; @@ -46,6 +50,12 @@ public class ClovaService { private String invokeUrl; @Value("${ncp.clova.clova-secret-key}") private String clovaSecret; + @Value("${ncp.clova.api-url}") + private String apiUrl; + @Value("${ncp.clova.api-key}") + private String apiKey; + @Value("${ncp.clova.gateway-key}") + private String gatewayKey; public ClovaSpeechResponseList clovaSpeech(Long fileId, Long memberId) throws JsonProcessingException { Member trainer = memberService.getMember(); @@ -140,5 +150,45 @@ private List parseClovaSpeechResponse(String voiceResponse, return responses; } + + public Object test(ClovaMessage request) throws JsonProcessingException { + List requestList = new ArrayList<>(); + + ClovaXRequest clovaXRequest = ClovaXRequest.builder() + .role("system") + .content(request.getContent()) + .build(); + requestList.add(clovaXRequest); + + ClovaXRequestList clovaXRequestList = ClovaXRequestList.builder() + .messages(requestList) + .build(); + + RestTemplate restTemplate = new RestTemplate(); + String url = apiUrl; + HttpHeaders headers = buildHeaders(); + String body = objectMapper.writeValueAsString(clovaXRequestList); + HttpEntity entity = new HttpEntity<>(body, headers); + + ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class); + + log.info("body:", entity); + + String responseBody = response.getBody(); + + JsonNode rootNode = objectMapper.readTree(responseBody); + String content = rootNode.path("result").path("message").path("content").asText(); + + return content; + } + + public HttpHeaders buildHeaders() { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + headers.set("X-NCP-CLOVASTUDIO-API-KEY", apiKey); + headers.set("X-NCP-APIGW-API-KEY", gatewayKey); + + return headers; + } } diff --git a/src/main/java/com/phu/backend/service/voicefile/VoiceFileService.java b/src/main/java/com/phu/backend/service/voicefile/VoiceFileService.java index cfb9399..f471fa6 100644 --- a/src/main/java/com/phu/backend/service/voicefile/VoiceFileService.java +++ b/src/main/java/com/phu/backend/service/voicefile/VoiceFileService.java @@ -8,7 +8,7 @@ import com.phu.backend.domain.member.Member; import com.phu.backend.domain.member.Part; import com.phu.backend.domain.voicefile.VoiceFile; -import com.phu.backend.dto.clova.ClovaSpeechResponseList; +import com.phu.backend.dto.clova.response.ClovaSpeechResponseList; import com.phu.backend.dto.voicefile.response.VoiceFileListResponse; import com.phu.backend.dto.voicefile.response.VoiceFileResponse; import com.phu.backend.exception.dailychart.NotFoundChartMemberException; From 50a477657932939f0e47a929797c242f5fe8433b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=EC=9A=B0=ED=98=81?= Date: Sun, 5 Jan 2025 14:33:16 +0900 Subject: [PATCH 7/7] =?UTF-8?q?feat:=20=ED=8C=A8=ED=82=A4=EC=A7=80=20?= =?UTF-8?q?=EA=B5=AC=EC=A1=B0=20=EB=B3=80=EA=B2=BD=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EC=9D=B8=ED=95=9C=20import=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/phu/backend/domain/clovavoicetext/ClovaVoiceText.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/phu/backend/domain/clovavoicetext/ClovaVoiceText.java b/src/main/java/com/phu/backend/domain/clovavoicetext/ClovaVoiceText.java index d16b069..723b6a1 100644 --- a/src/main/java/com/phu/backend/domain/clovavoicetext/ClovaVoiceText.java +++ b/src/main/java/com/phu/backend/domain/clovavoicetext/ClovaVoiceText.java @@ -1,6 +1,6 @@ package com.phu.backend.domain.clovavoicetext; -import com.phu.backend.dto.clova.ClovaSpeechResponse; +import com.phu.backend.dto.clova.response.ClovaSpeechResponse; import com.phu.backend.global.BaseTimeEntity; import lombok.AccessLevel; import lombok.Builder;