feat(ai-knowledge-base): 实现AI知识库文档上传与管理功能
- 新增AiDocument实体类,映射数据库ai_document表结构 - 添加AiDocumentMapper接口,提供文档增删改查及状态更新等数据库操作 - 实现AiKnowledgeBaseService接口及其实现类AiKnowledgeBaseServiceImpl,支持文件上传、文档列表查询、删除和重新索引 - 在AiKnowledgeBaseController中提供REST接口支持文件上传、文档管理和异步重新索引操作 - 实现DocumentProcessor组件,负责文档解析、文本切片、摘要生成和向量化存储 - 集成MinioService实现文件的上传、下载和删除操作 - 设计KbDocumentVO作为知识库文档视图对象,方便接口数据传输和展示 - 增加文件类型支持和上传文件校验,限制最大50MB文件大小 - 使用异步机制处理文档解析和向量化,提高系统处理性能和响应速度 - 实现文档状态管理和错误处理,确保文档处理流程的正确性和稳定性
This commit is contained in:
@@ -4,7 +4,6 @@ import cn.yinlihupo.domain.vo.KbDocumentVO;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* AI知识库服务接口
|
||||
@@ -35,7 +34,7 @@ public interface AiKnowledgeBaseService {
|
||||
* @param docId 文档UUID
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
void deleteDocument(UUID docId, Long userId);
|
||||
void deleteDocument(String docId, Long userId);
|
||||
|
||||
/**
|
||||
* 重新索引文档
|
||||
@@ -43,7 +42,7 @@ public interface AiKnowledgeBaseService {
|
||||
* @param docId 文档UUID
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
void reindexDocument(UUID docId, Long userId);
|
||||
void reindexDocument(String docId, Long userId);
|
||||
|
||||
/**
|
||||
* 处理文档(解析、切片、向量化)
|
||||
|
||||
@@ -39,7 +39,7 @@ public class AiKnowledgeBaseServiceImpl implements AiKnowledgeBaseService {
|
||||
validateFile(file);
|
||||
|
||||
// 2. 生成文档UUID
|
||||
UUID docId = UUID.randomUUID();
|
||||
String docId = UUID.randomUUID().toString();
|
||||
|
||||
// 3. 上传文件到MinIO
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
@@ -63,6 +63,7 @@ public class AiKnowledgeBaseServiceImpl implements AiKnowledgeBaseService {
|
||||
doc.setFileType(fileExtension);
|
||||
doc.setFileSize(file.getSize());
|
||||
doc.setFilePath(filePath);
|
||||
doc.setContent("");
|
||||
doc.setStatus("pending"); // 待处理状态
|
||||
doc.setChunkTotal(0);
|
||||
doc.setCreateBy(userId);
|
||||
@@ -86,7 +87,7 @@ public class AiKnowledgeBaseServiceImpl implements AiKnowledgeBaseService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteDocument(UUID docId, Long userId) {
|
||||
public void deleteDocument(String docId, Long userId) {
|
||||
// 1. 查询文档
|
||||
AiDocument doc = documentMapper.selectByDocId(docId);
|
||||
if (doc == null) {
|
||||
@@ -111,7 +112,7 @@ public class AiKnowledgeBaseServiceImpl implements AiKnowledgeBaseService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reindexDocument(UUID docId, Long userId) {
|
||||
public void reindexDocument(String docId, Long userId) {
|
||||
// 1. 查询文档
|
||||
AiDocument doc = documentMapper.selectByDocId(docId);
|
||||
if (doc == null) {
|
||||
|
||||
@@ -13,7 +13,9 @@ import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.*;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -167,7 +169,7 @@ public class DocumentProcessor {
|
||||
* @param chunks 切片列表
|
||||
*/
|
||||
private void storeChunks(AiDocument parentDoc, List<String> chunks) {
|
||||
UUID docId = parentDoc.getDocId();
|
||||
String docId = parentDoc.getDocId();
|
||||
Long parentId = parentDoc.getId();
|
||||
|
||||
for (int i = 0; i < chunks.size(); i++) {
|
||||
@@ -206,7 +208,7 @@ public class DocumentProcessor {
|
||||
*
|
||||
* @param docId 文档UUID
|
||||
*/
|
||||
public void deleteDocumentVectors(UUID docId) {
|
||||
public void deleteDocumentVectors(String docId) {
|
||||
try {
|
||||
// 查询所有相关切片
|
||||
// 注意:pgvector store的删除需要根据metadata过滤
|
||||
|
||||
Reference in New Issue
Block a user