liusuyi
2026-05-18 8a7b666f0bed7c1dc03767166c7ecd602552a5c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.ard.agent.controller;
 
import com.ard.agent.service.KnowledgeBaseService;
import com.ard.agent.service.RagChatService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 知识库独立接口
 *
 * @author lsy
 * @date 2026/5/11
 */
 
 
@Slf4j
@RestController
@RequestMapping("/api/knowledge")
public class knowledgeBaseController {
    @Resource
    private RagChatService ragChatService;
    @Resource
    private KnowledgeBaseService knowledgeBaseService;
 
    /**
     * 上传文档到知识库
     */
    @PostMapping("/upload")
    public ResponseEntity<Map<String, Object>> uploadDocument(@RequestParam("file") MultipartFile file) {
        log.info("收到文档上传请求: {}", file.getOriginalFilename());
 
        if (file.isEmpty()) {
            Map<String, Object> error = new HashMap<>();
            error.put("success", false);
            error.put("message", "文件不能为空");
            return ResponseEntity.badRequest().body(error);
        }
 
        Map<String, Object> result = knowledgeBaseService.uploadDocument(file);
 
        if (Boolean.TRUE.equals(result.get("success"))) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.badRequest().body(result);
        }
    }
 
    /**
     * 批量上传文档
     */
    @PostMapping("/batch-upload")
    public ResponseEntity<Map<String, Object>> batchUploadDocuments(@RequestParam("files") List<MultipartFile> files) {
        log.info("收到批量文档上传请求,共 {} 个文件", files.size());
 
        Map<String, Object> batchResult = new HashMap<>();
        List<Map<String, Object>> results = new ArrayList<>();
 
        int successCount = 0;
        int failCount = 0;
 
        for (MultipartFile file : files) {
            Map<String, Object> result = knowledgeBaseService.uploadDocument(file);
            results.add(result);
            if (Boolean.TRUE.equals(result.get("success"))) {
                successCount++;
            } else {
                failCount++;
            }
        }
 
        batchResult.put("total", files.size());
        batchResult.put("success", successCount);
        batchResult.put("fail", failCount);
        batchResult.put("details", results);
 
        return ResponseEntity.ok(batchResult);
    }
 
 
    /**
     * 流式接口(新增)
     * 访问示例:/chat/stream?question=什么是Spring
     */
    @GetMapping(value = "/chat/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> chatStream(@RequestParam("question") String question) {
        return ragChatService.chatStream(question);
    }
}