Browse Source

修改智能体新增对话就出现历史对话

余思翰 2 days ago
parent
commit
66bdab9728

+ 0
- 46
llm-back/ruoyi-agent/src/main/java/com/ruoyi/agent/service/impl/McpServiceImpl.java View File

69
         return generateAnswerWithDocumentAndCollection(embeddingModel, templatePath, title, requests, "http://192.168.28.188:8000/v1/chat/completions");
69
         return generateAnswerWithDocumentAndCollection(embeddingModel, templatePath, title, requests, "http://192.168.28.188:8000/v1/chat/completions");
70
     }
70
     }
71
 
71
 
72
-    @ToolMapping(description = "梳理招标文件服务要求")
73
-    public String getBidRequest(@Param(description = "文件路径") String document) throws IOException {
74
-        try (XWPFDocument doc = new XWPFDocument(new FileInputStream(document))) {
75
-            StringBuilder content = new StringBuilder();
76
-            for (XWPFParagraph paragraph : doc.getParagraphs()) {
77
-                content.append(paragraph.getText()).append("\n");
78
-            }
79
-            return content.toString();
80
-        }
81
-    }
82
-
83
-    @ToolMapping(description = "撰写技术方案")
84
-    public String writeTechnicalPlan(@Param(description = "文件路径") String document, @Param(description = "技术方案内容") String resultContent) throws IOException {
85
-        // 创建临时输出文件路径
86
-
87
-        try (FileInputStream fis = new FileInputStream(document);
88
-             XWPFDocument doc = new XWPFDocument(fis);
89
-             FileOutputStream fos = new FileOutputStream(document)) {
90
-
91
-            // 在最后添加分页符和新内容
92
-            XWPFParagraph pageBreak = doc.createParagraph();
93
-            pageBreak.setPageBreak(true);
94
-
95
-            XWPFParagraph newContent = doc.createParagraph();
96
-            newContent.createRun().setText(resultContent);
97
-
98
-            // 保存文档
99
-            doc.write(fos);
100
-
101
-            return "技术方案添加完成,已保存到: " + document;
102
-        } catch (IOException e) {
103
-            throw new IOException("处理Word文档时出错: " + e.getMessage(), e);
104
-        }
105
-    }
106
-
107
-    @PromptMapping(description = "提出技术问题")
108
-    public Collection<ChatMessage> askQuestion(@Param(description = "描述招标文件服务要求") String request,
109
-                                               @Param(description = "参考上下文") String contexts,
110
-                                               @Param(description = "补充文字部分") String part) {
111
-        return Collections.singletonList(
112
-                ChatMessage.ofUser("请根据以下要求:'" + request + "\n" +
113
-                        "参考上下文:'" + contexts + "\n" +
114
-                        "回答以下问题:'" + part + "\n")
115
-        );
116
-    }
117
-
118
     /**
72
     /**
119
      * 从Milvus检索相关文档
73
      * 从Milvus检索相关文档
120
      * @return
74
      * @return

+ 1
- 1
llm-back/ruoyi-llm/src/main/java/com/ruoyi/web/llm/controller/McpController.java View File

114
             JSONObject jsonObject = JSONObject.parseObject(content);
114
             JSONObject jsonObject = JSONObject.parseObject(content);
115
             String name = jsonObject.getString("name");
115
             String name = jsonObject.getString("name");
116
             JSONObject arguments = jsonObject.getJSONObject("arguments");
116
             JSONObject arguments = jsonObject.getJSONObject("arguments");
117
-            if (arguments.getString("templatePath").contains("技术"))
117
+            if (arguments.getString("templatePath").contains("招标") || arguments.getString("templatePath").contains("询价"))
118
                 arguments.put("collectionName", "technical");
118
                 arguments.put("collectionName", "technical");
119
             resultContent = clientProvider.callToolAsText(name, arguments).getContent();
119
             resultContent = clientProvider.callToolAsText(name, arguments).getContent();
120
             assistantMessage = new AssistantMessage(resultContent);
120
             assistantMessage = new AssistantMessage(resultContent);

+ 54
- 21
llm-ui/src/views/llm/agent/AgentDetail.vue View File

1
 <!--
1
 <!--
2
  * @Author: wrh
2
  * @Author: wrh
3
  * @Date: 2025-01-01 00:00:00
3
  * @Date: 2025-01-01 00:00:00
4
- * @LastEditors: wrh
5
- * @LastEditTime: 2025-07-29 17:45:58
4
+ * @LastEditors: Please set LastEditors
5
+ * @LastEditTime: 2025-07-30 14:54:24
6
 -->
6
 -->
7
 <template>
7
 <template>
8
   <div class="agent-detail-container" v-loading="loading">
8
   <div class="agent-detail-container" v-loading="loading">
248
   }
248
   }
249
 }
249
 }
250
 
250
 
251
-// 选择话题
252
-const selectTopic = async (topic) => {
253
-  console.log('选择话题:', topic)
254
-  currentTopicId.value = topic.topicId
255
-  chatTitle.value = topic.topic
251
+// 加载指定话题的聊天记录(公共函数)
252
+const loadChatMessages = async (topicId) => {
256
   try {
253
   try {
257
-    // 加载该话题下的聊天记录
258
-    const res = await listChat({ topicId: topic.topicId });
254
+    const res = await listChat({ topicId: topicId });
259
     if (res.rows.length > 0) {
255
     if (res.rows.length > 0) {
260
       // 处理聊天消息格式
256
       // 处理聊天消息格式
261
       const messages = []
257
       const messages = []
265
           try {
261
           try {
266
             const fileResponse = await listDocument({ chatId: chat.chatId });
262
             const fileResponse = await listDocument({ chatId: chat.chatId });
267
             if (fileResponse.rows && fileResponse.rows.length > 0) {
263
             if (fileResponse.rows && fileResponse.rows.length > 0) {
268
-              const fileNames = fileResponse.rows.map(doc => doc.path).join(', ')
264
+              const fileNames = fileResponse.rows.map(doc => doc.path.split("/")[doc.path.split("/").length - 1]).join(', ');
269
               // 先添加文件信息消息
265
               // 先添加文件信息消息
270
               messages.push({
266
               messages.push({
271
                 role: 'assistant',
267
                 role: 'assistant',
272
                 content: `📎 相关文件: ${fileNames}`,
268
                 content: `📎 相关文件: ${fileNames}`,
273
-                timestamp: chat.inputTime || new Date(chat.createTime).toLocaleTimeString(),
274
                 isHtml: false,
269
                 isHtml: false,
275
                 isFileInfo: true // 标记为文件信息消息
270
                 isFileInfo: true // 标记为文件信息消息
276
               })
271
               })
292
         if (chat.output) {
287
         if (chat.output) {
293
           messages.push({
288
           messages.push({
294
             role: 'assistant',
289
             role: 'assistant',
295
-            content: formatContentLinks(chat.output),
290
+            content: chat.output,
296
             timestamp: chat.outputTime || new Date(chat.createTime).toLocaleTimeString(),
291
             timestamp: chat.outputTime || new Date(chat.createTime).toLocaleTimeString(),
297
             isHtml: true // 历史消息可能包含HTML内容
292
             isHtml: true // 历史消息可能包含HTML内容
298
           })
293
           })
300
       }
295
       }
301
       // 更新聊天消息
296
       // 更新聊天消息
302
       chatMessages.value = messages
297
       chatMessages.value = messages
303
-      // 滚动到底部
304
-      nextTick(() => {
305
-        scrollToBottom()
306
-      })
307
-      ElMessage.success(`已加载话题"${topic.topic}"的对话记录`)
298
+      return messages.length > 0
308
     } else {
299
     } else {
309
       chatMessages.value = []
300
       chatMessages.value = []
310
-      ElMessage.info('该话题暂无对话记录')
301
+      return false
311
     }
302
     }
312
   } catch (error) {
303
   } catch (error) {
313
     console.error('加载话题对话记录失败:', error)
304
     console.error('加载话题对话记录失败:', error)
305
+    chatMessages.value = []
306
+    throw error
307
+  }
308
+}
309
+
310
+// 选择话题
311
+const selectTopic = async (topic) => {
312
+  console.log('选择话题:', topic)
313
+  currentTopicId.value = topic.topicId
314
+  chatTitle.value = topic.topic
315
+
316
+  try {
317
+    const hasMessages = await loadChatMessages(topic.topicId)
318
+
319
+    // 滚动到底部
320
+    nextTick(() => {
321
+      scrollToBottom()
322
+    })
323
+
324
+    if (hasMessages) {
325
+      ElMessage.success(`已加载话题"${topic.topic}"的对话记录`)
326
+    } else {
327
+      ElMessage.info('该话题暂无对话记录')
328
+    }
329
+  } catch (error) {
314
     ElMessage.error('加载对话记录失败')
330
     ElMessage.error('加载对话记录失败')
315
   }
331
   }
316
 }
332
 }
338
   try {
354
   try {
339
     // 调用智能体回答API
355
     // 调用智能体回答API
340
     const response = await answer({
356
     const response = await answer({
341
-      query: message,
342
-      agentName: agentInfo.value.agentName
357
+      topicId: currentTopicId.value,
358
+      question: message
343
     })
359
     })
344
 
360
 
345
     // 添加助手回复
361
     // 添加助手回复
426
     ElMessage.success('文件上传成功');
442
     ElMessage.success('文件上传成功');
427
     let topicRes = await addTopic({ agentId: props.agentId, topic: fileName });
443
     let topicRes = await addTopic({ agentId: props.agentId, topic: fileName });
428
     const topicId = topicRes.msg;
444
     const topicId = topicRes.msg;
429
-    await addChat({ userId: userStore.id, chatId, topicId, output: assistantContent, outputTime: proxy.parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}') });
445
+    await updateChat({ userId: userStore.id, chatId, topicId, output: assistantContent, outputTime: proxy.parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}') });
446
+
447
+    // 刷新话题列表
448
+    await loadTopic();
449
+
450
+    // 激活当前话题并加载聊天记录
451
+    currentTopicId.value = topicId;
452
+    chatTitle.value = fileName;
453
+
454
+    // 重新加载该话题的聊天记录以显示文件相关内容
455
+    try {
456
+      await loadChatMessages(topicId)
457
+    } catch (error) {
458
+      console.error('加载话题聊天记录失败:', error)
459
+    }
460
+
461
+    // 清空文件上传列表
462
+    chatFileList.value = [];
430
 
463
 
431
     nextTick(() => {
464
     nextTick(() => {
432
       scrollToBottom()
465
       scrollToBottom()

Loading…
Cancel
Save