Merge remote-tracking branch 'origin/develop' into develop

develop
zhouj1203@hotmail.com 4 months ago
commit ec65450007

@ -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;
@ -23,6 +24,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;
@ -33,6 +35,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.*;
@ -50,6 +56,9 @@ public class ChatMessageServiceImpl implements ChatMessageService {
@Resource
private ChatAppRepository chatAppRepository;
@Resource
private JsonLoader jsonLoader;
@Override
public SseEmitter chatCompletion(ChatCompletionReqDto chatCompletionReqDto, String userId) {
@ -305,22 +314,24 @@ public class ChatMessageServiceImpl implements ChatMessageService {
String strData = JSON.toJSONString(chatCommentsDto.getData());
List<ChatModelDto> chatModels = new ArrayList<>();
ChatModelDto chatModel = null;
// 获取模型数据
chatModels = jsonLoader.myJsonDataBean();
/*
//验证获取模型数据 异常问题
File file = null;
try {
file = new File(ClassLoader.getSystemResource("Json/ChatModel.json").getPath()); //相对路径获取文件信息
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>>() {});
//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()));
}
try {
//chatModel = readerMethod(file);
chatModels = readerMethod(file);
} 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());
}*/
//循环查找对应的模型数据
for (ChatModelDto chatModelTemp : chatModels) {
@ -333,8 +344,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;
@ -485,32 +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;
}
}

@ -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 {
}
}
/**
* keyjson
* @param filepath
* @param key jsonkey
* @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(" ","");
}
}

Loading…
Cancel
Save