Files
ylhp-ai-project-manager/src/main/java/cn/yinlihupo/controller/ai/AiKnowledgeBaseController.java
JiaoTianBo 9f972f5e30 feat(ai-knowledge-base): 实现AI知识库文档上传与管理功能
- 新增AiDocument实体类,映射数据库ai_document表结构
- 添加AiDocumentMapper接口,提供文档增删改查及状态更新等数据库操作
- 实现AiKnowledgeBaseService接口及其实现类AiKnowledgeBaseServiceImpl,支持文件上传、文档列表查询、删除和重新索引
- 在AiKnowledgeBaseController中提供REST接口支持文件上传、文档管理和异步重新索引操作
- 实现DocumentProcessor组件,负责文档解析、文本切片、摘要生成和向量化存储
- 集成MinioService实现文件的上传、下载和删除操作
- 设计KbDocumentVO作为知识库文档视图对象,方便接口数据传输和展示
- 增加文件类型支持和上传文件校验,限制最大50MB文件大小
- 使用异步机制处理文档解析和向量化,提高系统处理性能和响应速度
- 实现文档状态管理和错误处理,确保文档处理流程的正确性和稳定性
2026-03-30 16:49:07 +08:00

138 lines
4.6 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package cn.yinlihupo.controller.ai;
import cn.yinlihupo.common.core.BaseResponse;
import cn.yinlihupo.common.util.ResultUtils;
import cn.yinlihupo.common.util.SecurityUtils;
import cn.yinlihupo.domain.vo.KbDocumentVO;
import cn.yinlihupo.service.ai.AiKnowledgeBaseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
/**
* AI知识库控制器
* 提供知识库文件上传、管理等功能
*/
@Slf4j
@RestController
@RequestMapping("/ai/kb")
@RequiredArgsConstructor
@Tag(name = "AI知识库", description = "AI知识库文档管理相关接口")
public class AiKnowledgeBaseController {
private final AiKnowledgeBaseService knowledgeBaseService;
/**
* 上传文件到知识库
*
* @param projectId 项目ID
* @param file 文件
* @return 文档信息
*/
@PostMapping("/upload")
@Operation(summary = "上传文件", description = "上传文件到项目知识库支持PDF、Word、TXT等格式")
public BaseResponse<KbDocumentVO> uploadFile(
@RequestParam Long projectId,
@RequestParam MultipartFile file) {
Long userId = SecurityUtils.getCurrentUserId();
if (userId == null) {
return ResultUtils.error("用户未登录");
}
if (projectId == null) {
return ResultUtils.error("项目ID不能为空");
}
if (file == null || file.isEmpty()) {
return ResultUtils.error("文件不能为空");
}
try {
KbDocumentVO doc = knowledgeBaseService.uploadFile(projectId, file, userId);
return ResultUtils.success("上传成功", doc);
} catch (Exception e) {
log.error("上传文件失败: {}", e.getMessage(), e);
return ResultUtils.error("上传失败: " + e.getMessage());
}
}
/**
* 获取项目知识库文档列表
*
* @param projectId 项目ID
* @return 文档列表
*/
@GetMapping("/documents")
@Operation(summary = "获取文档列表", description = "获取指定项目的知识库文档列表")
public BaseResponse<List<KbDocumentVO>> getDocuments(
@RequestParam Long projectId) {
Long userId = SecurityUtils.getCurrentUserId();
if (userId == null) {
return ResultUtils.error("用户未登录");
}
if (projectId == null) {
return ResultUtils.error("项目ID不能为空");
}
try {
List<KbDocumentVO> documents = knowledgeBaseService.getProjectDocuments(projectId);
return ResultUtils.success("查询成功", documents);
} catch (Exception e) {
log.error("获取文档列表失败: {}", e.getMessage(), e);
return ResultUtils.error("获取文档列表失败: " + e.getMessage());
}
}
/**
* 删除知识库文档
*
* @param docId 文档UUID
* @return 操作结果
*/
@DeleteMapping("/document/{docId}")
@Operation(summary = "删除文档", description = "删除指定的知识库文档")
public BaseResponse<Void> deleteDocument(@PathVariable String docId) {
Long userId = SecurityUtils.getCurrentUserId();
if (userId == null) {
return ResultUtils.error("用户未登录");
}
try {
knowledgeBaseService.deleteDocument(docId, userId);
return ResultUtils.success("删除成功", null);
} catch (Exception e) {
log.error("删除文档失败: {}", e.getMessage(), e);
return ResultUtils.error("删除失败: " + e.getMessage());
}
}
/**
* 重新索引文档
*
* @param docId 文档UUID
* @return 操作结果
*/
@PostMapping("/document/{docId}/reindex")
@Operation(summary = "重新索引文档", description = "重新解析并索引指定的文档")
public BaseResponse<Void> reindexDocument(@PathVariable String docId) {
Long userId = SecurityUtils.getCurrentUserId();
if (userId == null) {
return ResultUtils.error("用户未登录");
}
try {
knowledgeBaseService.reindexDocument(docId, userId);
return ResultUtils.success("重新索引已启动", null);
} catch (Exception e) {
log.error("重新索引文档失败: {}", e.getMessage(), e);
return ResultUtils.error("重新索引失败: " + e.getMessage());
}
}
}