package com.ruoyi.ai.config; import com.ruoyi.ai.store.MongoChatMemoryStore; 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 org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * AI Agent 配置类 * 知识库检索使用数据库管理的向量数据,通过 KnowledgeBaseVector 表管理文件向量记录 * * @author :yys * @date : 2025/5/2 20:01 */ @Configuration public class XiaozhiAgentConfig { @Autowired private MongoChatMemoryStore mongoChatMemoryStore; @Autowired private EmbeddingStore embeddingStore; @Autowired private EmbeddingModel embeddingModel; @Bean ChatMemoryProvider chatMemoryProviderXiaozhi() { return memoryId -> MessageWindowChatMemory.builder() .id(memoryId) .maxMessages(20) .chatMemoryStore(mongoChatMemoryStore) .build(); } /** * 知识库内容检索器 * 从向量数据库(Pinecone)检索已向量化的知识库内容 * 知识库文件通过 KnowledgeBaseVector 表管理,由 KnowledgeRagService 处理向量化 */ @Bean ContentRetriever contentRetrieverXiaozhi() { return EmbeddingStoreContentRetriever .builder() .embeddingModel(embeddingModel) .embeddingStore(embeddingStore) .maxResults(1) .minScore(0.8) .build(); } }