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

SpringBoot 集成 LangChain4j RAG PostgreSQL

SpringBoot 集成 LangChain4j RAG PostgreSQL

  • 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/467313.html

相关文章:

  • 寻找郑州网站优化公司上海自助建站
  • 瓜果蔬菜做的好的电商网站wordpress 自定义路由
  • EOM公式推导
  • 网站建设qinnet个股期权网站开发
  • 网站如何做好优化wordpress下载慢
  • 通信工程网站建设在线一键扒站源码php
  • 免费推广网站哪家好有哪些付费wordpress
  • 管理约束 · 开篇概述(一)
  • 电子商务网站建设培训小结北京模板开发建站
  • 源码之家网站国家企业信用公示信息系统入口
  • 华为OD机试C卷 - 对称美学 - 递归 - (Java C++ JavaScript Python)
  • 手怎么搭建网站泉州网站制作推广
  • 填空 前三章
  • 乐清网站制作优化邓亚萍20亿做网站
  • 进程的控制
  • 电脑网站微信支付怎么做的企业建设网站公司有哪些
  • 欧美做电影 迅雷下载网站网站工商备案查询
  • 深圳办公室装修哪家好正规的网站优化推广公司
  • 网站硬件方案全椒有做网站的吗
  • 提供做网站公司移动开发软件
  • 南阳卧龙区网站建设哪家好wordpress页面权限设置
  • 做网站win7好用么广州建网站比较有名的公司
  • 我的全栈学习之旅:FastAPI (持续更新!!!)
  • 做电影网站要怎么拿到版权资源下载网站建设
  • 佛山找企业的网站苏州实力做网站公司有哪些
  • 网站技术策划人员要求怎么搞到网站
  • 在 tinkinter 中 label 标签的 font 属性有哪几种形式设置?
  • wordpress友情链接激活seo关键词
  • 晋城网站制作公司怎么选wordpress 最新一片文章
  • 淄博市沂源县城乡建设局网站wordpress 删除 wordpress.org