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

SpringBoot 集成 LangChain4j RAG

SpringBoot 集成 LangChain4j RAG

  • 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.6.0</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 对 PostgreSQL 向量扩展(pgvector)的支持库--><dependency><groupId>dev.langchain4j</groupId><artifactId>langchain4j-pgvector</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: 8080spring:application:name: lang-chainlogging:file:name: logs/spring-oracle.loglevel:root: INFOcom.xu: INFOlangchain4j:ollama:chat-model:base-url: http://127.0.0.1:11434model-name: gemma3:latest

3 代码

3.1 RagConf

package com.xu.conf;import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.pgvector.PgVectorEmbeddingStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class RagConf {@Beanpublic EmbeddingStore<TextSegment> embeddingStore() {return PgVectorEmbeddingStore.builder().dropTableFirst(true).createTable(true).table("embedding").user("postgres").host("localhost").password("123456").database("postgres").port(5432).dimension(384).build();}}

3.2 RagController

package com.xu.chat.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("/test")public Object chat() {List<Document> documents = FileSystemDocumentLoader.loadDocuments("E:\\Test");if (CollectionUtils.isEmpty(documents)) {return 0;}EmbeddingStoreIngestor.ingest(documents, embeddingStore);return documents.size();}}

4 结果

在这里插入图片描述
在这里插入图片描述

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

相关文章:

  • linear protocol(线性协议)
  • net网络网站建设wordpress修改语言包目录
  • 购物网站建设的意义与目的镇江市建设工程安全监督站网站
  • UiKit中使用DiffableDataSource进行数据差异化更新
  • Elegant Query
  • 中小企业网站建设信息小企业想做网站推广找哪家强
  • 4A 架构之业务架构、数据架构、应用架构和技术架构
  • FOC电机控制原理
  • linux网络服务+linux数据库7
  • 阿里去可以做几个网站一线设计公司
  • 怎么面试一个网站开发的人wordpress 标签seo插件
  • 跨服务器快速传输数据
  • 一人AI自动化开发体系(Cursor 驱动):从需求到上线的全流程闭环与实战清单
  • Google 智能体设计模式:记忆管理
  • 大兴企业官网网站建设报价网站推荐界面
  • 精准核酸检测
  • 摩尔信使MThings入门教程3
  • 电子商务与网站建设报告福建建设工程信息网
  • 银座商城官网镇江seo网站优化
  • 网站建设好公司好做cps的网络文学网站
  • 廊坊做网站公司排名四川省住房和建设厅官方网站
  • 17.链路聚合——LACP模式(2025年10月10日)
  • PG数据文件位置迁移
  • 优化百度网站企业vi设计策划书
  • 建设一个网站平台需要哪些技术员网站到期时间查询
  • 网站建设在哪块做wordpress主题激活
  • 网站开发建设中上海网站排名优化优化
  • 做外卖在哪个网站做好兰州专业网站建设团队
  • 雄安做网站要多少钱网页建站专业公司
  • 微分中值定理(费马、罗尔、拉格朗日、柯西)