2026-06-08 29ccd9919082e0157f57989ae83b303f314bad6b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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();
    }
}