1. 调整Qanything代码查询逻辑

main
xueqingkun 10 months ago
parent ecf3c08edb
commit 93c68bcd64

@ -1,9 +1,6 @@
package com.supervision.qanything;
import com.supervision.qanything.dto.ChatResult;
import com.supervision.qanything.dto.CreateKBResult;
import com.supervision.qanything.dto.ResultWrapper;
import com.supervision.qanything.dto.UploadResult;
import com.supervision.qanything.dto.*;
import java.io.File;
import java.security.NoSuchAlgorithmException;
@ -28,6 +25,9 @@ public interface QanythingService {
*/
ResultWrapper<ChatResult> chat(String question,List<String> kbIds) throws NoSuchAlgorithmException;
ResultWrapper<ChatResult> chat(String question,List<HistoryDTO> history, List<String> kbIds) throws NoSuchAlgorithmException;
/**
*
* @param kbId ID

@ -26,5 +26,5 @@ public class SourceDTO {
/**
*
*/
private String source;
private String score;
}

@ -96,6 +96,29 @@ public class QanythingServiceImpl implements QanythingService {
}
}
@Override
public ResultWrapper<ChatResult> chat(String question, List<HistoryDTO> history, List<String> kbIds) throws NoSuchAlgorithmException {
Assert.notEmpty(kbIds, "kbIds不能为空");
Assert.notEmpty(question, "问题不能为空");
log.info("chat:请求入参question:{},kbIds:{},history:{}", question, kbIds,JSONUtil.toJsonStr(history));
ChatParam chatParam = new ChatParam();
chatParam.setQ(question);
chatParam.setKbIds(kbIds);
chatParam.setHistory(history);
// 添加鉴权相关参数
AuthV3Util.addAuthParams(APP_KEY, APP_SECRET, chatParam);
String paramString = JSONUtil.toJsonStr(chatParam);
log.info("chat:请求参数:{}", paramString);
HttpRequest request = HttpRequest.post(BASE_URL + QanythingConstant.URL_CHAT)
.body(paramString);
try (HttpResponse response = request.execute()){
String body = response.body();
log.info("chat:响应结果:{}", body);
return JSONUtil.toBean(body, new TypeReference<ResultWrapper<ChatResult>>(){},true);
}
}
@Override
public ResultWrapper<List<UploadResult>> uploadDoc(String kbId, File file) throws NoSuchAlgorithmException {
Assert.notNull(file, "文件不能为空");

@ -32,6 +32,9 @@ import java.io.IOException;
import java.nio.file.Files;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@Slf4j
@ -77,13 +80,22 @@ public class KGServiceImpl implements KGService {
if (CollUtil.isEmpty(sourceDTOList)){
return sourceKgInfoList;
}
// 根据fileId进行去重
sourceDTOList = sourceDTOList.stream().filter(distinctPredicate(SourceDTO::getFileId)).collect(Collectors.toList());
// qanything 中文件id与文件内容的映射
Map<String, String> fileIdMapContent = sourceDTOList.stream().collect(Collectors.toMap(SourceDTO::getFileId, SourceDTO::getContent, (o, n) -> o));
Set<String> qaFileId = sourceDTOList.stream().map(SourceDTO::getFileId).filter(Objects::nonNull).collect(Collectors.toSet());
// 优先选择的文档id
Set<String> preferFileIds = new HashSet<>();
if (CollUtil.isNotEmpty(qaFileId)) {
// 优先使用数据库中配置的信息
List<KgInfo> preferKgInfoList = kgInfoService.lambdaQuery().in(KgInfo::getFileQaDocId, qaFileId).list();
List<SourceKgInfo> sourceKgInfos = preferKgInfoList.stream().peek(kgInfo->preferFileIds.add(kgInfo.getFileQaDocId()))
.map(SourceKgInfo::kgInfo2SourceKgInfo)
.peek(kgInfo->{
if (StrUtil.isEmpty(kgInfo.getContent())){
kgInfo.setContent(fileIdMapContent.get(kgInfo.getFileQaDocId()));
}
}).map(SourceKgInfo::kgInfo2SourceKgInfo)
.filter(source-> StrUtil.isNotEmpty(source.getSummary())).collect(Collectors.toList());
sourceKgInfoList.addAll(sourceKgInfos);
}
@ -141,4 +153,11 @@ public class KGServiceImpl implements KGService {
return null;
}
public static <K> Predicate<K> distinctPredicate(Function<K, Object> function) {
ConcurrentHashMap<Object, Boolean> map = new ConcurrentHashMap<>();
// 根据key进行去重,并排除为null的数据
return t -> null == map.putIfAbsent(function.apply(t), true);
}
}

@ -18,7 +18,7 @@ public class SourceKgInfo {
private String title;
/**
* 1 2 3 4
* 1 2 3 4 5: 6:
*/
private String label;

@ -21,7 +21,7 @@ public class KgInfo implements Serializable {
private String id;
/**
* 1 2 3 4
* 1 2 3 4 5: 6:
*/
private String label;

Loading…
Cancel
Save