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

style: 优化知识库代码结构

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