From 5c47b0010588dd02cc04d045f96de2ca8f5c1a74 Mon Sep 17 00:00:00 2001 From: PL <774412461@qq.com> Date: Tue, 3 Sep 2024 10:10:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E6=AF=8F=E6=AC=A1=E8=AF=BB=E5=8F=96js?= =?UTF-8?q?on=E6=95=B0=E6=8D=AE=E6=94=B9=E4=B8=BA=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E5=B0=B1=E8=AF=BB=E5=8F=96json=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=EF=BC=88=E8=8E=B7=E5=8F=96=E6=A8=A1=E5=9E=8B=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/cn/teammodel/ai/JsonLoader.java | 73 ++++++++++++++++ .../service/impl/ChatMessageServiceImpl.java | 86 ++----------------- 2 files changed, 80 insertions(+), 79 deletions(-) create mode 100644 src/main/java/cn/teammodel/ai/JsonLoader.java 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(); - } }