feat(document): 实现多格式文档上传与解析功能
- 移除 AiChatController 中的 PDF 读取相关逻辑与依赖- 新增 DocumentController 支持文件上传接口 - 新增 DocumentIngestionService 接口及实现,负责文档处理流程 - 抽象 DocumentParser 接口统一各类文档解析器行为 - 重构所有具体文档读取器(PDF、HTML、JSON 等)实现新的解析接口- 引入 MultipartFileResource 工具类以适配 Spring AI 读取器 - 添加 DocumentUploadResponse 响应模型类 - 各文档读取器增加对文件扩展名和 MIME 类型的支持判断
This commit is contained in:
@@ -3,21 +3,18 @@ package com.hanserwei.chat.reader;
|
||||
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.core.io.Resource;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class MyTikaPptReader {
|
||||
public class MyTikaPptReader implements DocumentParser {
|
||||
|
||||
@Value("classpath:/document/XX牌云感变频空调说明书.pptx")
|
||||
private Resource resource;
|
||||
|
||||
public List<Document> loadPpt() {
|
||||
@Override
|
||||
public List<Document> parse(MultipartFile file) {
|
||||
// 新建 TikaDocumentReader 阅读器
|
||||
TikaDocumentReader tikaDocumentReader = new TikaDocumentReader(resource);
|
||||
TikaDocumentReader tikaDocumentReader = new TikaDocumentReader(MultipartFileResource.of(file));
|
||||
// 读取并转换为 Document 文档集合
|
||||
List<Document> documents = tikaDocumentReader.get();
|
||||
|
||||
@@ -26,4 +23,12 @@ public class MyTikaPptReader {
|
||||
TokenTextSplitter splitter = new TokenTextSplitter(1000, 400, 10, 5000, true);
|
||||
return splitter.apply(documents);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(String filename, String contentType) {
|
||||
return hasExtension(filename, "ppt", "pptx") ||
|
||||
matchesContentType(contentType,
|
||||
"application/vnd.ms-powerpoint",
|
||||
"application/vnd.openxmlformats-officedocument.presentationml.presentation");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user