2025-03-29 14:18:18 +00:00
|
|
|
package com.ai.config;
|
|
|
|
|
2025-03-29 15:46:43 +00:00
|
|
|
import com.ai.function.MyCalculator;
|
2025-03-29 14:18:18 +00:00
|
|
|
import com.ai.service.Assist;
|
|
|
|
import dev.langchain4j.community.model.dashscope.QwenChatModel;
|
2025-03-29 14:57:09 +00:00
|
|
|
import dev.langchain4j.data.document.Document;
|
|
|
|
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
|
|
|
|
import dev.langchain4j.data.segment.TextSegment;
|
2025-03-29 14:18:18 +00:00
|
|
|
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
|
|
|
|
import dev.langchain4j.model.chat.ChatLanguageModel;
|
2025-03-29 14:57:09 +00:00
|
|
|
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
|
2025-03-29 14:18:18 +00:00
|
|
|
import dev.langchain4j.service.AiServices;
|
2025-03-29 14:57:09 +00:00
|
|
|
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
|
|
|
|
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
|
2025-03-29 14:18:18 +00:00
|
|
|
import lombok.RequiredArgsConstructor;
|
2025-03-29 15:54:35 +00:00
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
2025-03-29 14:18:18 +00:00
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
2025-03-29 14:57:09 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2025-03-29 14:18:18 +00:00
|
|
|
@Configuration
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
public class AssistantInit {
|
|
|
|
|
2025-03-29 14:57:09 +00:00
|
|
|
// All files in a directory, txt seems to be faster
|
2025-03-29 14:18:18 +00:00
|
|
|
|
2025-03-29 15:54:35 +00:00
|
|
|
@Value("${langchain4j.api-key}")
|
|
|
|
private String apiKey;
|
|
|
|
|
|
|
|
@Value("${langchain4j.model}")
|
|
|
|
private String model;
|
|
|
|
|
2025-03-29 14:18:18 +00:00
|
|
|
@Bean
|
|
|
|
public Assist init() {
|
2025-03-29 14:57:09 +00:00
|
|
|
ChatLanguageModel qwenModel = QwenChatModel.builder()
|
2025-03-29 15:54:35 +00:00
|
|
|
.apiKey(apiKey)
|
|
|
|
.modelName(model)
|
2025-03-29 14:57:09 +00:00
|
|
|
.build();
|
|
|
|
List<Document> documents = FileSystemDocumentLoader.loadDocuments("E:\\ideaProject\\liang-ai");
|
|
|
|
// for simplicity, we will use an in-memory one:
|
|
|
|
InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
|
|
|
|
EmbeddingStoreIngestor.ingest(documents, embeddingStore);
|
2025-03-29 14:18:18 +00:00
|
|
|
return AiServices.builder(Assist.class)
|
|
|
|
.chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))
|
2025-03-29 14:57:09 +00:00
|
|
|
.chatLanguageModel(qwenModel)
|
2025-03-29 15:46:43 +00:00
|
|
|
.contentRetriever(EmbeddingStoreContentRetriever.from(embeddingStore))
|
2025-03-29 15:54:35 +00:00
|
|
|
// .tools(new MyCalculator())
|
2025-03-29 15:46:43 +00:00
|
|
|
.build();
|
2025-03-29 14:18:18 +00:00
|
|
|
}
|
|
|
|
}
|