From d1c52e2bf63034a4a50e0fe99bffc643fd257198 Mon Sep 17 00:00:00 2001 From: Wonder Date: Thu, 6 Nov 2025 17:03:31 +0800 Subject: [PATCH] =?UTF-8?q?=20=E2=99=BB=EF=B8=8FRefactor:=20LLM=E8=BE=93?= =?UTF-8?q?=E5=87=BAJSON=E5=8F=AF=E7=94=A8=E6=80=A7=E6=A0=A1=E9=AA=8C=20?= =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E7=B3=BB=E7=BB=9F=E5=AE=89=E5=85=A8=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wonder/bi/manager/ChartAgentManager.java | 2 ++ .../java/com/wonder/bi/utils/JsonUtils.java | 21 +++++++++++++++++++ .../com/wonder/bi/utils/JsonUtilsTest.java | 15 +++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 Backend/src/main/java/com/wonder/bi/utils/JsonUtils.java create mode 100644 Backend/src/test/java/com/wonder/bi/utils/JsonUtilsTest.java diff --git a/Backend/src/main/java/com/wonder/bi/manager/ChartAgentManager.java b/Backend/src/main/java/com/wonder/bi/manager/ChartAgentManager.java index 06f100f..51260a6 100644 --- a/Backend/src/main/java/com/wonder/bi/manager/ChartAgentManager.java +++ b/Backend/src/main/java/com/wonder/bi/manager/ChartAgentManager.java @@ -1,6 +1,7 @@ package com.wonder.bi.manager; import com.wonder.bi.constant.CommonConstant; +import com.wonder.bi.utils.JsonUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.ai.chat.client.ChatClient; import org.springframework.stereotype.Component; @@ -23,6 +24,7 @@ public class ChartAgentManager { .call() .content(); log.info(content); + JsonUtils.isValidJson(content); return content; } } diff --git a/Backend/src/main/java/com/wonder/bi/utils/JsonUtils.java b/Backend/src/main/java/com/wonder/bi/utils/JsonUtils.java new file mode 100644 index 0000000..2d7b76e --- /dev/null +++ b/Backend/src/main/java/com/wonder/bi/utils/JsonUtils.java @@ -0,0 +1,21 @@ +package com.wonder.bi.utils; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.wonder.bi.common.ErrorCode; +import com.wonder.bi.exception.BusinessException; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class JsonUtils { + + public static boolean isValidJson(String input) { + ObjectMapper mapper = new ObjectMapper(); + try { + mapper.readTree(input); + return true; + } catch (JsonProcessingException e) { + throw new BusinessException(ErrorCode.OPERATION_ERROR, "LLM JSON 输出错误,请重试"); + } + } +} diff --git a/Backend/src/test/java/com/wonder/bi/utils/JsonUtilsTest.java b/Backend/src/test/java/com/wonder/bi/utils/JsonUtilsTest.java new file mode 100644 index 0000000..13e2069 --- /dev/null +++ b/Backend/src/test/java/com/wonder/bi/utils/JsonUtilsTest.java @@ -0,0 +1,15 @@ +package com.wonder.bi.utils; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class JsonUtilsTest { + + @Test + void isValidJson() { + boolean validJson = JsonUtils.isValidJson("{\"name\": \"John\", \"age\": 30, \"isStudent\": false}"); + assertTrue(validJson); + } +} \ No newline at end of file