将每次读取json数据改为初始项目就读取json数据(获取模型数据)

develop
PL 4 months ago
parent 267facc601
commit 5c47b00105

@ -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
* SpringJSONSpring Bean使@PostConstructBeanJSON
*
*<-- pom.xml -->
*<dependency>
*<groupId>com.fasterxml.jackson.core</groupId>
*<artifactId>jackson-databind</artifactId>
*<version>2.13.1</version>
*</dependency>
*/
@Configuration
public class JsonLoader implements InitializingBean {
private List<ChatModelDto> 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<List<ChatModelDto>>() {});
}catch (Exception e){
throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "读取文件" + Arrays.toString(e.getStackTrace()) + e.getMessage());
}
}
/**
*
* @return
*/
@Bean
public List<ChatModelDto> myJsonDataBean(){
return chatModels;
}
@Override
public void afterPropertiesSet() throws Exception {
JsonLoader();
}
}

@ -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<ChatModelDto> 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<ChatModelDto> 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<ChatModelDto> chatModelDtos = new ArrayList<>();
//转换问题
try {
//转换方式
chatModelDtos = JSON.parseObject(strData2, new TypeReference<List<ChatModelDto>>() {});
} catch (Exception e) {
throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "类型转换失败");
}
return chatModelDtos;
}
/**
*
*
* @param fileName
* @return
*/
public static List<ChatModelDto> 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<ChatModelDto> chatModelDtos = new ArrayList<>();
//转换问题
try {
//转换方式
chatModelDtos = JSON.parseObject(strData2, new TypeReference<List<ChatModelDto>>() {
});
} catch (Exception e) {
throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "类型转换失败");
}
return chatModelDtos;
//return content.toString();
}
}

Loading…
Cancel
Save