|
@@ -1,7 +1,25 @@
|
|
|
package cn.iocoder.yudao.module.ai.service.knowledge;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
|
|
+import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
|
|
+import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
|
+import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.AiKnowledgeDocumentCreateReqVO;
|
|
|
+import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeDocumentDO;
|
|
|
+import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeSegmentDO;
|
|
|
+import cn.iocoder.yudao.module.ai.dal.mysql.knowledge.AiKnowledgeDocumentMapper;
|
|
|
+import cn.iocoder.yudao.module.ai.dal.mysql.knowledge.AiKnowledgeSegmentMapper;
|
|
|
+import cn.iocoder.yudao.module.ai.enums.knowledge.AiKnowledgeDocumentStatusEnum;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.ai.document.Document;
|
|
|
+import org.springframework.ai.reader.tika.TikaDocumentReader;
|
|
|
+import org.springframework.ai.transformer.splitter.TokenTextSplitter;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* AI 知识库-文档 Service 实现类
|
|
@@ -12,5 +30,55 @@ import org.springframework.stereotype.Service;
|
|
|
@Slf4j
|
|
|
public class AiKnowledgeDocumentServiceImpl implements AiKnowledgeDocumentService {
|
|
|
|
|
|
+ @Resource
|
|
|
+ private AiKnowledgeDocumentMapper documentMapper;
|
|
|
+ @Resource
|
|
|
+ private AiKnowledgeSegmentMapper segmentMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private TokenTextSplitter tokenTextSplitter;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private AiEmbeddingService embeddingService;
|
|
|
+
|
|
|
+ // TODO @xin 临时测试用,后续删
|
|
|
+ @Value("classpath:/webapp/test/Fel.pdf")
|
|
|
+ private org.springframework.core.io.Resource data;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Long createKnowledgeDocument(AiKnowledgeDocumentCreateReqVO createReqVO) {
|
|
|
+ AiKnowledgeDocumentDO documentDO = BeanUtils.toBean(createReqVO, AiKnowledgeDocumentDO.class);
|
|
|
+ documentDO
|
|
|
+ //todo
|
|
|
+ .setTokens(0).setWordCount(0)
|
|
|
+ .setStatus(CommonStatusEnum.ENABLE.getStatus()).setSliceStatus(AiKnowledgeDocumentStatusEnum.SUCCESS.getStatus());
|
|
|
+ documentMapper.insert(documentDO);
|
|
|
+
|
|
|
+ TikaDocumentReader loader = new TikaDocumentReader(data);
|
|
|
+ List<Document> documents = loader.get();
|
|
|
+ Long documentId = documentDO.getId();
|
|
|
+ if (CollUtil.isEmpty(documents)) {
|
|
|
+ log.info("文档内容为空");
|
|
|
+ return documentId;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 文档分段
|
|
|
+ List<Document> segments = tokenTextSplitter.apply(documents);
|
|
|
+
|
|
|
+ List<AiKnowledgeSegmentDO> segmentDOList = CollectionUtils.convertList(segments,
|
|
|
+ segment -> new AiKnowledgeSegmentDO().setContent(segment.getContent()).setDocumentId(documentId)
|
|
|
+ //todo
|
|
|
+ .setTokens(0).setWordCount(0)
|
|
|
+ .setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
|
|
+
|
|
|
+ // 分段内容入库
|
|
|
+ segmentMapper.insertBatch(segmentDOList);
|
|
|
+
|
|
|
+ //向量化并存储
|
|
|
+ embeddingService.add(segments);
|
|
|
|
|
|
+ return documentId;
|
|
|
+ }
|
|
|
}
|