Commit 4399dca0 authored by alex yao's avatar alex yao

style: 优化知识库代码结构

parent 87d16df2
......@@ -50,7 +50,7 @@ public interface KnowledgeService {
/**
* 获取用户知识库
*/
List<BizKnowledgeDocumentEntity> searchDocuments(String search, String trainStatus,List<Integer> kdIds, PagingInfo pagingInfo) throws Exception;
List<BizKnowledgeDocumentEntity> searchDocuments(String search, String trainStatus, List<Integer> kdIds, String memberId, PagingInfo pagingInfo);
/**
* 获取用户知识库文档
......
......@@ -151,6 +151,9 @@ public class KnowledgeServiceImpl implements KnowledgeService {
@Override
public String enableKnowledge(Integer kdId) throws Exception {
BizKnowledgeDocumentEntity bizKnowledgeDocumentEntity = bizKnowledgeDocumentService.get(kdId);
if (bizKnowledgeDocumentEntity == null) {
throw new BusinessException("document not exist");
}
String isEnable = bizKnowledgeDocumentEntity.getIsEnable();
if (CommonConstant.YOrN.N.equals(isEnable)) {
......@@ -166,6 +169,11 @@ public class KnowledgeServiceImpl implements KnowledgeService {
public Boolean trainKnowledge(Integer knowledgeInfoId, List<Integer> kdIds, SegmentationConfigRequest segmentationConfig) throws Exception {
Assert.notEmpty(kdIds);
Assert.isTrue(kdIds.size() <= 5, "Training cannot exceed five documents");
BizKnowledgeInfoEntity knowledgeInfoEntity = bizKnowledgeInfoService.get(knowledgeInfoId);
if (knowledgeInfoEntity == null) {
throw new BusinessException("Knowledge information does not exist");
}
for (Integer kdId : kdIds) {
BizKnowledgeDocumentEntity bizKnowledgeDocumentEntity = bizKnowledgeDocumentService.get(kdId);
String currentStatus = bizKnowledgeDocumentEntity.getTrainStatus();
......@@ -187,8 +195,6 @@ public class KnowledgeServiceImpl implements KnowledgeService {
knowledgeProducerService.trainKnowledge(message);
}
BizKnowledgeInfoEntity knowledgeInfoEntity = bizKnowledgeInfoService.get(knowledgeInfoId);
if (StringUtils.isNotBlank(knowledgeInfoEntity.getKdIds())) {
List<Integer> kdIdList = JsonUtils.deSerialize(knowledgeInfoEntity.getKdIds(), new TypeReference<List<Integer>>() {
}.getType());
......@@ -203,13 +209,9 @@ public class KnowledgeServiceImpl implements KnowledgeService {
}
@Override
public List<BizKnowledgeDocumentEntity> searchDocuments(String search, String trainStatus, List<Integer> kdIds, PagingInfo pagingInfo) throws Exception {
public List<BizKnowledgeDocumentEntity> searchDocuments(String search, String trainStatus, List<Integer> kdIds, String memberId, PagingInfo pagingInfo) {
List<BizKnowledgeDocumentEntity> result = new ArrayList<>();
String memberId = BlContext.getCurrentUser().getUserId().toString();
if (StringUtils.isBlank(memberId)) {
memberId = BlContext.getCurrentUserNotException().getUserId().toString();
}
List<KnowledgeQueryItem> knowledgeQueryItems = bizKnowledgeDocumentService.searchKnowledge(search, trainStatus, memberId, null, kdIds, pagingInfo);
List<KnowledgeQueryItem> knowledgeQueryItems = bizKnowledgeDocumentService.searchKnowledge(search, trainStatus, memberId, null, kdIds, pagingInfo);
if (CollectionUtils.isNotEmpty(knowledgeQueryItems)) {
result = knowledgeQueryItems.stream().map(item -> {
BizKnowledgeDocumentEntity entity = new BizKnowledgeDocumentEntity();
......@@ -230,6 +232,9 @@ public class KnowledgeServiceImpl implements KnowledgeService {
List<BizKnowledgeDocumentEntity> res = new ArrayList<>();
for (Integer kdId : kdIds) {
BizKnowledgeDocumentEntity bizKnowledgeDocumentEntity = bizKnowledgeDocumentService.get(kdId);
if (bizKnowledgeDocumentEntity == null) {
continue;
}
res.add(bizKnowledgeDocumentEntity);
}
return res;
......@@ -249,6 +254,9 @@ public class KnowledgeServiceImpl implements KnowledgeService {
@Override
public void openKnowledgeChunk(Integer kdId, String chunkRelationId, String isOpen) {
BizKnowledgeDocumentEntity bizKnowledgeDocumentEntity = bizKnowledgeDocumentService.get(kdId);
if (bizKnowledgeDocumentEntity == null) {
throw new BusinessException("The knowledge does not exist");
}
String knowledgeId = bizKnowledgeDocumentEntity.getKnowledgeId();
demandKnowledgeService.openKnowledgeChunk(knowledgeId, chunkRelationId, isOpen);
}
......@@ -256,6 +264,9 @@ public class KnowledgeServiceImpl implements KnowledgeService {
@Override
public void deleteKnowledgeChunk(Integer kdId, String chunkRelationId) {
BizKnowledgeDocumentEntity bizKnowledgeDocumentEntity = bizKnowledgeDocumentService.get(kdId);
if (bizKnowledgeDocumentEntity == null) {
throw new BusinessException("The knowledge does not exist");
}
String knowledgeId = bizKnowledgeDocumentEntity.getKnowledgeId();
demandKnowledgeService.deleteKnowledgeChunk(knowledgeId, chunkRelationId);
}
......@@ -263,6 +274,9 @@ public class KnowledgeServiceImpl implements KnowledgeService {
@Override
public void updateKnowledgeChunkDoc(Integer kdId, String chunkRelationId, String content) {
BizKnowledgeDocumentEntity bizKnowledgeDocumentEntity = bizKnowledgeDocumentService.get(kdId);
if (bizKnowledgeDocumentEntity == null) {
throw new BusinessException("The knowledge does not exist");
}
String knowledgeId = bizKnowledgeDocumentEntity.getKnowledgeId();
demandKnowledgeService.updateKnowledgeChunkDoc(knowledgeId, chunkRelationId, content);
}
......@@ -270,6 +284,9 @@ public class KnowledgeServiceImpl implements KnowledgeService {
@Override
public void addKnowledgeChunk(Integer kdId, String content, Integer chunkSort) {
BizKnowledgeDocumentEntity bizKnowledgeDocumentEntity = bizKnowledgeDocumentService.get(kdId);
if (bizKnowledgeDocumentEntity == null) {
throw new BusinessException("The knowledge does not exist");
}
String knowledgeId = bizKnowledgeDocumentEntity.getKnowledgeId();
demandKnowledgeService.addKnowledgeChunk(knowledgeId, content, chunkSort);
}
......@@ -279,8 +296,7 @@ public class KnowledgeServiceImpl implements KnowledgeService {
//获取知识库配置
List<Integer> kdIdList = new ArrayList<>();
if (ArrayUtils.isNotEmpty(knowledgeInfoIds)) {
String[] knowledgeIds = knowledgeInfoIds;
for (String knowledgeId : knowledgeIds) {
for (String knowledgeId : knowledgeInfoIds) {
BizKnowledgeInfoEntity bizKnowledgeInfoEntity = bizKnowledgeInfoService.get(Integer.valueOf(knowledgeId));
if (bizKnowledgeInfoEntity == null || org.apache.commons.lang3.StringUtils.isBlank(bizKnowledgeInfoEntity.getKdIds())) {
continue;
......
......@@ -46,9 +46,8 @@ public class KnowledgeRestImpl implements KnowledgeRest {
public List<BizKnowledgeDocumentDto> uploadDocument(MultipartFile[] documentFiles) throws Exception {
Assert.notEmpty(documentFiles);
Assert.isTrue(documentFiles.length <= 5, "Uploaded files can be no larger than five");
List<BizKnowledgeDocumentDto> result = new ArrayList<>();
List<BizKnowledgeDocumentEntity> entities = knowledgeService.uploadDocument(documentFiles);
result = entities.stream().map(entity -> {
List<BizKnowledgeDocumentDto> result = entities.stream().map(entity -> {
BizKnowledgeDocumentDto dto = new BizKnowledgeDocumentDto();
BeanUtil.copyProperties(entity, dto);
return dto;
......@@ -58,12 +57,15 @@ public class KnowledgeRestImpl implements KnowledgeRest {
@Override
public Boolean delDocument(Integer knowledgeInfoId, Integer kdId) {
Assert.notNull(knowledgeInfoId);
Assert.notNull(kdId);
return knowledgeService.delDocument(knowledgeInfoId, kdId);
}
@Override
public Boolean batchDelDocument(Integer knowledgeInfoId, List<Integer> kdId) {
Assert.notEmpty(kdId);
Assert.notNull(knowledgeInfoId);
boolean result = true;
for (Integer id : kdId) {
result = result && knowledgeService.delDocument(knowledgeInfoId, id);
......@@ -73,23 +75,29 @@ public class KnowledgeRestImpl implements KnowledgeRest {
@Override
public Boolean renameDocument(Integer kdId, String rename) throws Exception {
Assert.notNull(kdId);
Assert.notEmpty(rename);
return knowledgeService.renameDocument(kdId, rename);
}
@Override
public String enableKnowledge(Integer kdId) throws Exception {
Assert.notNull(kdId);
return knowledgeService.enableKnowledge(kdId);
}
@Override
public Boolean trainKnowledge(TrainKnowledgeDto dto) throws Exception {
//执行任务
knowledgeService.trainKnowledge(dto.getKnowledgeInfoId(), dto.getKdIds(), dto.getSegmentationConfig());
return true;
Assert.notNull(dto);
Assert.notNull(dto.getKnowledgeInfoId());
Assert.notNull(dto.getKdIds());
Assert.notNull(dto.getSegmentationConfig());
return knowledgeService.trainKnowledge(dto.getKnowledgeInfoId(), dto.getKdIds(), dto.getSegmentationConfig());
}
@Override
public List<BizKnowledgeDocumentDto> searchDocuments(String search, String trainStatus, Integer knowledgeInfoId, PagingInfo pagingInfo) throws Exception {
Assert.notNull(knowledgeInfoId);
List<BizKnowledgeDocumentDto> result = new ArrayList<>();
BizKnowledgeInfoEntity bizKnowledgeInfoEntity = bizKnowledgeInfoService.get(knowledgeInfoId);
......@@ -106,7 +114,8 @@ public class KnowledgeRestImpl implements KnowledgeRest {
return result;
}
List<BizKnowledgeDocumentEntity> entities = knowledgeService.searchDocuments(search, trainStatus, kdIdList, pagingInfo);
UserBaseEntity currentUser = BlContext.getCurrentUserNotException();
List<BizKnowledgeDocumentEntity> entities = knowledgeService.searchDocuments(search, trainStatus, kdIdList, currentUser.getUserId().toString(), pagingInfo);
if (CollectionUtils.isNotEmpty(entities)) {
result = entities.stream().map(entity -> {
BizKnowledgeDocumentDto dto = new BizKnowledgeDocumentDto();
......@@ -146,8 +155,8 @@ public class KnowledgeRestImpl implements KnowledgeRest {
@Override
public List<BizKnowledgeDocumentDto> getListByKdIds(List<Integer> kdIds) {
List<BizKnowledgeDocumentDto> res = new ArrayList<>();
Assert.notEmpty(kdIds);
List<BizKnowledgeDocumentDto> res = new ArrayList<>();
List<BizKnowledgeDocumentEntity> entities = knowledgeService.getListByKdIds(kdIds);
if (CollectionUtils.isNotEmpty(entities)) {
res = entities.stream().map(entity -> {
......
package cn.com.poc.thirdparty.resource.demand.ai.aggregate.impl;
import cn.com.poc.common.utils.JsonUtils;
import cn.com.poc.common.utils.ListUtils;
import cn.com.poc.common.utils.http.LocalHttpClient;
import cn.com.poc.support.dgTools.result.DgtoolsApiResult;
import cn.com.poc.thirdparty.resource.demand.ai.aggregate.DemandKnowledgeService;
import cn.com.poc.thirdparty.resource.demand.ai.entity.knowledge.*;
import cn.com.poc.thirdparty.resource.demand.member.service.DemandAuthService;
import cn.com.poc.support.dgTools.DgtoolsAbstractHttpClient;
import cn.com.poc.support.dgTools.constants.DgtoolsApiConstants;
import cn.com.poc.support.dgTools.result.AbstractResult;
import cn.com.yict.framemax.core.config.Config;
import cn.com.poc.thirdparty.resource.demand.ai.aggregate.DemandKnowledgeService;
import cn.com.poc.thirdparty.resource.demand.ai.entity.knowledge.*;
import cn.com.poc.thirdparty.resource.demand.member.service.DemandAuthService;
import cn.com.yict.framemax.core.exception.BusinessException;
import cn.com.yict.framemax.data.model.PagingInfo;
import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSON;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.hadoop.yarn.api.protocolrecords.GetQueueInfoRequest;
import org.apache.http.Header;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
......@@ -65,13 +53,13 @@ public class DemandKnowledgeServiceImpl implements DemandKnowledgeService {
@Override
public List<String> searchKnowledge(String query, List<String> knowledgeIds, Integer topK) {
Assert.notBlank(query);
if (CollectionUtils.isEmpty(knowledgeIds)) {
return new ArrayList<String>();
}
Assert.notBlank(query);
Assert.notNull(topK);
if (topK == null) {
topK = 3;
}
SearchKnowledgeRequest searchKnowledgeRequest = new SearchKnowledgeRequest();
searchKnowledgeRequest.setQuery(query);
searchKnowledgeRequest.setKnowLedgeIds(knowledgeIds);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment