|
@@ -1,20 +1,19 @@
|
1
|
1
|
package com.ruoyi.web.llm.controller;
|
2
|
2
|
|
|
3
|
+import com.alibaba.fastjson2.JSONObject;
|
|
4
|
+import com.ruoyi.common.config.RuoYiConfig;
|
3
|
5
|
import com.ruoyi.common.core.controller.BaseController;
|
4
|
|
-import com.ruoyi.web.llm.service.ILangChainMilvusService;
|
5
|
6
|
import org.noear.solon.ai.chat.ChatModel;
|
6
|
7
|
import org.noear.solon.ai.chat.ChatResponse;
|
7
|
8
|
import org.noear.solon.ai.chat.message.AssistantMessage;
|
8
|
9
|
import org.noear.solon.ai.chat.message.ChatMessage;
|
9
|
|
-import org.noear.solon.ai.chat.tool.FunctionToolDesc;
|
10
|
10
|
import org.noear.solon.ai.mcp.client.McpClientProvider;
|
11
|
|
-import org.reactivestreams.Publisher;
|
12
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
13
|
11
|
import org.springframework.web.bind.annotation.GetMapping;
|
14
|
12
|
import org.springframework.web.bind.annotation.RequestMapping;
|
15
|
13
|
import org.springframework.web.bind.annotation.RestController;
|
16
|
|
-import reactor.core.publisher.Flux;
|
|
14
|
+import org.springframework.web.multipart.MultipartFile;
|
17
|
15
|
|
|
16
|
+import java.io.File;
|
18
|
17
|
import java.io.IOException;
|
19
|
18
|
import java.util.HashMap;
|
20
|
19
|
import java.util.List;
|
|
@@ -31,31 +30,34 @@ import java.util.Map;
|
31
|
30
|
@RequestMapping("/llm/mcp")
|
32
|
31
|
public class McpController extends BaseController
|
33
|
32
|
{
|
34
|
|
- @Autowired
|
35
|
|
- private ILangChainMilvusService langChainMilvusService;
|
36
|
|
-
|
37
|
33
|
/**
|
38
|
|
- * 打开word文档
|
|
34
|
+ * 同步问答
|
39
|
35
|
* @return
|
40
|
36
|
*/
|
41
|
37
|
@GetMapping("/answer")
|
42
|
|
- public Flux<AssistantMessage> answer(String question) throws IOException {
|
|
38
|
+ public AssistantMessage answer(String question) throws IOException {
|
43
|
39
|
McpClientProvider clientProvider = McpClientProvider.builder()
|
44
|
|
- .apiUrl("http://localhost:8080/llm/mcp/sse")
|
45
|
|
- .build();
|
|
40
|
+ .apiUrl("http://localhost:8080/llm/mcp/sse")
|
|
41
|
+ .build();
|
46
|
42
|
|
47
|
43
|
ChatModel chatModel = ChatModel.of("http://192.168.28.188:8000/v1/chat/completions")
|
48
|
44
|
.provider("openai")
|
49
|
|
- .model("DeepSeek-R1-Distill-Qwen-1.5B")
|
|
45
|
+ .model("Qwen2.5-1.5B-Instruct")
|
50
|
46
|
.apiKey("1")
|
51
|
47
|
.defaultToolsAdd(clientProvider)
|
52
|
48
|
.build();
|
53
|
|
- Publisher<ChatResponse> publisher = chatModel.prompt(question).stream();
|
54
|
|
-
|
55
|
|
- return Flux.from(publisher)
|
56
|
|
- .map(response -> {
|
57
|
|
- return response.getChoices().get(0).getMessage();
|
58
|
|
- });
|
|
49
|
+ ChatResponse response = chatModel.prompt(question).call();
|
|
50
|
+ String resultContent = response.lastChoice().getMessage().getResultContent();
|
|
51
|
+ AssistantMessage assistantMessage = new AssistantMessage(resultContent);
|
|
52
|
+ if (resultContent.startsWith("<tool_call>")) {
|
|
53
|
+ String content = resultContent.replace("<tool_call>\n", "").replace("\n</tool_call>", "");
|
|
54
|
+ JSONObject jsonObject = JSONObject.parseObject(content);
|
|
55
|
+ String name = jsonObject.getString("name");
|
|
56
|
+ JSONObject arguments = jsonObject.getJSONObject("arguments");
|
|
57
|
+ resultContent = clientProvider.callToolAsText(name, arguments).getContent();
|
|
58
|
+ assistantMessage = new AssistantMessage(resultContent);
|
|
59
|
+ }
|
|
60
|
+ return assistantMessage;
|
59
|
61
|
}
|
60
|
62
|
|
61
|
63
|
/**
|
|
@@ -63,7 +65,7 @@ public class McpController extends BaseController
|
63
|
65
|
* @return
|
64
|
66
|
*/
|
65
|
67
|
@GetMapping("/tech")
|
66
|
|
- public AssistantMessage tech(String file) throws IOException {
|
|
68
|
+ public AssistantMessage tech(MultipartFile file) throws IOException {
|
67
|
69
|
McpClientProvider clientProvider = McpClientProvider.builder()
|
68
|
70
|
.apiUrl("http://localhost:8080/llm/mcp/sse")
|
69
|
71
|
.build();
|
|
@@ -73,19 +75,25 @@ public class McpController extends BaseController
|
73
|
75
|
.apiKey("1")
|
74
|
76
|
.defaultToolsAdd(clientProvider)
|
75
|
77
|
.build();
|
76
|
|
-
|
|
78
|
+ File profilePath = new File( RuoYiConfig.getProfile() + "/upload/knowledge");
|
|
79
|
+ if (!profilePath.exists())
|
|
80
|
+ profilePath.mkdirs();
|
|
81
|
+ File transferFile = new File( profilePath + File.separator + file.getOriginalFilename());
|
|
82
|
+ if (!transferFile.exists()) {
|
|
83
|
+ file.transferTo(transferFile);
|
|
84
|
+ }
|
77
|
85
|
Map<String,Object> path = new HashMap<>();
|
78
|
|
- path.put("document", file);
|
|
86
|
+ path.put("document", transferFile.getPath());
|
79
|
87
|
String content = clientProvider.callToolAsText("openDocument", path).getContent();
|
80
|
88
|
Map<String,Object> request = new HashMap<>();
|
81
|
89
|
request.put("request", content);
|
82
|
90
|
List<ChatMessage> messages = clientProvider.getPromptAsMessages("askQuestion", request);
|
83
|
91
|
ChatResponse response = chatModel.prompt(messages).call();
|
84
|
92
|
System.out.println(response.getChoices().get(0).getMessage());
|
85
|
|
- Map<String,Object> write = new HashMap<>();
|
86
|
|
- write.put("document", file);
|
87
|
|
- write.put("resultContent", response.getChoices().get(0).getMessage().getResultContent());
|
88
|
|
- clientProvider.callToolAsText("writeTechnicalPlan", write);
|
|
93
|
+// Map<String,Object> write = new HashMap<>();
|
|
94
|
+// write.put("document", transferFile.getPath());
|
|
95
|
+// write.put("resultContent", response.getChoices().get(0).getMessage().getResultContent());
|
|
96
|
+// String res = clientProvider.callToolAsText("writeTechnicalPlan", write).getContent();
|
89
|
97
|
return response.getChoices().get(0).getMessage();
|
90
|
98
|
}
|
91
|
99
|
|