Просмотр исходного кода

deepseek 品类解析json去除md格式

wangyunpeng 1 месяц назад
Родитель
Сommit
331d5d1bcc

+ 3 - 10
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recommend/ArticleCategoryService.java

@@ -30,6 +30,7 @@ import com.tzld.longarticle.recommend.server.repository.longArticle.ArticleCateg
 import com.tzld.longarticle.recommend.server.repository.longArticle.ArticleCrawlerPlanRepository;
 import com.tzld.longarticle.recommend.server.repository.longArticle.ArticlePoolPromotionSourceRepository;
 import com.tzld.longarticle.recommend.server.util.DateUtils;
+import com.tzld.longarticle.recommend.server.util.JSONUtils;
 import com.tzld.longarticle.recommend.server.util.Md5Util;
 import com.tzld.longarticle.recommend.server.util.TitleSimilarCheckUtil;
 import com.tzld.longarticle.recommend.server.util.page.Page;
@@ -119,11 +120,7 @@ public class ArticleCategoryService {
             long now = System.currentTimeMillis();
             JSONObject obj = null;
             if (kimiResult.isSuccess()) {
-                try {
-                    obj = JSONObject.parseObject(kimiResult.getResponse().getChoices().get(0).getMessage().getContent());
-                } catch (Exception e) {
-                    log.error(kimiResult.getResponse().getChoices().get(0).getMessage().getContent());
-                }
+                obj = JSONUtils.parseAIJsonContent(kimiResult.getResponse().getChoices().get(0).getMessage().getContent());
             }
             for (ArticleCategory articleCategory : partition) {
                 articleCategory.setKimiResult(kimiResult.getResponseStr().trim());
@@ -350,11 +347,7 @@ public class ArticleCategoryService {
             long now = System.currentTimeMillis();
             JSONObject obj = null;
             if (kimiResult.isSuccess()) {
-                try {
-                    obj = JSONObject.parseObject(kimiResult.getResponse().getChoices().get(0).getMessage().getContent());
-                } catch (Exception e) {
-                    log.error(kimiResult.getResponse().getChoices().get(0).getMessage().getContent());
-                }
+                obj = JSONUtils.parseAIJsonContent(kimiResult.getResponse().getChoices().get(0).getMessage().getContent());
             }
             articleCategory.setKimiResult(kimiResult.getResponseStr().trim());
             articleCategory.setUpdateTimestamp(now);

+ 39 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/JSONUtils.java

@@ -47,4 +47,43 @@ public class JSONUtils {
         return defaultValue;
     }
 
+    /**
+     * 解析AI返回的JSON内容,自动处理markdown代码块包裹的情况
+     * AI返回的内容可能被 ```json ... ``` 包裹,导致直接解析失败
+     */
+    public static JSONObject parseAIJsonContent(String content) {
+        if (StringUtils.isBlank(content)) {
+            return null;
+        }
+        // 先直接尝试解析
+        try {
+            return JSONObject.parseObject(content);
+        } catch (Exception e) {
+            log.warn("JSON直接解析失败,尝试清理markdown格式后重试, content=[{}]", content);
+        }
+        // 清理markdown代码块标记后重试
+        String cleaned = cleanMarkdownJsonBlock(content);
+        try {
+            return JSONObject.parseObject(cleaned);
+        } catch (Exception e) {
+            log.error("清理markdown格式后JSON解析仍然失败, cleaned=[{}]", cleaned, e);
+            return null;
+        }
+    }
+
+    /**
+     * 清理markdown代码块标记,如 ```json ... ```
+     */
+    public static String cleanMarkdownJsonBlock(String content) {
+        if (StringUtils.isBlank(content)) {
+            return content;
+        }
+        String cleaned = content.trim();
+        // 去除开头的 ```json 或 ```
+        cleaned = cleaned.replaceAll("^```(?:json)?\\s*", "");
+        // 去除结尾的 ```
+        cleaned = cleaned.replaceAll("\\s*```$", "");
+        return cleaned.trim();
+    }
+
 }