feat(document): 实现多格式文档上传与解析功能
- 移除 AiChatController 中的 PDF 读取相关逻辑与依赖- 新增 DocumentController 支持文件上传接口 - 新增 DocumentIngestionService 接口及实现,负责文档处理流程 - 抽象 DocumentParser 接口统一各类文档解析器行为 - 重构所有具体文档读取器(PDF、HTML、JSON 等)实现新的解析接口- 引入 MultipartFileResource 工具类以适配 Spring AI 读取器 - 添加 DocumentUploadResponse 响应模型类 - 各文档读取器增加对文件扩展名和 MIME 类型的支持判断
This commit is contained in:
@@ -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!";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.hanserwei.chat.controller;
|
||||
|
||||
import com.hanserwei.chat.model.vo.DocumentUploadResponse;
|
||||
import com.hanserwei.chat.service.DocumentIngestionService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/documents")
|
||||
@RequiredArgsConstructor
|
||||
public class DocumentController {
|
||||
|
||||
private final DocumentIngestionService documentIngestionService;
|
||||
|
||||
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
public ResponseEntity<DocumentUploadResponse> upload(@RequestParam("file") MultipartFile file) {
|
||||
int documentCount = documentIngestionService.ingest(file);
|
||||
log.info("文件 {} 上传成功。", file.getOriginalFilename());
|
||||
return ResponseEntity.ok(new DocumentUploadResponse(file.getOriginalFilename(), documentCount, "ok"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user