From 54bf566482176e143f1632f96697a906a19e8e4e Mon Sep 17 00:00:00 2001 From: PL <774412461@qq.com> Date: Fri, 30 Aug 2024 15:56:26 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E8=AF=BB=E5=8F=96=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E6=9B=B4=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/ChatMessageServiceImpl.java | 56 +++++++++++++++++-- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java b/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java index fbb5a98..ab66df3 100644 --- a/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java @@ -33,6 +33,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.*; @@ -309,18 +313,20 @@ public class ChatMessageServiceImpl implements ChatMessageService { //验证获取模型数据 异常问题 File file = null; try { - file = new File(ClassLoader.getSystemResource("Json/ChatModel.json").getPath()); //相对路径获取文件信息 - + chatModels = readResourceFile("Json/ChatModel.json"); + //String str = readResourceFile("Json/ChatModel.json"); + //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 { + /*try { //chatModel = readerMethod(file); chatModels = readerMethod(file); } catch (Exception e) { throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "读取文件并转换实体类" + Arrays.toString(e.getStackTrace())); - } + }*/ //循环查找对应的模型数据 for (ChatModelDto chatModelTemp : chatModels) { @@ -513,4 +519,46 @@ public class ChatMessageServiceImpl implements ChatMessageService { 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(); + } } From 436e4d807a7dd77b432a97e74146a2a9d7ccaa41 Mon Sep 17 00:00:00 2001 From: PL <774412461@qq.com> Date: Fri, 30 Aug 2024 16:45:39 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E8=AF=BB=E5=8F=96json=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E6=9B=B4=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/ChatMessageServiceImpl.java | 22 +++++++-- .../java/cn/teammodel/utils/FileUtil.java | 47 +++++++++++++++++++ 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java b/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java index ab66df3..aed57fa 100644 --- a/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java @@ -23,6 +23,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; @@ -313,13 +314,25 @@ public class ChatMessageServiceImpl implements ChatMessageService { //验证获取模型数据 异常问题 File file = null; try { - chatModels = readResourceFile("Json/ChatModel.json"); + String fileText = FileUtil.getFileText("Json/ChatModel.json"); + //转换问题 + try { + //String strData01 = JSON.toJSONString(fileText); + //获取聊天字段中的数据 + Object str = JSON.parseObject(fileText).get("chatModel"); + String strData1 = JSON.toJSONString(str); + //转换方式 + chatModels = JSON.parseObject(strData1, new TypeReference>() { }); + } catch (Exception e) { + throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "类型转换失败"); + } + + //chatModels = readResourceFile("Json/ChatModel.json"); //String str = readResourceFile("Json/ChatModel.json"); //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())); + throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "读取文件" + Arrays.toString(e.getStackTrace()) + e.getMessage()); } /*try { //chatModel = readerMethod(file); @@ -511,8 +524,7 @@ public class ChatMessageServiceImpl implements ChatMessageService { //转换问题 try { //转换方式 - chatModelDtos = JSON.parseObject(strData2, new TypeReference>() { - }); + chatModelDtos = JSON.parseObject(strData2, new TypeReference>() {}); } catch (Exception e) { throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "类型转换失败"); } 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(" ",""); + } } From 267facc601694cdc4d21074aabe9c7627481c1c4 Mon Sep 17 00:00:00 2001 From: PL <774412461@qq.com> Date: Fri, 30 Aug 2024 17:36:33 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=8E=92=E6=9F=A5=E8=AF=BB=E5=8F=96json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/ChatMessageServiceImpl.java | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java b/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java index aed57fa..40a8682 100644 --- a/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java @@ -312,30 +312,25 @@ public class ChatMessageServiceImpl implements ChatMessageService { ChatModelDto chatModel = null; //验证获取模型数据 异常问题 - File file = null; try { String fileText = FileUtil.getFileText("Json/ChatModel.json"); - //转换问题 - try { - //String strData01 = JSON.toJSONString(fileText); - //获取聊天字段中的数据 - Object str = JSON.parseObject(fileText).get("chatModel"); - String strData1 = JSON.toJSONString(str); - //转换方式 - chatModels = JSON.parseObject(strData1, new TypeReference>() { }); - } catch (Exception e) { - throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "类型转换失败"); - } + String jsonData = JSON.toJSONString(fileText); + //获取聊天字段中的数据 + Object obj = JSON.parseObject(jsonData).get("chatModel"); + String jsonData01 = JSON.toJSONString(obj); + //转换方式 + chatModels = JSON.parseObject(jsonData01, new TypeReference>() {}); - //chatModels = readResourceFile("Json/ChatModel.json"); - //String str = readResourceFile("Json/ChatModel.json"); - //file = new File(ClassLoader.getSystemResource("Json/ChatModel.json").getPath()); //相对路径获取文件信息 - //File file = new File("src/main/resources/Json/ChatModel.json"); //绝对路径获取文件信息 + 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 { - //chatModel = readerMethod(file); + //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())); @@ -352,8 +347,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; 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 4/4] =?UTF-8?q?=E5=B0=86=E6=AF=8F=E6=AC=A1=E8=AF=BB?= =?UTF-8?q?=E5=8F=96json=E6=95=B0=E6=8D=AE=E6=94=B9=E4=B8=BA=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E9=A1=B9=E7=9B=AE=E5=B0=B1=E8=AF=BB=E5=8F=96json?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=EF=BC=88=E8=8E=B7=E5=8F=96=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E6=95=B0=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(); - } }