优化 json 格式处理
This commit is contained in:
@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jiayunet.ai.AiChatAbility;
|
||||
import org.jiayunet.ai.AiResponseCleanTool;
|
||||
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
||||
import org.jiayunet.mapper.AppJobDataMapper;
|
||||
import org.jiayunet.mapper.JobMapper;
|
||||
@@ -128,7 +129,7 @@ public class JobCleanService {
|
||||
|
||||
// 3. 解析JSON
|
||||
try {
|
||||
String json = cleanAiResponse(aiResponse);
|
||||
String json = AiResponseCleanTool.clean(aiResponse);
|
||||
JsonNode root = HttpTool.objectMapper.readTree(json);
|
||||
|
||||
// valid 校验
|
||||
@@ -221,7 +222,7 @@ public class JobCleanService {
|
||||
"\n\n【专业分类列表】\n" + dictCacheService.getMajorCategoryText();
|
||||
|
||||
String aiResponse = aiChatAbility.chat(systemPrompt, userMessage);
|
||||
String json = cleanAiResponse(aiResponse);
|
||||
String json = AiResponseCleanTool.clean(aiResponse);
|
||||
|
||||
try {
|
||||
JsonNode root = HttpTool.objectMapper.readTree(json);
|
||||
@@ -280,7 +281,7 @@ public class JobCleanService {
|
||||
String userMessage = "【岗位信息】\n标题: " + title + "\n职责: " + description + "\n要求: " + requirement;
|
||||
|
||||
String aiResponse = aiChatAbility.chat(systemPrompt, userMessage);
|
||||
String json = cleanAiResponse(aiResponse);
|
||||
String json = AiResponseCleanTool.clean(aiResponse);
|
||||
|
||||
try {
|
||||
JsonNode arrayNode = HttpTool.objectMapper.readTree(json);
|
||||
@@ -377,16 +378,6 @@ public class JobCleanService {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/** 清理AI返回的markdown代码块和控制字符 */
|
||||
private String cleanAiResponse(String response) {
|
||||
String json = response.trim();
|
||||
if (json.startsWith("```")) {
|
||||
json = json.replaceAll("^```\\w*\\n?", "").replaceAll("\\n?```$", "").trim();
|
||||
}
|
||||
json = json.replaceAll("[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]", "");
|
||||
return json;
|
||||
}
|
||||
|
||||
private String nullToEmpty(String s) {
|
||||
return s == null ? "" : s;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jiayunet.ai.AiChatAbility;
|
||||
import org.jiayunet.ai.AiResponseCleanTool;
|
||||
import org.jiayunet.mapper.*;
|
||||
import org.jiayunet.pojo.po.*;
|
||||
import org.jiayunet.pojo.vo.UserHonorsVo;
|
||||
@@ -145,7 +146,7 @@ public class UserProfileAnalyzeService {
|
||||
|
||||
String userMessage = "【用户简历】\n" + profileJson;
|
||||
String aiResponse = aiChatAbility.chat(systemPrompt, userMessage);
|
||||
String json = cleanAiResponse(aiResponse);
|
||||
String json = AiResponseCleanTool.clean(aiResponse);
|
||||
|
||||
try {
|
||||
JsonNode root = HttpTool.objectMapper.readTree(json);
|
||||
@@ -193,7 +194,7 @@ public class UserProfileAnalyzeService {
|
||||
String userMessage = "【用户简历】\n" + profileJson + "\n\n【专业分类列表】\n" + dictCacheService.getMajorCategoryText();
|
||||
|
||||
String aiResponse = aiChatAbility.chat(systemPrompt, userMessage);
|
||||
String json = cleanAiResponse(aiResponse);
|
||||
String json = AiResponseCleanTool.clean(aiResponse);
|
||||
|
||||
try {
|
||||
JsonNode root = HttpTool.objectMapper.readTree(json);
|
||||
@@ -253,7 +254,7 @@ public class UserProfileAnalyzeService {
|
||||
|
||||
String userMessage = "【用户简历】\n" + profileJson;
|
||||
String aiResponse = aiChatAbility.chat(systemPrompt, userMessage);
|
||||
String json = cleanAiResponse(aiResponse);
|
||||
String json = AiResponseCleanTool.clean(aiResponse);
|
||||
|
||||
try {
|
||||
JsonNode arrayNode = HttpTool.objectMapper.readTree(json);
|
||||
@@ -332,13 +333,4 @@ public class UserProfileAnalyzeService {
|
||||
relationMapper.batchInsert(relations);
|
||||
}
|
||||
|
||||
/** 清理AI返回的markdown代码块和控制字符 */
|
||||
private String cleanAiResponse(String response) {
|
||||
String json = response.trim();
|
||||
if (json.startsWith("```")) {
|
||||
json = json.replaceAll("^```\\w*\\n?", "").replaceAll("\\n?```$", "").trim();
|
||||
}
|
||||
json = json.replaceAll("[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]", "");
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user