2026-04-28 35f74c2abd95e6913180fbccd864915486eecc48
src/main/java/com/ruoyi/ai/config/XiaozhiAgentConfig.java
@@ -1,24 +1,15 @@
package com.ruoyi.ai.config;
import com.ruoyi.ai.store.MongoChatMemoryStore;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Collections;
import java.util.List;
/**
 * @author :yys
@@ -35,8 +26,8 @@
    @Autowired
    private EmbeddingModel embeddingModel;
    @Value("${knowledge.one}")
    private String one;
//    @Value("${knowledge.one}")
//    private String one;
//
//    @Value("${knowledge.two}")
//    private String two;
@@ -53,33 +44,33 @@
                .build();
    }
    @Bean
    ContentRetriever contentRetrieverXiaozhi() {
        //使用FileSystemDocumentLoader读取指定目录下的知识库文档
        //并使用默认的文档解析器对文档进行解析
        Document document1 = FileSystemDocumentLoader.loadDocument(one);
//        Document document2 = FileSystemDocumentLoader.loadDocument(two);
//        Document document3 = FileSystemDocumentLoader.loadDocument(three);
//        List<Document> documents = Arrays.asList(document1, document2, document3);
        List<Document> documents = Collections.singletonList(document1);
//         2. 将数据库数据转为LangChain4j的Document对象
//        List<Document> documents = new ArrayList<>();
        //使用内存向量存储
        InMemoryEmbeddingStore<TextSegment> inMemoryEmbeddingStore = new InMemoryEmbeddingStore<>();
        //使用默认的文档分割器
        EmbeddingStoreIngestor.builder()
                .embeddingModel(embeddingModel)
                .embeddingStore(inMemoryEmbeddingStore)
                .build()
                .ingest(documents);
        //从嵌入存储(EmbeddingStore)里检索和查询内容相关的信息
        return EmbeddingStoreContentRetriever.builder()
                .embeddingModel(embeddingModel)
                .embeddingStore(inMemoryEmbeddingStore)
                .build();
    }
//    @Bean
//    ContentRetriever contentRetrieverXiaozhi() {
//        //使用FileSystemDocumentLoader读取指定目录下的知识库文档
//        //并使用默认的文档解析器对文档进行解析
////        Document document1 = FileSystemDocumentLoader.loadDocument(one);
////        Document document2 = FileSystemDocumentLoader.loadDocument(two);
////        Document document3 = FileSystemDocumentLoader.loadDocument(three);
////        List<Document> documents = Arrays.asList(document1, document2, document3);
//
////        List<Document> documents = Collections.singletonList(document1);
////         2. 将数据库数据转为LangChain4j的Document对象
////        List<Document> documents = new ArrayList<>();
//
//        //使用内存向量存储
//        InMemoryEmbeddingStore<TextSegment> inMemoryEmbeddingStore = new InMemoryEmbeddingStore<>();
//        //使用默认的文档分割器
//        EmbeddingStoreIngestor.builder()
//                .embeddingModel(embeddingModel)
//                .embeddingStore(inMemoryEmbeddingStore)
//                .build()
//                .ingest(documents);
//        //从嵌入存储(EmbeddingStore)里检索和查询内容相关的信息
//        return EmbeddingStoreContentRetriever.builder()
//                .embeddingModel(embeddingModel)
//                .embeddingStore(inMemoryEmbeddingStore)
//                .build();
//    }
    @Bean
    ContentRetriever contentRetrieverXiaozhiPincone() {