当前位置: 首页 > news >正文

SpringBoot 集成 LangChain4j RAG Redis

SpringBoot 集成 LangChain4j RAG Redis

  • 1 依赖
  • 2 配置
  • 3 代码
    • 3.1 RagConf
    • 3.2 RagController
  • 4 结果

1 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.5.6</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.xu</groupId><artifactId>lang-chain</artifactId><version>1.0.0</version><name>lang-chain</name><description>Demo project for Spring Boot</description><properties><java.version>25</java.version></properties><dependencyManagement><dependencies><dependency><groupId>dev.langchain4j</groupId><artifactId>langchain4j-bom</artifactId><version>1.7.0</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>dev.langchain4j</groupId><artifactId>langchain4j-community-bom</artifactId><version>1.7.1-beta14</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><!--Spring Boot 的 Web starter--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--LangChain4j 的 Spring Boot starter--><dependency><groupId>dev.langchain4j</groupId><artifactId>langchain4j-spring-boot-starter</artifactId></dependency><!--LangChain4j 针对 OpenAI 的 Spring Boot starter--><dependency><groupId>dev.langchain4j</groupId><artifactId>langchain4j-open-ai-spring-boot-starter</artifactId></dependency><!--LangChain4j 对 Ollama 的支持库-->
<!--        <dependency>-->
<!--            <groupId>dev.langchain4j</groupId>-->
<!--            <artifactId>langchain4j-ollama</artifactId>-->
<!--        </dependency>--><!--LangChain4j RAG 的最简单方法--><dependency><groupId>dev.langchain4j</groupId><artifactId>langchain4j-easy-rag</artifactId></dependency><!--LangChain4j 对 redis 向量扩展(vector)的支持库--><dependency><groupId>dev.langchain4j</groupId><artifactId>langchain4j-community-redis-spring-boot-starter</artifactId></dependency><!--Spring Boot 的开发工具,提供热部署、自动重启等功能,加速开发过程--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><!--简化 Java 代码的工具库--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!--Spring Boot 的测试 starter--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><annotationProcessorPaths><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></path></annotationProcessorPaths></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>

2 配置

server:port: 8080servlet:context-path: /spring:application:name: langchain4jlogging:file:name: logs/spring-oracle.loglevel:root: INFOcom.xu: INFOlangchain4j:community:redis:host: localhostport: 6379user: langchain4jpassword: 123456indexName: langchain4jprefix: ":lc4j:"dimension: 1536metadataKeys:- username- age- source- category

3 代码

3.1 RagConf

package com.xu.conf;import dev.langchain4j.community.store.embedding.redis.RedisEmbeddingStore;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.store.embedding.EmbeddingStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class RagConf {@Beanpublic EmbeddingStore<TextSegment> embeddingStore() {return RedisEmbeddingStore.builder().host("127.0.0.1").port(6379).dimension(1536).build();}}

3.2 RagController

package com.xu.controller;import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
@AllArgsConstructor
@RequestMapping("/ollama/rag")
public class RagController {private final EmbeddingStore<TextSegment> embeddingStore;@GetMapping("/save")public Object save() {List<Document> documents = FileSystemDocumentLoader.loadDocuments("D:\\SourceCode\\简历");if (CollectionUtils.isEmpty(documents)) {return 0;}EmbeddingStoreIngestor.ingest(documents, embeddingStore);return documents.size();}}

4 结果

在这里插入图片描述

http://www.dtcms.com/a/470503.html

相关文章:

  • 【QT】customPlot 设置图例透明背景和文字颜色
  • AT指令解析:ring_buffer、信号量、互斥量等基础知识
  • 任务网站(做任务学技能的)潍坊市建设局网站
  • 【算法】——动态规划之路径问题
  • 阿里云企业建站教程泰州东方医院
  • 东阿县住房和城乡建设局网站申请做版主 再什么网站
  • 深入理解数据在内存中的存储:整数与浮点数的二进制表示
  • 广东网站营销seo费用品牌推广活动有哪些
  • 特效音网站建设公司官网制作平台
  • MySQL数据库安装后,如何设置自动化备份策略?
  • 【开题答辩全过程】以 保险业务信息管理系统为例,包含答辩的问题和答案
  • 进口食品销售销售在那个网站做seo托管
  • 公司模板网站建设成绩查询系统网站开发
  • 建设大淘客网站雅布设计师
  • Oracle ADRCI工具全面使用指南:从基础到故障诊断实战
  • 美食网站设计欣赏上海著名网站建设
  • 【智能系统项目开发与学习记录】bringup功能包详解
  • 外贸网建站建公司网站的详细步骤
  • 美食网站建设书成都seo技术
  • 江河建设集团有限公司网站梧州网站建设流程
  • 在Qt中使用VTK
  • 正安北郊湖吉他文化广场自动化监测
  • 【论文阅读】DSPy-based neural-symbolic pipeline to enhance spatial reasoning in LLMs
  • cn域名后缀网站163企业邮箱格式
  • psql常用命令
  • 高速公路自动车道保持系统原理与实现
  • 番禺做网站最便宜的哪家公司wordpress注册界面
  • 【推荐100个unity插件】将您的场景渲染为美丽的冬季风景——Global Snow 2
  • Windows安装Elasticsearch保姆级教程
  • 温州网站链接怎么做在山东省建设监理协会网站