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

    }
}
相关推荐
weixin_449290014 小时前
Dify 三模式安全配置清单
ai
二哈赛车手4 小时前
新人笔记---ApiFox的一些常见使用出错
java·笔记·spring
counterxing4 小时前
Agent 跑起来之后,难的是复用、观测和评测
node.js·agent·ai编程
uccs4 小时前
大模型底层机制与Agent开发
agent·ai编程·claude
为何创造硅基生物5 小时前
C语言 结构体内存对齐规则(通俗易懂版)
c语言·开发语言
吃好睡好便好5 小时前
在Matlab中绘制横直方图
开发语言·学习·算法·matlab
栗子~~5 小时前
JAVA - 二层缓存设计(本地缓冲+redis缓冲+广播所有本地缓冲失效) demo
java·redis·缓存
星寂樱易李5 小时前
iperf3 + Python-- 网络带宽、网速、网络稳定性
开发语言·网络·python
YDS8295 小时前
DeepSeek RAG&MCP + Agent智能体项目 —— RAG知识库的搭建和接口实现
java·ai·springboot·agent·rag·deepseek
counterxing5 小时前
我把 Codex 里的 Skills 做成了一个 MCP,还支持分享
前端·agent·ai编程