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

    }
}
相关推荐
Brilliantwxx4 分钟前
【C++】认识标准库STL(2)
开发语言·c++
陈蒙_5 分钟前
三板斧解决 Trae 卡顿
安卓·agent·ai编程·trae·trae 卡顿
半天法师5 分钟前
Bug 记录:UE 结构体转 JSON 时 Key 字段大小写异常 (Editor 与打包后表现不一致)
ai·ue5·json·bug
故事还在继续吗9 分钟前
STL 容器算法手册
开发语言·c++·算法
weisian15110 分钟前
Java并发编程--48-美团Leaf与百度UidGenerator:分布式ID生成器的工业级实践
java·leaf号段模式·leaf雪花模式·uidgenerator
胖纳特11 分钟前
Nextcloud 文件预览困局与破局:集成 BaseMetas Fileview 实现全格式在线预览
前端·后端
Elastic 中国社区官方博客12 分钟前
通过受管控的控制平面加速商品陈列优化
大数据·数据库·人工智能·elasticsearch·搜索引擎·平面·ai
lczllx12 分钟前
MIT 6.824-lab3A(实现思路)
后端
一个心烑12 分钟前
Layui结合springboot读取返回值,前端展示简单示例
前端·spring boot·layui