|
|
|
@ -2,22 +2,41 @@ package com.supervision.service.impl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
import cn.hutool.core.io.IoUtil;
|
|
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
|
import com.supervision.model.GeBytearray;
|
|
|
|
|
import com.supervision.model.KgInfo;
|
|
|
|
|
import com.supervision.qanything.QanythingService;
|
|
|
|
|
import com.supervision.qanything.dto.ChatResult;
|
|
|
|
|
import com.supervision.qanything.dto.ResultWrapper;
|
|
|
|
|
import com.supervision.qanything.dto.SourceDTO;
|
|
|
|
|
import com.supervision.qanything.dto.UploadResult;
|
|
|
|
|
import com.supervision.service.GeBytearrayService;
|
|
|
|
|
import com.supervision.service.KGService;
|
|
|
|
|
import com.supervision.service.KgInfoService;
|
|
|
|
|
import com.supervision.vo.kg.ChatResVo;
|
|
|
|
|
import com.supervision.vo.kg.SourceKgInfo;
|
|
|
|
|
import com.supervision.vo.kg.UploadDocResVo;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Service
|
|
|
|
@ -31,25 +50,81 @@ public class KGServiceImpl implements KGService {
|
|
|
|
|
|
|
|
|
|
private final KgInfoService kgInfoService;
|
|
|
|
|
|
|
|
|
|
private final GeBytearrayService geBytearrayService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ChatResult chat(String question) {
|
|
|
|
|
public ChatResVo chat(String question) throws NoSuchAlgorithmException {
|
|
|
|
|
Assert.notEmpty(question, "问题不能为空");
|
|
|
|
|
|
|
|
|
|
ResultWrapper<ChatResult> chat = qanythingService.chat(question, CollUtil.newArrayList(kbId));
|
|
|
|
|
if (!chat.isSuccess()) {
|
|
|
|
|
log.info("Qanything 聊天失败,原因:{}",chat.getMsg());
|
|
|
|
|
return ChatResVo.makeError();
|
|
|
|
|
}
|
|
|
|
|
ChatResult result = chat.getResult();
|
|
|
|
|
if (Objects.isNull(result)){
|
|
|
|
|
log.info("Qanything 聊天结果为空");
|
|
|
|
|
return ChatResVo.makeError();
|
|
|
|
|
}
|
|
|
|
|
ChatResVo chatResVo = ChatResVo.makeSuccess(result.getResponse());
|
|
|
|
|
if (CollUtil.isEmpty(result.getSource())){
|
|
|
|
|
return chatResVo;
|
|
|
|
|
}
|
|
|
|
|
Set<String> qaFileId = result.getSource().stream().map(SourceDTO::getFileId).filter(Objects::nonNull).collect(Collectors.toSet());
|
|
|
|
|
if (CollUtil.isNotEmpty(qaFileId)) {
|
|
|
|
|
List<SourceKgInfo> sourceKgInfos = kgInfoService.lambdaQuery().in(KgInfo::getFileQaDocId, qaFileId).list()
|
|
|
|
|
.stream().map(SourceKgInfo::kgInfo2SourceKgInfo).collect(Collectors.toList());
|
|
|
|
|
chatResVo.setSourceKgInfoList(sourceKgInfos);
|
|
|
|
|
}
|
|
|
|
|
return chatResVo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public IPage<KgInfo> hotKG(Page<KgInfo> page) {
|
|
|
|
|
|
|
|
|
|
return kgInfoService.lambdaQuery().eq(KgInfo::getHotFlag,1).page(page);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void uploadDoc(KgInfo kgInfo,String kbId, File file) throws IOException {
|
|
|
|
|
|
|
|
|
|
// 保存到qanthing中
|
|
|
|
|
try {
|
|
|
|
|
ResultWrapper<ChatResult> chat = qanythingService.chat(question, CollUtil.newArrayList(kbId));
|
|
|
|
|
if (chat.isSuccess()){
|
|
|
|
|
return chat.getResult();
|
|
|
|
|
ResultWrapper<List<UploadResult>> resultWrapper = qanythingService.uploadDoc(kbId, file);
|
|
|
|
|
if (!resultWrapper.isSuccess() || CollUtil.isEmpty(resultWrapper.getResult())){
|
|
|
|
|
log.error("Qanything 上传失败,原因:{}",resultWrapper.getMsg());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
List<UploadResult> result = resultWrapper.getResult();
|
|
|
|
|
if (CollUtil.size(result)>1){
|
|
|
|
|
log.warn("Qanything 上传成功,但是返回结果不止一个,返回结果:{}",result);
|
|
|
|
|
}
|
|
|
|
|
kgInfo.setFileQaDocId(CollUtil.getFirst(result).getFileId());
|
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
|
log.error("Qanything 签名失败",e);
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
//保存到数据库中
|
|
|
|
|
GeBytearray geBytearray = new GeBytearray();
|
|
|
|
|
geBytearray.setFileName(file.getName());
|
|
|
|
|
geBytearray.setFileSize(file.length());
|
|
|
|
|
geBytearray.setContent(IoUtil.readBytes(Files.newInputStream(file.toPath())));
|
|
|
|
|
geBytearrayService.save(geBytearray);
|
|
|
|
|
|
|
|
|
|
// 保存文件信息
|
|
|
|
|
kgInfo.setFileByteId(geBytearray.getId());
|
|
|
|
|
kgInfoService.save(kgInfo);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public IPage<KgInfo> hotKG(Page<KgInfo> page) {
|
|
|
|
|
public UploadDocResVo uploadDoc(String kbId, MultipartFile multipartFile) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return kgInfoService.lambdaQuery().eq(KgInfo::getHotFlag,1).page(page);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|