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 40a8682..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; @@ -55,6 +56,9 @@ public class ChatMessageServiceImpl implements ChatMessageService { @Resource private ChatAppRepository chatAppRepository; + @Resource + private JsonLoader jsonLoader; + @Override public SseEmitter chatCompletion(ChatCompletionReqDto chatCompletionReqDto, String userId) { @@ -310,7 +314,10 @@ public class ChatMessageServiceImpl implements ChatMessageService { String strData = JSON.toJSONString(chatCommentsDto.getData()); List chatModels = new ArrayList<>(); ChatModelDto chatModel = null; + // 获取模型数据 + chatModels = jsonLoader.myJsonDataBean(); + /* //验证获取模型数据 异常问题 try { String fileText = FileUtil.getFileText("Json/ChatModel.json"); @@ -324,16 +331,6 @@ public class ChatMessageServiceImpl implements ChatMessageService { 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()) + e.getMessage()); - } - - /*try { - //File file = null; - //chatModels = readResourceFile("Json/ChatModel.json"); - //file = new File(ClassLoader.getSystemResource("Json/ChatModel.json").getPath()); //相对路径获取文件信息 - //File file = new File("src/main/resources/Json/ChatModel.json"); //绝对路径获取文件信息 - chatModels = readerMethod(file); - } catch (Exception e) { - throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "读取文件并转换实体类" + Arrays.toString(e.getStackTrace())); }*/ //循环查找对应的模型数据 @@ -498,73 +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; - } - - /** - * 读取资源文件 并转换实体类 - * 读取文件信息并转换为智能对话模型数组对象 - * @param fileName - * @return - */ - public static List readResourceFile(String fileName) { - StringBuilder content = new StringBuilder(); - URL resourceUrl = ChatMessageServiceImpl.class.getClassLoader().getResource(fileName); - - if (resourceUrl != null) { - try { - String filePath = Paths.get(resourceUrl.toURI()).toString(); - content.append(new String(Files.readAllBytes(Paths.get(filePath)))); - } catch (IOException | IllegalArgumentException e) { - e.printStackTrace(); - } catch (URISyntaxException e) { - throw new RuntimeException(e); - } - } else { - System.out.println("Resource not found."); - } - - String strData = JSON.toJSONString(content); - //获取聊天字段中的数据 - 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; - //return content.toString(); - } }