From 3db82228603dae1667f19d501c4ba066938af28d Mon Sep 17 00:00:00 2001 From: dmori Date: Tue, 3 Jun 2025 15:27:51 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20JSON=20=EC=9D=91=EB=8B=B5=20?= =?UTF-8?q?=EC=A0=95=EB=A6=AC=20=EC=9C=A0=ED=8B=B8=EB=A6=AC=ED=8B=B0=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EA=B0=95=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/ClaudeService.java | 28 +++++++++++++++---- src/main/resources/application.properties | 2 +- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/_1/spring_rest_api/service/ClaudeService.java b/src/main/java/com/_1/spring_rest_api/service/ClaudeService.java index 1c8f521..d00b1cd 100644 --- a/src/main/java/com/_1/spring_rest_api/service/ClaudeService.java +++ b/src/main/java/com/_1/spring_rest_api/service/ClaudeService.java @@ -223,14 +223,32 @@ private List generateQuestionsFromContent(String content, int minQu } private String cleanJsonResponse(String response) { - // ```json과 ``` 제거 + if (response == null || response.trim().isEmpty()) { + throw new IllegalArgumentException("AI 응답이 비어있습니다."); + } + String cleaned = response.trim(); - // 다양한 마크다운 패턴 처리 - cleaned = cleaned.replaceAll("^```(json)?\\s*", ""); // 시작 부분 - cleaned = cleaned.replaceAll("\\s*```$", ""); // 끝 부분 + // markdown 코드 블록 제거 (```json과 ```) + if (cleaned.startsWith("```json")) { + cleaned = cleaned.substring(7); // "```json" 제거 + } else if (cleaned.startsWith("```")) { + cleaned = cleaned.substring(3); // "```" 제거 + } + + if (cleaned.endsWith("```")) { + cleaned = cleaned.substring(0, cleaned.length() - 3); // 마지막 "```" 제거 + } + + // 앞뒤 공백 제거 + cleaned = cleaned.trim(); + + // JSON 유효성 기본 검증 + if (!cleaned.startsWith("[") && !cleaned.startsWith("{")) { + throw new IllegalArgumentException("유효하지 않은 JSON 형식입니다: " + cleaned.substring(0, Math.min(100, cleaned.length()))); + } - return cleaned.trim(); + return cleaned; } private String generateSummationByClaude(Long textId, String text) { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index c23707d..bdc552c 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,7 +2,7 @@ spring.application.name=spring-rest-api spring.profiles.include=secret # \u00EC\u0084\u009C\u00EB\u00B2\u0084 \u00EC\u0084\u00A4\u00EC\u00A0\u0095 -server.port=8080 +server.port=8081 # JPA \u00EC\u0084\u00A4\u00EC\u00A0\u0095 spring.jpa.hibernate.ddl-auto=update From fa28a46272d8af989542cd326b11b393af2ef24b Mon Sep 17 00:00:00 2001 From: dmori Date: Tue, 3 Jun 2025 15:30:43 +0900 Subject: [PATCH 2/4] =?UTF-8?q?fix:=20AI=20=EB=AA=A8=EB=8D=B8=EC=9D=98=20m?= =?UTF-8?q?arkdown=20=EA=B0=90=EC=8B=B8=EC=A7=84=20JSON=20=EC=9D=91?= =?UTF-8?q?=EB=8B=B5=20=EC=B2=98=EB=A6=AC=20=EC=98=A4=EB=A5=98=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/_1/spring_rest_api/service/ClaudeService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/_1/spring_rest_api/service/ClaudeService.java b/src/main/java/com/_1/spring_rest_api/service/ClaudeService.java index d00b1cd..4e45c01 100644 --- a/src/main/java/com/_1/spring_rest_api/service/ClaudeService.java +++ b/src/main/java/com/_1/spring_rest_api/service/ClaudeService.java @@ -214,7 +214,7 @@ private List generateQuestionsFromContent(String content, int minQu // JSON 파싱 return objectMapper.readValue( - responseContent, + cleanJsonString, new TypeReference>() {} ); } catch (Exception e) { From 82b1a7f21e5d190e9d615c83d12b6d9d00a30313 Mon Sep 17 00:00:00 2001 From: dmori Date: Tue, 3 Jun 2025 15:32:35 +0900 Subject: [PATCH 3/4] =?UTF-8?q?improve:=20=EC=88=9C=EC=88=98=20JSON=20?= =?UTF-8?q?=ED=98=95=EC=8B=9D=20=EC=9A=94=EC=B2=AD=EC=9D=84=20=EC=9C=84?= =?UTF-8?q?=ED=95=9C=20AI=20=ED=94=84=EB=A1=AC=ED=94=84=ED=8A=B8=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/_1/spring_rest_api/service/ClaudeService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/_1/spring_rest_api/service/ClaudeService.java b/src/main/java/com/_1/spring_rest_api/service/ClaudeService.java index 4e45c01..6a41157 100644 --- a/src/main/java/com/_1/spring_rest_api/service/ClaudeService.java +++ b/src/main/java/com/_1/spring_rest_api/service/ClaudeService.java @@ -195,6 +195,8 @@ private List generateQuestionsFromContent(String content, int minQu \\}> ... ] + 7. 응답은 반드시 순수한 JSON 배열 형태로만 제공해주세요. + 8. markdown 코드 블록이나 추가 설명 없이 JSON만 반환해주세요. """.formatted(minQuestionCount); Message systemMessage = new SystemMessage(systemPrompt); @@ -209,7 +211,6 @@ private List generateQuestionsFromContent(String content, int minQu ChatResponse response = chatModel.call(prompt); String responseContent = response.getResult().getOutput().getText(); - // 마크다운 코드 블록 제거 <- 이 부분 추가! String cleanJsonString = cleanJsonResponse(responseContent); // JSON 파싱 From b989de7794df85a95ff12fa59b2fe63d3e5f13c5 Mon Sep 17 00:00:00 2001 From: dmori Date: Tue, 3 Jun 2025 15:33:10 +0900 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20=EC=9E=98=EB=AA=BB=EB=90=9C=20?= =?UTF-8?q?=ED=8F=AC=ED=8A=B8=20=EB=B2=88=ED=98=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index bdc552c..c23707d 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,7 +2,7 @@ spring.application.name=spring-rest-api spring.profiles.include=secret # \u00EC\u0084\u009C\u00EB\u00B2\u0084 \u00EC\u0084\u00A4\u00EC\u00A0\u0095 -server.port=8081 +server.port=8080 # JPA \u00EC\u0084\u00A4\u00EC\u00A0\u0095 spring.jpa.hibernate.ddl-auto=update