Springboot+langchain4j的RAG检索增强生成

最终传递给构造器一个由springboot自动管理的自己配置的Bean容器即可

.contentRetriever(contentRetriever)

,所有的努力都是为了这个CR,作用是从你的文档知识库中获取到与你提问最相符的一个文档片段,其中包括了把文档切片,变成向量存入数据库中,把用户的问题通过Embedding模型进行向量转化,与文档向量进行相似度匹配,过滤,放入Rank模型,排出123来

java 复制代码
@Configuration
public class RagConfig {

    @Resource
    private EmbeddingModel qwenEmbeddingModel;

    @Bean
    public EmbeddingStore<TextSegment> embeddingStore() {
        // 这里使用内存存储,重启后数据会丢失。
        // 如果需要持久化,后续可以换成 Chroma, Milvus, PGVector 等
        return new InMemoryEmbeddingStore<>();
    }

    @Bean
    public ContentRetriever contentRetriever(EmbeddingStore<TextSegment> embeddingStore){
        //加载文档
        List<Document> documents = FileSystemDocumentLoader.loadDocuments("src/main/resources/doc");
        //文档切割器
        DocumentByParagraphSplitter documentByParagraphSplitter =
                new DocumentByParagraphSplitter(1000, 200);
        //文档加载器
        EmbeddingStoreIngestor ingestor = EmbeddingStoreIngestor.builder()
                .documentSplitter(documentByParagraphSplitter)
                .embeddingModel(qwenEmbeddingModel)
                .textSegmentTransformer(textSegment -> TextSegment.from(
                        textSegment.metadata().getString("file_name")
                                + '\n' + textSegment.text(), textSegment.metadata()))
                .embeddingStore(embeddingStore)
                .build();
        //加载文档
        ingestor.ingest(documents);
        //存到向量数据库中:
        return EmbeddingStoreContentRetriever.builder()
                .embeddingStore(embeddingStore)
                .embeddingModel(qwenEmbeddingModel)
                .maxResults(5) //检索五条
                .minScore(0.75) //匹配度
                .build();

    }
}
相关推荐
2301_7890156212 小时前
DS进阶:AVL树
开发语言·数据结构·c++·算法
曹牧12 小时前
BeanUtils.copyProperties‌
java
changhong198613 小时前
如何在 Spring Boot 中配置数据库?
数据库·spring boot·后端
QWQ___qwq13 小时前
Java线程安全深度总结:基本类型与引用类型的本质区别
java·安全·面试
Filotimo_13 小时前
5.3 Internet基础知识
开发语言·php
闽农13 小时前
Cursor taking longer than expected 问题这样解决
agent·ai编程·cursor·taking longer
识君啊13 小时前
Java异常处理:中小厂面试通关指南
java·开发语言·面试·异常处理·exception·中小厂
Lei活在当下14 小时前
Codex 工程化实践指南:深入理解 AGENTS.md、SKILL.md 与 MCP
android·openai·ai编程
qyzm15 小时前
天梯赛练习(3月13日)
开发语言·数据结构·python·算法·贪心算法
月月玩代码15 小时前
Actuator,Spring Boot应用监控与管理端点!
java·spring boot·后端