| | |
| | | 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; |
| | | |
| | | /** |
| | | * AI Agent 配置类 |
| | | * 知识库检索使用数据库管理的向量数据,通过 KnowledgeBaseVector 表管理文件向量记录 |
| | | * |
| | | * @author :yys |
| | | * @date : 2025/5/2 20:01 |
| | | */ |
| | |
| | | @Autowired |
| | | private EmbeddingModel embeddingModel; |
| | | |
| | | @Value("${knowledge.one}") |
| | | private String one; |
| | | // |
| | | // @Value("${knowledge.two}") |
| | | // private String two; |
| | | // |
| | | // @Value("${knowledge.three}") |
| | | // private String three; |
| | | |
| | | @Bean |
| | | ChatMemoryProvider chatMemoryProviderXiaozhi() { |
| | | return memoryId -> MessageWindowChatMemory.builder() |
| | |
| | | .build(); |
| | | } |
| | | |
| | | /** |
| | | * 知识库内容检索器 |
| | | * 从向量数据库(Pinecone)检索已向量化的知识库内容 |
| | | * 知识库文件通过 KnowledgeBaseVector 表管理,由 KnowledgeRagService 处理向量化 |
| | | */ |
| | | @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() { |
| | | // 创建一个 EmbeddingStoreContentRetriever 对象,用于从嵌入存储中检索内容 |
| | | return EmbeddingStoreContentRetriever |
| | | .builder() |
| | | // 设置用于生成嵌入向量的嵌入模型 |
| | | .embeddingModel(embeddingModel) |
| | | // 指定要使用的嵌入存储 |
| | | .embeddingStore(embeddingStore) |
| | | // 设置最大检索结果数量,这里表示最多返回 1 条匹配结果 |
| | | .maxResults(1) |
| | | // 设置最小得分阈值,只有得分大于等于 0.8 的结果才会被返回 |
| | | .minScore(0.8) |
| | | // 构建最终的 EmbeddingStoreContentRetriever 实例 |
| | | .build(); |
| | | } |
| | | } |