20250408 增加向量数据库milvus配置,将知识库内容向量化后保存到milvus中
This commit is contained in:
parent
228fb4a94e
commit
eb9c01e74e
11
pom.xml
11
pom.xml
@ -56,6 +56,17 @@
|
|||||||
<version>1.0.0-beta1</version>
|
<version>1.0.0-beta1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>dev.langchain4j</groupId>
|
||||||
|
<artifactId>langchain4j-milvus</artifactId>
|
||||||
|
<version>1.0.0-beta1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>dev.langchain4j</groupId>
|
||||||
|
<artifactId>langchain4j-embeddings-all-minilm-l6-v2</artifactId>
|
||||||
|
<version>1.0.0-beta1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>dev.langchain4j</groupId>
|
<groupId>dev.langchain4j</groupId>
|
||||||
<artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
|
<artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
|
||||||
|
@ -39,15 +39,10 @@ public class AssistantInit {
|
|||||||
ChatLanguageModel qwenModel = QwenChatModel.builder()
|
ChatLanguageModel qwenModel = QwenChatModel.builder()
|
||||||
.apiKey(apiKey)
|
.apiKey(apiKey)
|
||||||
.modelName(model)
|
.modelName(model)
|
||||||
.build();
|
.build();// for simplicity, we will use an in-memory one:
|
||||||
List<Document> documents = FileSystemDocumentLoader.loadDocuments(ragPath);
|
|
||||||
// for simplicity, we will use an in-memory one:
|
|
||||||
InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
|
|
||||||
EmbeddingStoreIngestor.ingest(documents, embeddingStore);
|
|
||||||
return AiServices.builder(Assist.class)
|
return AiServices.builder(Assist.class)
|
||||||
.chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))
|
.chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))
|
||||||
.chatLanguageModel(qwenModel)
|
.chatLanguageModel(qwenModel)
|
||||||
.contentRetriever(EmbeddingStoreContentRetriever.from(embeddingStore))
|
|
||||||
// .tools(new MyCalculator())
|
// .tools(new MyCalculator())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,11 @@ import dev.langchain4j.model.chat.ChatLanguageModel;
|
|||||||
import dev.langchain4j.rag.content.retriever.ContentRetriever;
|
import dev.langchain4j.rag.content.retriever.ContentRetriever;
|
||||||
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
|
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
|
||||||
import dev.langchain4j.service.AiServices;
|
import dev.langchain4j.service.AiServices;
|
||||||
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
|
import dev.langchain4j.store.embedding.EmbeddingStore;
|
||||||
|
import dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore;
|
||||||
|
import io.milvus.common.clientenum.ConsistencyLevelEnum;
|
||||||
|
import io.milvus.param.IndexType;
|
||||||
|
import io.milvus.param.MetricType;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
@ -41,7 +45,21 @@ public class SegmentConfig {
|
|||||||
.modelName(model)
|
.modelName(model)
|
||||||
.build();
|
.build();
|
||||||
QwenEmbeddingModel embeddingModel = QwenEmbeddingModel.builder().apiKey(apiKey).build();
|
QwenEmbeddingModel embeddingModel = QwenEmbeddingModel.builder().apiKey(apiKey).build();
|
||||||
InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
|
// InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
|
||||||
|
EmbeddingStore<TextSegment> embeddingStore = MilvusEmbeddingStore.builder()
|
||||||
|
.host("localhost")
|
||||||
|
.port(19530)
|
||||||
|
.collectionName("langchain4j_collection")
|
||||||
|
.dimension(1536)
|
||||||
|
.indexType(IndexType.FLAT)
|
||||||
|
.metricType(MetricType.COSINE)
|
||||||
|
.consistencyLevel(ConsistencyLevelEnum.EVENTUALLY)
|
||||||
|
.autoFlushOnInsert(true)
|
||||||
|
.idFieldName("id")
|
||||||
|
.textFieldName("text")
|
||||||
|
.metadataFieldName("metadata")
|
||||||
|
.vectorFieldName("vector")
|
||||||
|
.build();
|
||||||
List<Document> documents = FileSystemDocumentLoader.loadDocuments(ragPath);
|
List<Document> documents = FileSystemDocumentLoader.loadDocuments(ragPath);
|
||||||
for (Document document : documents) {
|
for (Document document : documents) {
|
||||||
DocumentByLineSplitter splitter = new DocumentByLineSplitter(300,30);
|
DocumentByLineSplitter splitter = new DocumentByLineSplitter(300,30);
|
||||||
|
@ -95,7 +95,7 @@ public class LangChainController {
|
|||||||
@GetMapping("/embedd/chat")
|
@GetMapping("/embedd/chat")
|
||||||
public String embeddChat(@RequestParam("input") String input) {
|
public String embeddChat(@RequestParam("input") String input) {
|
||||||
System.out.println("start embedd chat...");
|
System.out.println("start embedd chat...");
|
||||||
embeddingService.embedding(input);
|
// embeddingService.embedding(input);
|
||||||
return segmentAssist.chat(input);
|
return segmentAssist.chat(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user