feat(document): 实现多格式文档上传与解析功能

- 移除 AiChatController 中的 PDF 读取相关逻辑与依赖- 新增 DocumentController 支持文件上传接口
- 新增 DocumentIngestionService 接口及实现,负责文档处理流程
- 抽象 DocumentParser 接口统一各类文档解析器行为
- 重构所有具体文档读取器(PDF、HTML、JSON 等)实现新的解析接口- 引入 MultipartFileResource 工具类以适配 Spring AI 读取器
- 添加 DocumentUploadResponse 响应模型类
- 各文档读取器增加对文件扩展名和 MIME 类型的支持判断
This commit is contained in:
2025-10-31 21:31:44 +08:00
parent 5ee2a0f11c
commit a9fce282ed
14 changed files with 269 additions and 102 deletions

View File

@@ -2,20 +2,15 @@ package com.hanserwei.chat.controller;
import com.hanserwei.chat.model.dto.ChatMessageDTO;
import com.hanserwei.chat.model.vo.AIResponse;
import com.hanserwei.chat.reader.MyPdfReader;
import com.hanserwei.chat.utils.ConversationContext;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/ai")
@@ -23,10 +18,6 @@ public class AiChatController {
@Resource
private ChatClient dashScopeChatClient;
@Resource
private MyPdfReader myPdfReader;
@Resource
private VectorStore vectorStore;
@PostMapping(path = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<AIResponse> chatWithAi(@RequestBody ChatMessageDTO chatMessageDTO) {
@@ -44,11 +35,4 @@ public class AiChatController {
.contextWrite(ctx -> ConversationContext.withConversationId(chatMessageDTO.getConversionId()))
.doFinally(signalType -> ConversationContext.clear());
}
@GetMapping("/readpdf")
public String readPdf() {
List<Document> docsFromPdf = myPdfReader.getDocsFromPdf();
vectorStore.add(docsFromPdf);
return "ok!";
}
}