Browse Source

直接使用大模型会话

lamphua 2 months ago
parent
commit
6167c4474d

+ 4
- 5
llm-back/cmc-agent/pom.xml View File

65
             <version>0.35.0</version>
65
             <version>0.35.0</version>
66
         </dependency>
66
         </dependency>
67
 
67
 
68
-        <!-- 解析PDF -->
68
+        <!-- LangChain4j pdfParser 集成 -->
69
         <dependency>
69
         <dependency>
70
-            <groupId>org.apache.pdfbox</groupId>
71
-            <artifactId>pdfbox</artifactId>
72
-            <version>3.0.3</version>
70
+            <groupId>dev.langchain4j</groupId>
71
+            <artifactId>langchain4j-document-parser-apache-pdfbox</artifactId>
72
+            <version>0.35.0</version>
73
         </dependency>
73
         </dependency>
74
 
74
 
75
-
76
     </dependencies>
75
     </dependencies>
77
 
76
 
78
     <build>
77
     <build>

+ 2
- 2
llm-back/cmc-agent/src/main/java/com/cmc/agent/controller/KnowLedgeController.java View File

31
     public AjaxResult createKnowLedgeBase(String collectionName)
31
     public AjaxResult createKnowLedgeBase(String collectionName)
32
     {
32
     {
33
         LangChainMilvusService langChainMilvusService = new LangChainMilvusService(
33
         LangChainMilvusService langChainMilvusService = new LangChainMilvusService(
34
-                "localhost",
34
+                "192.168.28.188",
35
                 19530,
35
                 19530,
36
                 collectionName,
36
                 collectionName,
37
                 embeddingModel);
37
                 embeddingModel);
45
     @PostMapping("/insert")
45
     @PostMapping("/insert")
46
     public AjaxResult insertKnowledge(MultipartFile file, String collectionName) throws IOException {
46
     public AjaxResult insertKnowledge(MultipartFile file, String collectionName) throws IOException {
47
         LangChainMilvusService langChainMilvusService = new LangChainMilvusService(
47
         LangChainMilvusService langChainMilvusService = new LangChainMilvusService(
48
-                "localhost",
48
+                "192.168.28.188",
49
                 19530,
49
                 19530,
50
                 collectionName,
50
                 collectionName,
51
                 embeddingModel);
51
                 embeddingModel);

+ 5
- 5
llm-back/cmc-agent/src/main/java/com/cmc/agent/controller/RagController.java View File

28
      * 增强检索生成回答
28
      * 增强检索生成回答
29
      */
29
      */
30
     @PostMapping("/answer")
30
     @PostMapping("/answer")
31
-    public AjaxResult answerWithRAG(String question, String collectionName) throws IOException {
31
+    public AjaxResult answer(String question, String collectionName) throws IOException {
32
         LangChainMilvusService langChainMilvusService = new LangChainMilvusService(
32
         LangChainMilvusService langChainMilvusService = new LangChainMilvusService(
33
-                "localhost",
33
+                "192.168.28.188",
34
                 19530,
34
                 19530,
35
                 collectionName,
35
                 collectionName,
36
                 embeddingModel);
36
                 embeddingModel);
37
 
37
 
38
-        // 2. Milvus检索
38
+        // 1. Milvus检索
39
         List<String> contexts = langChainMilvusService.retrieveFromMilvus(question, 3);
39
         List<String> contexts = langChainMilvusService.retrieveFromMilvus(question, 3);
40
 
40
 
41
-        // 3. 调用本地LLM或HTTP服务
42
-        return success(langChainMilvusService.generateAnswer(question, contexts));
41
+        // 2. 调用本地LLM或HTTP服务
42
+        return success(langChainMilvusService.generateAnswerWithRag(question, contexts));
43
     }
43
     }
44
 
44
 
45
 }
45
 }

+ 55
- 0
llm-back/cmc-agent/src/main/java/com/cmc/agent/controller/SessionController.java View File

1
+package com.cmc.agent.controller;
2
+
3
+import com.google.gson.Gson;
4
+import com.ruoyi.common.core.controller.BaseController;
5
+import com.ruoyi.common.core.domain.AjaxResult;
6
+import okhttp3.*;
7
+import org.springframework.web.bind.annotation.PostMapping;
8
+import org.springframework.web.bind.annotation.RequestMapping;
9
+import org.springframework.web.bind.annotation.RestController;
10
+
11
+import java.io.IOException;
12
+import java.util.HashMap;
13
+import java.util.Map;
14
+
15
+/**
16
+ * cmc知识库Controller
17
+ * 
18
+ * @author cmc
19
+ * @date 2025-04-08
20
+ */
21
+@RestController
22
+@RequestMapping("/llm/session")
23
+public class SessionController extends BaseController
24
+{
25
+    /**
26
+     * 生成回答
27
+     */
28
+    @PostMapping("/answer")
29
+    public AjaxResult answer(String question) throws IOException {
30
+
31
+        // 1. 调用本地LLM或HTTP服务
32
+        return success(generateAnswer(question));
33
+    }
34
+
35
+    // 调用LLM生成回答
36
+    public String generateAnswer(String question) throws IOException {
37
+        Gson gson = new Gson();
38
+        Map<String, String> hashMap = new HashMap<>();
39
+        hashMap.put("prompt", question);
40
+        hashMap.put("max_tokens", "512");
41
+        RequestBody body = RequestBody.create(
42
+                MediaType.parse("application/json"),
43
+                new Gson().toJson(hashMap));
44
+
45
+        Request request = new Request.Builder()
46
+                .url("http://192.168.28.188:8080/generate")
47
+                .post(body)
48
+                .build();
49
+
50
+        try (Response response = new OkHttpClient().newCall(request).execute()) {
51
+            return gson.fromJson(response.body().string(), Map.class).get("generated_text").toString();
52
+        }
53
+    }
54
+
55
+}

+ 21
- 7
llm-back/cmc-agent/src/main/java/com/cmc/agent/service/LangChainMilvusService.java View File

31
 import java.util.stream.Collectors;
31
 import java.util.stream.Collectors;
32
 
32
 
33
 public class LangChainMilvusService {
33
 public class LangChainMilvusService {
34
-    private static final String LLM_SERVICE_URL = "http://localhost:8000/generate";
35
-    private final MilvusServiceClient milvusClient;
36
-    private final String collectionName;
37
-    private  EmbeddingModel embeddingModel;
34
+    private String LLM_SERVICE_URL;
35
+    private MilvusServiceClient milvusClient;
36
+    private String collectionName;
37
+    private EmbeddingModel embeddingModel;
38
 
38
 
39
     /**
39
     /**
40
      * 连接milvus知识库,加入langchain自带emdding
40
      * 连接milvus知识库,加入langchain自带emdding
46
                         .withPort(port)
46
                         .withPort(port)
47
                         .build()
47
                         .build()
48
         );
48
         );
49
+        this.LLM_SERVICE_URL = "http://" + host + ":8000/generate";
49
         this.collectionName = collectionName;
50
         this.collectionName = collectionName;
50
         this.embeddingModel = embeddingModel;
51
         this.embeddingModel = embeddingModel;
51
     }
52
     }
52
 
53
 
54
+    /**
55
+     * 不使用向量数据库
56
+     */
57
+    public LangChainMilvusService(String host, int port) {
58
+        this.milvusClient = new MilvusServiceClient(
59
+                ConnectParam.newBuilder()
60
+                        .withHost(host)
61
+                        .withPort(port)
62
+                        .build()
63
+        );
64
+        this.LLM_SERVICE_URL = "http://" + host + ":8000/generate";
65
+    }
66
+
53
     /**
67
     /**
54
      * 新建知识库Collection(含Schema、Field、Index)
68
      * 新建知识库Collection(含Schema、Field、Index)
55
      */
69
      */
158
                 .collect(Collectors.toList());
172
                 .collect(Collectors.toList());
159
     }
173
     }
160
 
174
 
161
-    // 调用LLM生成回答
162
-    public String generateAnswer(String question, List<String> contexts) throws IOException {
175
+    // 调用LLM+RAG生成回答
176
+    public String generateAnswerWithRag(String question, List<String> contexts) throws IOException {
163
         String prompt = buildPrompt(question, contexts);
177
         String prompt = buildPrompt(question, contexts);
164
         Gson gson = new Gson();
178
         Gson gson = new Gson();
165
         Map<String, String> hashMap = new HashMap<>();
179
         Map<String, String> hashMap = new HashMap<>();
175
                 .build();
189
                 .build();
176
 
190
 
177
         try (Response response = new OkHttpClient().newCall(request).execute()) {
191
         try (Response response = new OkHttpClient().newCall(request).execute()) {
178
-            return gson.fromJson(response.body().string(), Map.class).get("text").toString();
192
+            return gson.fromJson(response.body().string(), Map.class).get("generated_text").toString();
179
         }
193
         }
180
     }
194
     }
181
 
195
 

Loading…
Cancel
Save