|
|
|
@ -4,6 +4,7 @@ package com.supervision.service.impl;
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
import cn.hutool.core.io.IoUtil;
|
|
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
|
import com.supervision.model.GeBytearray;
|
|
|
|
@ -22,20 +23,15 @@ 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.*;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
@ -57,7 +53,8 @@ public class KGServiceImpl implements KGService {
|
|
|
|
|
public ChatResVo chat(String question) throws NoSuchAlgorithmException {
|
|
|
|
|
Assert.notEmpty(question, "问题不能为空");
|
|
|
|
|
|
|
|
|
|
ResultWrapper<ChatResult> chat = qanythingService.chat(question, CollUtil.newArrayList(kbId));
|
|
|
|
|
ArrayList<String> kbIds = CollUtil.newArrayList("KB6ada48988e694ad197af53cc0d1e4b78_240328", "KB403a6543629648a3a74b60be5707398f_240328");
|
|
|
|
|
ResultWrapper<ChatResult> chat = qanythingService.chat(question, kbIds);
|
|
|
|
|
if (!chat.isSuccess()) {
|
|
|
|
|
log.info("Qanything 聊天失败,原因:{}",chat.getMsg());
|
|
|
|
|
return ChatResVo.makeError();
|
|
|
|
@ -71,13 +68,30 @@ public class KGServiceImpl implements KGService {
|
|
|
|
|
if (CollUtil.isEmpty(result.getSource())){
|
|
|
|
|
return chatResVo;
|
|
|
|
|
}
|
|
|
|
|
Set<String> qaFileId = result.getSource().stream().map(SourceDTO::getFileId).filter(Objects::nonNull).collect(Collectors.toSet());
|
|
|
|
|
chatResVo.setSourceKgInfoList(qaSource2SourceKgInfo(result.getSource()));
|
|
|
|
|
return chatResVo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<SourceKgInfo> qaSource2SourceKgInfo(List<SourceDTO> sourceDTOList){
|
|
|
|
|
List<SourceKgInfo> sourceKgInfoList = CollUtil.newArrayList();
|
|
|
|
|
if (CollUtil.isEmpty(sourceDTOList)){
|
|
|
|
|
return sourceKgInfoList;
|
|
|
|
|
}
|
|
|
|
|
Set<String> qaFileId = sourceDTOList.stream().map(SourceDTO::getFileId).filter(Objects::nonNull).collect(Collectors.toSet());
|
|
|
|
|
Set<String> preferFileIds = new HashSet<>();
|
|
|
|
|
if (CollUtil.isNotEmpty(qaFileId)) {
|
|
|
|
|
List<SourceKgInfo> sourceKgInfos = kgInfoService.lambdaQuery().in(KgInfo::getFileQaDocId, qaFileId).list()
|
|
|
|
|
.stream().map(SourceKgInfo::kgInfo2SourceKgInfo).collect(Collectors.toList());
|
|
|
|
|
chatResVo.setSourceKgInfoList(sourceKgInfos);
|
|
|
|
|
// 优先使用数据库中配置的信息
|
|
|
|
|
List<KgInfo> preferKgInfoList = kgInfoService.lambdaQuery().in(KgInfo::getFileQaDocId, qaFileId).list();
|
|
|
|
|
List<SourceKgInfo> sourceKgInfos = preferKgInfoList.stream().peek(kgInfo->preferFileIds.add(kgInfo.getFileQaDocId()))
|
|
|
|
|
.map(SourceKgInfo::kgInfo2SourceKgInfo)
|
|
|
|
|
.filter(source-> StrUtil.isNotEmpty(source.getSummary())).collect(Collectors.toList());
|
|
|
|
|
sourceKgInfoList.addAll(sourceKgInfos);
|
|
|
|
|
}
|
|
|
|
|
return chatResVo;
|
|
|
|
|
// 数据库中未配置信息,则是使用Qanything返回的信息
|
|
|
|
|
List<SourceKgInfo> candidateKgInfoList = sourceDTOList.stream().filter(source -> !preferFileIds.contains(source.getFileId()))
|
|
|
|
|
.map(SourceKgInfo::sourceDTO2SourceKgInfo).collect(Collectors.toList());
|
|
|
|
|
sourceKgInfoList.addAll(candidateKgInfoList);
|
|
|
|
|
return sourceKgInfoList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|