瀏覽代碼

智能体上传文件返回chatid及助手message

lamphua 3 天之前
父節點
當前提交
d4ce4ba520

+ 1
- 1
llm-back/ruoyi-llm/src/main/java/com/ruoyi/web/llm/controller/CmcAgentController.java 查看文件

@@ -86,7 +86,7 @@ public class CmcAgentController extends BaseController
86 86
     @PostMapping("/upload")
87 87
     public AjaxResult upload(MultipartFile file, String agentName) throws IOException
88 88
     {
89
-        return success(new AssistantMessage(cmcAgentService.uploadDocument(file, agentName)));
89
+        return success(cmcAgentService.uploadDocument(file, agentName));
90 90
     }
91 91
 
92 92
     /**

+ 2
- 2
llm-back/ruoyi-llm/src/main/java/com/ruoyi/web/llm/controller/CmcDocumentController.java 查看文件

@@ -73,9 +73,9 @@ public class CmcDocumentController extends BaseController
73 73
      * @return
74 74
      */
75 75
     @PostMapping("/upload")
76
-    public AjaxResult upload(MultipartFile[] fileList, String agentName) throws IOException
76
+    public AjaxResult upload(MultipartFile[] fileList) throws IOException
77 77
     {
78
-        return success(cmcDocumentService.uploadDocument(fileList, agentName));
78
+        return success(cmcDocumentService.uploadDocument(fileList));
79 79
     }
80 80
 
81 81
     /**

+ 1
- 1
llm-back/ruoyi-llm/src/main/java/com/ruoyi/web/llm/service/impl/LangChainMilvusServiceImpl.java 查看文件

@@ -64,7 +64,7 @@ public class LangChainMilvusServiceImpl implements ILangChainMilvusService
64 64
         File profilePath = new File( RuoYiConfig.getProfile() + "/upload/rag/knowledge/" + collectionName);
65 65
         if (!profilePath.exists())
66 66
             profilePath.mkdirs();
67
-        File transferFile = new File( profilePath + File.separator + file.getOriginalFilename());
67
+        File transferFile = new File( profilePath + "/" + file.getOriginalFilename());
68 68
         if (!transferFile.exists())
69 69
             file.transferTo(transferFile);
70 70
         List<TextSegment> segments = splitDocument(file.getOriginalFilename(), transferFile);

+ 2
- 1
llm-back/ruoyi-system/src/main/java/com/ruoyi/llm/service/ICmcAgentService.java 查看文件

@@ -3,6 +3,7 @@ package com.ruoyi.llm.service;
3 3
 import java.io.IOException;
4 4
 import java.util.List;
5 5
 
6
+import com.alibaba.fastjson2.JSONObject;
6 7
 import com.ruoyi.llm.domain.CmcAgent;
7 8
 import org.springframework.web.multipart.MultipartFile;
8 9
 
@@ -44,7 +45,7 @@ public interface ICmcAgentService
44 45
      * @param file 文件
45 46
      * @return 结果
46 47
      */
47
-    public String uploadDocument(MultipartFile file, String agentName) throws IOException;
48
+    public JSONObject uploadDocument(MultipartFile file, String agentName) throws IOException;
48 49
 
49 50
     /**
50 51
      * 上传多文件

+ 1
- 2
llm-back/ruoyi-system/src/main/java/com/ruoyi/llm/service/ICmcDocumentService.java 查看文件

@@ -35,10 +35,9 @@ public interface ICmcDocumentService
35 35
      * 上传外部文件
36 36
      *
37 37
      * @param fileList 文件
38
-     * @param agentName 智能体名称
39 38
      * @return 结果
40 39
      */
41
-    public JSONObject uploadDocument(MultipartFile[] fileList, String agentName) throws IOException;
40
+    public JSONObject uploadDocument(MultipartFile[] fileList) throws IOException;
42 41
 
43 42
     /**
44 43
      * 新增cmc聊天附件

+ 19
- 3
llm-back/ruoyi-system/src/main/java/com/ruoyi/llm/service/impl/CmcAgentServiceImpl.java 查看文件

@@ -6,8 +6,12 @@ import java.io.FileOutputStream;
6 6
 import java.io.IOException;
7 7
 import java.util.List;
8 8
 
9
+import com.alibaba.fastjson2.JSONObject;
9 10
 import com.ruoyi.common.config.RuoYiConfig;
10 11
 import com.ruoyi.common.utils.DateUtils;
12
+import com.ruoyi.common.utils.SnowFlake;
13
+import com.ruoyi.llm.domain.CmcDocument;
14
+import com.ruoyi.llm.mapper.CmcDocumentMapper;
11 15
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
12 16
 import org.springframework.beans.factory.annotation.Autowired;
13 17
 import org.springframework.stereotype.Service;
@@ -28,6 +32,9 @@ public class CmcAgentServiceImpl implements ICmcAgentService
28 32
     @Autowired
29 33
     private CmcAgentMapper cmcAgentMapper;
30 34
 
35
+    @Autowired
36
+    private CmcDocumentMapper cmcDocumentMapper;
37
+
31 38
     /**
32 39
      * 查询智能体
33 40
      * 
@@ -73,15 +80,23 @@ public class CmcAgentServiceImpl implements ICmcAgentService
73 80
      * @return 结果
74 81
      */
75 82
     @Override
76
-    public String uploadDocument(MultipartFile file, String agentName) throws IOException {
83
+    public JSONObject uploadDocument(MultipartFile file, String agentName) throws IOException {
77 84
         File profilePath = new File( RuoYiConfig.getProfile() + "/upload/agent/" + agentName);
78 85
         if (!profilePath.exists())
79 86
             profilePath.mkdirs();
80
-        File transferFile = new File(profilePath + File.separator + file.getOriginalFilename());
87
+        File transferFile = new File(profilePath + "/" + file.getOriginalFilename());
81 88
         if (!transferFile.exists())
82 89
             file.transferTo(transferFile);
90
+        String chatId = new SnowFlake().generateId();
91
+        JSONObject jsonObject = new JSONObject();
92
+        jsonObject.put("chatId", chatId);
83 93
         String[] filenameSplit = file.getOriginalFilename().split("\\.");
84 94
         String outputFilename = "/upload/agent/" + agentName + "/" + file.getOriginalFilename().replace(filenameSplit[filenameSplit.length - 2], filenameSplit[filenameSplit.length - 2] + "_" + agentName);
95
+        CmcDocument cmcDocument = new CmcDocument();
96
+        cmcDocument.setDocumentId(new SnowFlake().generateId());
97
+        cmcDocument.setChatId(chatId);
98
+        cmcDocument.setPath(outputFilename);
99
+        cmcDocumentMapper.insertCmcDocument(cmcDocument);
85 100
         String message = "";
86 101
         if (agentName.contains("技术")) {
87 102
             XWPFDocument doc = new XWPFDocument(new FileInputStream(RuoYiConfig.getProfile() + "/upload/agent/template/technical.docx"));
@@ -105,7 +120,8 @@ public class CmcAgentServiceImpl implements ICmcAgentService
105 120
                     "8 职业健康、安全生产及环保水保措施\n\n" +
106 121
                     "请问您需要哪些章节撰写的帮助?";
107 122
         }
108
-        return message;
123
+        jsonObject.put("assistantMessage", message);
124
+        return jsonObject;
109 125
     }
110 126
 
111 127
     /**

+ 1
- 5
llm-back/ruoyi-system/src/main/java/com/ruoyi/llm/service/impl/CmcDocumentServiceImpl.java 查看文件

@@ -66,15 +66,11 @@ public class CmcDocumentServiceImpl implements ICmcDocumentService
66 66
      * 上传外部文件
67 67
      *
68 68
      * @param fileList 文件
69
-     * @param agentName 智能体名称
70 69
      * @return 结果
71 70
      */
72 71
     @Override
73
-    public JSONObject uploadDocument(MultipartFile[] fileList, String agentName) throws IOException {
72
+    public JSONObject uploadDocument(MultipartFile[] fileList) throws IOException {
74 73
         String prefixPath = "";
75
-        if (!agentName.equals(""))
76
-            prefixPath = "/upload/agent/" + agentName;
77
-        else
78 74
             prefixPath = "/upload/rag/document";
79 75
         File profilePath = new File( RuoYiConfig.getProfile() + prefixPath);
80 76
         if (!profilePath.exists())

+ 1
- 11
llm-back/sql/cmc_llm.sql 查看文件

@@ -57,6 +57,7 @@ CREATE TABLE `cmc_document`  (
57 57
 DROP TABLE IF EXISTS `cmc_topic`;
58 58
 CREATE TABLE `cmc_topic`  (
59 59
   `topic_id` char(19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '聊天主题id',
60
+  `agent_id` int DEFAULT 0 COMMENT '智能体id',
60 61
   `topic` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '主题',
61 62
   `create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
62 63
   PRIMARY KEY (`topic_id`) USING BTREE
@@ -76,7 +77,6 @@ CREATE TABLE `cmc_agent`  (
76 77
   `description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '描述',
77 78
   `create_by` bigint NULL DEFAULT NULL COMMENT '创建人',
78 79
   `create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
79
-  `role_id` int NULL DEFAULT NULL COMMENT '角色id',
80 80
   PRIMARY KEY (`agent_id`) USING BTREE
81 81
 ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '智能体表' ROW_FORMAT = Dynamic;
82 82
 
@@ -84,22 +84,6 @@ CREATE TABLE `cmc_agent`  (
84 84
 -- Records of cmc_agent
85 85
 -- ----------------------------
86 86
 
87
-DROP TABLE IF EXISTS `cmc_agent_tool`;
88
-CREATE TABLE `cmc_agent_tool`  (
89
-  `agent_tool_id` int NOT NULL COMMENT '智能体工具id',
90
-  `tool_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '工具描述',
91
-  `agent_id` int NULL DEFAULT NULL COMMENT '智能体id',
92
-  `tool_sort` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '顺序',
93
-  PRIMARY KEY (`agent_tool_id`) USING BTREE
94
-) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '智能体工具表' ROW_FORMAT = Dynamic;
95
-
96
-
97 87
 -- ----------------------------
98 88
 -- Table structure for gen_table
99 89
 -- ----------------------------

+ 3
- 4
llm-ui/src/api/llm/document.js 查看文件

@@ -1,8 +1,8 @@
1 1
 /*
2 2
  * @Author: ysh
3 3
  * @Date: 2025-06-13 10:35:45
4
- * @LastEditors: 
5
- * @LastEditTime: 2025-07-22 10:38:57
4
+ * @LastEditors: wrh
5
+ * @LastEditTime: 2025-07-29 17:48:55
6 6
  */
7 7
 import request from '@/utils/request'
8 8
 
@@ -51,12 +51,11 @@ export function delDocument(documentId) {
51 51
 
52 52
 
53 53
 // 上传聊天外部文件
54
-export function uploadDocument(fileList, agentName) {
54
+export function uploadDocument(fileList) {
55 55
   const formData = new FormData()
56 56
   for (let f of fileList) {
57 57
     formData.append('fileList', f)
58 58
   }
59
-  formData.append('agentName', agentName)
60 59
   return request({
61 60
     url: '/llm/document/upload',
62 61
     method: 'post',

+ 6
- 7
llm-ui/src/views/llm/agent/AgentDetail.vue 查看文件

@@ -1,8 +1,8 @@
1 1
 <!--
2 2
  * @Author: wrh
3 3
  * @Date: 2025-01-01 00:00:00
4
- * @LastEditors: Please set LastEditors
5
- * @LastEditTime: 2025-07-29 16:58:28
4
+ * @LastEditors: wrh
5
+ * @LastEditTime: 2025-07-29 17:45:58
6 6
 -->
7 7
 <template>
8 8
   <div class="agent-detail-container" v-loading="loading">
@@ -165,7 +165,7 @@ import { getAgent, opening, uploadFile } from '@/api/llm/agent';
165 165
 import { answer } from '@/api/llm/mcp';
166 166
 import { listTopic, getTopic, delTopic, addTopic, updateTopic } from "@/api/llm/topic";
167 167
 import { listChat, addChat, updateChat } from "@/api/llm/chat";
168
-import { listDocument, uploadDocument } from "@/api/llm/document";
168
+import { listDocument } from "@/api/llm/document";
169 169
 import useUserStore from '@/store/modules/user'
170 170
 
171 171
 const { proxy } = getCurrentInstance()
@@ -407,13 +407,12 @@ const submitChatUpload = async () => {
407 407
     const file = chatFileList.value[0].raw // 只取第一个文件
408 408
     const fileName = file.name;
409 409
     const response = await uploadFile(file, agentInfo.value.agentName)
410
-    const fileRes = await uploadDocument([file]); // 保存上传的文件到数据库
411
-    const chatId = fileRes.data.chatId; //获取保存后的chatId
410
+    const chatId = response.data.chatId; //获取保存后的chatId
412 411
     // 解析返回的数据
413 412
     let assistantContent = '文件上传成功!'
414
-    if (response.data && response.data.resultContent) {
413
+    if (response.data && response.data.assistantMessage) {
415 414
       // 格式化链接:在href前加上基础API地址
416
-      assistantContent = formatContentLinks(response.data.resultContent)
415
+      assistantContent = formatContentLinks(response.data.assistantMessage)
417 416
     }
418 417
 
419 418
     // 添加上传成功的消息到聊天记录

+ 2
- 2
llm-ui/src/views/llm/chat/index.vue 查看文件

@@ -212,7 +212,7 @@
212 212
                         <el-icon class="file-icon">
213 213
                           <Document />
214 214
                         </el-icon>
215
-                        <span class="file-name">{{ file.path }}</span>
215
+                        <span class="file-name">{{ file.path.split("/")[file.path.split("/").length - 1] }}</span>
216 216
                       </div>
217 217
                     </div>
218 218
                   </div>
@@ -590,7 +590,7 @@ const handleFileSelect = async (event) => {
590 590
     // 立即上传文件
591 591
     try {
592 592
       isUploading.value = true;
593
-      let res = await uploadDocument(files, '');
593
+      let res = await uploadDocument(files);
594 594
       documentChatId.value = res.data.chatId;
595 595
       // 上传成功后,将文件添加到已上传列表
596 596
       selectedFiles.value = [...selectedFiles.value, ...files];

Loading…
取消
儲存