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();

    }
}
相关推荐
俊哥V25 分钟前
每日 AI 研究简报 · 2026-10-19
人工智能·ai
秋饼32 分钟前
LLM API密钥动态轮换实战:Spring Vault + Spring AI + 多Provider路由
java·ai·技术分享·后端开发
gCode Teacher 格码致知42 分钟前
Pandas教学-17:Series与Dataframe的关系
开发语言·python·pandas
发量惊人的中年网工1 小时前
分公司访问总部ERP慢怎么办?SD-WAN组网的排查与验收要点
服务器·开发语言·php
下页、再停留1 小时前
【C#桌面客户端系列学习-15】生成可视化图表之折线图、柱状图、饼状图
开发语言·c#
ShineWinsu1 小时前
对于JavaScript:操作元素的解析
开发语言·前端·javascript·面试·web·dom·元素
马良神笔2 小时前
2026 年Rag最近技术前沿总结
后端·python·django
程序猿DD2 小时前
同样10美元,OpenDesign和Command Code的DeepSeek V4.1 Flash额度到底有多少?
后端
Tangyuewei2 小时前
小米的 Coding Agent 开源了
人工智能·开源·ai编程
福兮说2 小时前
JSON 格式正确,数据就可靠吗?我用一份自编配置检查类型与空值
java·前端·json