|
|
|
@ -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<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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|