|
|
@ -1,5 +1,6 @@
|
|
|
|
package cn.teammodel.service.impl;
|
|
|
|
package cn.teammodel.service.impl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.teammodel.ai.JsonLoader;
|
|
|
|
import cn.teammodel.ai.SparkGptClient;
|
|
|
|
import cn.teammodel.ai.SparkGptClient;
|
|
|
|
import cn.teammodel.ai.SseHelper;
|
|
|
|
import cn.teammodel.ai.SseHelper;
|
|
|
|
import cn.teammodel.ai.cache.HistoryCache;
|
|
|
|
import cn.teammodel.ai.cache.HistoryCache;
|
|
|
@ -23,6 +24,7 @@ import cn.teammodel.service.ChatMessageService;
|
|
|
|
import cn.teammodel.utils.FileUtil;
|
|
|
|
import cn.teammodel.utils.FileUtil;
|
|
|
|
import cn.teammodel.utils.RepositoryUtil;
|
|
|
|
import cn.teammodel.utils.RepositoryUtil;
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
|
import com.alibaba.fastjson2.TypeReference;
|
|
|
|
import com.alibaba.fastjson2.TypeReference;
|
|
|
|
import com.azure.cosmos.models.CosmosPatchOperations;
|
|
|
|
import com.azure.cosmos.models.CosmosPatchOperations;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
@ -33,6 +35,10 @@ import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
import java.io.*;
|
|
|
|
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.time.Instant;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
|
@ -50,6 +56,9 @@ public class ChatMessageServiceImpl implements ChatMessageService {
|
|
|
|
@Resource
|
|
|
|
@Resource
|
|
|
|
private ChatAppRepository chatAppRepository;
|
|
|
|
private ChatAppRepository chatAppRepository;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
|
|
|
private JsonLoader jsonLoader;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public SseEmitter chatCompletion(ChatCompletionReqDto chatCompletionReqDto, String userId) {
|
|
|
|
public SseEmitter chatCompletion(ChatCompletionReqDto chatCompletionReqDto, String userId) {
|
|
|
@ -305,22 +314,24 @@ public class ChatMessageServiceImpl implements ChatMessageService {
|
|
|
|
String strData = JSON.toJSONString(chatCommentsDto.getData());
|
|
|
|
String strData = JSON.toJSONString(chatCommentsDto.getData());
|
|
|
|
List<ChatModelDto> chatModels = new ArrayList<>();
|
|
|
|
List<ChatModelDto> chatModels = new ArrayList<>();
|
|
|
|
ChatModelDto chatModel = null;
|
|
|
|
ChatModelDto chatModel = null;
|
|
|
|
|
|
|
|
// 获取模型数据
|
|
|
|
|
|
|
|
chatModels = jsonLoader.myJsonDataBean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
//验证获取模型数据 异常问题
|
|
|
|
//验证获取模型数据 异常问题
|
|
|
|
File file = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
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);
|
|
|
|
String fileText = FileUtil.getFileText("Json/ChatModel.json");
|
|
|
|
chatModels = readerMethod(file);
|
|
|
|
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>>() {});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.info("获取地址fileText:"+fileText+"----文件内容Data:"+ jsonData +"----获取模型集合Object:" + obj +"----获取模型集合String:"+ jsonData01 +"----获取模型集合机构:"+chatModels);
|
|
|
|
} catch (Exception e) {
|
|
|
|
} 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) {
|
|
|
|
for (ChatModelDto chatModelTemp : chatModels) {
|
|
|
@ -333,8 +344,7 @@ public class ChatMessageServiceImpl implements ChatMessageService {
|
|
|
|
|
|
|
|
|
|
|
|
if (chatModel != null) {
|
|
|
|
if (chatModel != null) {
|
|
|
|
//角色条件
|
|
|
|
//角色条件
|
|
|
|
builder.append(String.format(chatModel.getRole(), chatCommentsDto.getPeriod(),
|
|
|
|
builder.append(String.format(chatModel.getRole(), chatCommentsDto.getPeriod(), chatCommentsDto.getSubject()));
|
|
|
|
chatCommentsDto.getSubject()));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ChatModelDto finalChatModel = chatModel;
|
|
|
|
ChatModelDto finalChatModel = chatModel;
|
|
|
@ -485,32 +495,4 @@ public class ChatMessageServiceImpl implements ChatMessageService {
|
|
|
|
|
|
|
|
|
|
|
|
return builder.toString();
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|