diff --git a/src/main/java/cn/teammodel/ai/JsonLoader.java b/src/main/java/cn/teammodel/ai/JsonLoader.java new file mode 100644 index 0000000..c8a1307 --- /dev/null +++ b/src/main/java/cn/teammodel/ai/JsonLoader.java @@ -0,0 +1,73 @@ +package cn.teammodel.ai; + +import cn.teammodel.common.ErrorCode; +import cn.teammodel.config.exception.ServiceException; +import cn.teammodel.model.dto.ai.ChatModelDto; +import cn.teammodel.utils.FileUtil; +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.TypeReference; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import javax.annotation.PostConstruct; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * 读取json文件的聊天模型 数据 + * 使用单例模式 + * + * 项目启动时就加载json文件,Spring Bean + * 要在Spring项目启动时就加载JSON文件,并将其作为Spring Bean进行管理,你可以使用@PostConstruct注解的方法来在Bean初始化后加载JSON文件 + * + *<-- 添加到pom.xml中 --> + * + *com.fasterxml.jackson.core + *jackson-databind + *2.13.1 + * + */ +@Configuration +public class JsonLoader implements InitializingBean { + + private List chatModels = new ArrayList<>(); + + private void JsonLoader() { + // 私有构造函数防止实例化 + } + + /** + * 读取json文件 + */ + @PostConstruct + public void loadJsonData(){ + try{ + + String fileText = FileUtil.getFileText("Json/ChatModel.json"); + String jsonData = JSON.toJSONString(fileText); + //获取聊天字段中的数据 + Object obj = JSON.parseObject(jsonData).get("chatModel"); + String jsonData01 = JSON.toJSONString(obj); + //转换方式 + chatModels = JSON.parseObject(jsonData01, new TypeReference>() {}); + }catch (Exception e){ + throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "读取文件" + Arrays.toString(e.getStackTrace()) + e.getMessage()); + } + } + + /** + * 返回聊天模型数据 + * @return + */ + @Bean + public List myJsonDataBean(){ + return chatModels; + } + + @Override + public void afterPropertiesSet() throws Exception { + JsonLoader(); + } +} diff --git a/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java b/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java index fbb5a98..bf80ea0 100644 --- a/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java @@ -1,5 +1,6 @@ package cn.teammodel.service.impl; +import cn.teammodel.ai.JsonLoader; import cn.teammodel.ai.SparkGptClient; import cn.teammodel.ai.SseHelper; import cn.teammodel.ai.cache.HistoryCache; @@ -23,6 +24,7 @@ import cn.teammodel.service.ChatMessageService; import cn.teammodel.utils.FileUtil; import cn.teammodel.utils.RepositoryUtil; import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.TypeReference; import com.azure.cosmos.models.CosmosPatchOperations; import lombok.extern.slf4j.Slf4j; @@ -33,6 +35,10 @@ import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import javax.annotation.Resource; import java.io.*; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; import java.time.Instant; import java.util.*; @@ -50,6 +56,9 @@ public class ChatMessageServiceImpl implements ChatMessageService { @Resource private ChatAppRepository chatAppRepository; + @Resource + private JsonLoader jsonLoader; + @Override public SseEmitter chatCompletion(ChatCompletionReqDto chatCompletionReqDto, String userId) { @@ -305,22 +314,24 @@ public class ChatMessageServiceImpl implements ChatMessageService { String strData = JSON.toJSONString(chatCommentsDto.getData()); List chatModels = new ArrayList<>(); ChatModelDto chatModel = null; + // 获取模型数据 + chatModels = jsonLoader.myJsonDataBean(); + /* //验证获取模型数据 异常问题 - File file = null; - try { - file = new File(ClassLoader.getSystemResource("Json/ChatModel.json").getPath()); //相对路径获取文件信息 - - //File file = new File("src/main/resources/Json/ChatModel.json"); //绝对路径获取文件信息 - } catch (Exception e) { - throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "读取文件" + Arrays.toString(e.getStackTrace())); - } try { - //chatModel = readerMethod(file); - chatModels = readerMethod(file); + String fileText = FileUtil.getFileText("Json/ChatModel.json"); + String jsonData = JSON.toJSONString(fileText); + //获取聊天字段中的数据 + Object obj = JSON.parseObject(jsonData).get("chatModel"); + String jsonData01 = JSON.toJSONString(obj); + //转换方式 + chatModels = JSON.parseObject(jsonData01, new TypeReference>() {}); + + log.info("获取地址fileText:"+fileText+"----文件内容Data:"+ jsonData +"----获取模型集合Object:" + obj +"----获取模型集合String:"+ jsonData01 +"----获取模型集合机构:"+chatModels); } catch (Exception e) { - throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "读取文件并转换实体类" + Arrays.toString(e.getStackTrace())); - } + throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "读取文件" + Arrays.toString(e.getStackTrace()) + e.getMessage()); + }*/ //循环查找对应的模型数据 for (ChatModelDto chatModelTemp : chatModels) { @@ -333,8 +344,7 @@ public class ChatMessageServiceImpl implements ChatMessageService { if (chatModel != null) { //角色条件 - builder.append(String.format(chatModel.getRole(), chatCommentsDto.getPeriod(), - chatCommentsDto.getSubject())); + builder.append(String.format(chatModel.getRole(), chatCommentsDto.getPeriod(), chatCommentsDto.getSubject())); } ChatModelDto finalChatModel = chatModel; @@ -485,32 +495,4 @@ public class ChatMessageServiceImpl implements ChatMessageService { return builder.toString(); } - - /** - * 读取文件信息并转换为智能对话模型数组对象 - * - * @param file - * @return - */ - private static List readerMethod(File file) { - //读取文件信息,并返回string字符串 ,并改成json格式 - String fileTxt = FileUtil.readFile(file); - String strData = JSON.toJSONString(fileTxt); - - //获取聊天字段中的数据 - Object str = JSON.parseObject(strData).get("chatModel"); - String strData2 = JSON.toJSONString(str); - List chatModelDtos = new ArrayList<>(); - - //转换问题 - try { - //转换方式 - chatModelDtos = JSON.parseObject(strData2, new TypeReference>() { - }); - } catch (Exception e) { - throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "类型转换失败"); - } - - return chatModelDtos; - } } diff --git a/src/main/java/cn/teammodel/utils/FileUtil.java b/src/main/java/cn/teammodel/utils/FileUtil.java index 0174172..383c172 100644 --- a/src/main/java/cn/teammodel/utils/FileUtil.java +++ b/src/main/java/cn/teammodel/utils/FileUtil.java @@ -7,6 +7,12 @@ import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import com.alibaba.fastjson.JSONObject; +import org.apache.commons.lang3.StringUtils; +import org.springframework.core.io.ClassPathResource; +import java.io.InputStreamReader; +import java.io.Reader; + public class FileUtil { /** @@ -32,4 +38,45 @@ public class FileUtil { } } + /** + * 根据key获取json + * @param filepath 文件路径 + * @param key json的key + * @return + */ + public static Object getJSONByKey(String filepath, String key){ + String text = FileUtil.getFileText(filepath); + JSONObject object = new JSONObject(); + if (StringUtils.isNoneBlank(text)){ + object = parseObject(text); + } + return object.get(key); + } + + /** + * 将json 字符串转换为json对象 + */ + public static JSONObject parseObject(String text) { + return JSONObject.parseObject(text); + } + + /** + * 获取文件内文本 + * @param filepath + * @return + */ + public static String getFileText(String filepath){ + StringBuffer sb = new StringBuffer(); + try{ + ClassPathResource classPathResource = new ClassPathResource(filepath); + Reader reader = new InputStreamReader(classPathResource.getInputStream()); + int ch = 0; + while ((ch = reader.read())!=-1){ + sb.append((char)ch); + } + }catch (Exception e) { + e.printStackTrace(); + } + return sb.toString().replaceAll("\r\n","").replaceAll(" ",""); + } }