ソースを参照

直接使用大模型会话

lamphua 1日前
コミット
6167c4474d

+ 4
- 5
llm-back/cmc-agent/pom.xml ファイルの表示

@@ -65,14 +65,13 @@
65 65
             <version>0.35.0</version>
66 66
         </dependency>
67 67
 
68
-        <!-- 解析PDF -->
68
+        <!-- LangChain4j pdfParser 集成 -->
69 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 73
         </dependency>
74 74
 
75
-
76 75
     </dependencies>
77 76
 
78 77
     <build>

+ 2
- 2
llm-back/cmc-agent/src/main/java/com/cmc/agent/controller/KnowLedgeController.java ファイルの表示

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

+ 5
- 5
llm-back/cmc-agent/src/main/java/com/cmc/agent/controller/RagController.java ファイルの表示

@@ -28,18 +28,18 @@ public class RagController extends BaseController
28 28
      * 增强检索生成回答
29 29
      */
30 30
     @PostMapping("/answer")
31
-    public AjaxResult answerWithRAG(String question, String collectionName) throws IOException {
31
+    public AjaxResult answer(String question, String collectionName) throws IOException {
32 32
         LangChainMilvusService langChainMilvusService = new LangChainMilvusService(
33
-                "localhost",
33
+                "192.168.28.188",
34 34
                 19530,
35 35
                 collectionName,
36 36
                 embeddingModel);
37 37
 
38
-        // 2. Milvus检索
38
+        // 1. Milvus检索
39 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 ファイルの表示

@@ -0,0 +1,55 @@
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 ファイルの表示

@@ -31,10 +31,10 @@ import java.util.*;
31 31
 import java.util.stream.Collectors;
32 32
 
33 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 40
      * 连接milvus知识库,加入langchain自带emdding
@@ -46,10 +46,24 @@ public class LangChainMilvusService {
46 46
                         .withPort(port)
47 47
                         .build()
48 48
         );
49
+        this.LLM_SERVICE_URL = "http://" + host + ":8000/generate";
49 50
         this.collectionName = collectionName;
50 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 68
      * 新建知识库Collection(含Schema、Field、Index)
55 69
      */
@@ -158,8 +172,8 @@ public class LangChainMilvusService {
158 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 177
         String prompt = buildPrompt(question, contexts);
164 178
         Gson gson = new Gson();
165 179
         Map<String, String> hashMap = new HashMap<>();
@@ -175,7 +189,7 @@ public class LangChainMilvusService {
175 189
                 .build();
176 190
 
177 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
 

読み込み中…
キャンセル
保存