|
|
@@ -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
|
|