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

LangChain4j第三篇: RAG的简单应用与实践

引言:RAG 构建属于你的大模型

大语言模型(LLM)的知识体系本质上仅限于它所接受的训练数据。
其一在知识时效性方面,模型参数固化于训练完成的时点,而现实世界中的知识和信息持续动态更新。
其二在非公开数据层面,企业内部的机密文档(如产品设计图、商业策略等)及个人隐私数据均未被纳入训练范围;
其三,领域专业性维度,特定垂直领域的前沿进展或专业壁垒较高的知识可能未被充分覆盖。基于以上方面通过RAG技术连接我们外部的知识库,来补充增强数据的准确性。

一、 RAG 原理简述

简单来说是在将数据发送给LLM之前从数据中查找相关信息片段并将其注入提示符的方法。这样,LLM将获得相关信息,并能够使用这些信息进行回复。
RAG过程分为两个不同的阶段:索引和检索。LangChain4j为这两个阶段提供了工具。

1.1Indexing

这个过程可以根据具体所使用的信息检索方法而变化。对于向量搜索,我们需要对文本切分成小的块,并将块进行向量化,最后将它们存储在量数据库中。
下面是索引阶段的简化图:
在这里插入图片描述

1.2 Retrieval

对于向量搜索,先将用户的问题在向量数据库中进行相似度检索。然后将相关内容(原始文档的片段)注入提示并发送给LLM。
这是检索阶段的简化示意图:
在这里插入图片描述

二、 应用实践

2.1 maven 引入相关pom 依赖

  <dependency><groupId>dev.langchain4j</groupId><artifactId>langchain4j-ollama-spring-boot-starter</artifactId><version>1.0.0-beta3</version></dependency><dependency><groupId>dev.langchain4j</groupId><artifactId>langchain4j-spring-boot-starter</artifactId><version>1.0.0-beta3</version></dependency><dependency><groupId>dev.langchain4j</groupId><artifactId>langchain4j-embeddings-all-minilm-l6-v2</artifactId><version>1.0.0-beta3</version></dependency>

在application.properties 中添加配置

langchain4j.ollama.chat-model.base-url=http://127.0.0.1:11868  #ollma 访问地址
langchain4j.ollama.chat-model.model-name=deepseek-r1:14b  # 填写本地运行的模型
langchain4j.ollama.chat-model.log-requests=true
langchain4j.ollama.chat-model.log-responses=true

2.2 代码示例

以下是官方提供的示例代码,示例切分文档 miles-of-smiles-terms-of-use.txt

Miles of Smiles Car Rental Services Terms of Use1. Introduction
These Terms of Service (“Terms”) govern the access or use by you, an individual, from within any country in the world, of applications, websites, content, products, and services (“Services”) made available by Miles of Smiles Car Rental Services, a company registered in the United States of America.2. The Services
Miles of Smiles rents out vehicles to the end user. We reserve the right to temporarily or permanently discontinue the Services at any time and are not liable for any modification, suspension or discontinuation of the Services.3. Bookings
3.1 Users may make a booking through our website or mobile application.
3.2 You must provide accurate, current and complete information during the reservation process. You are responsible for all charges incurred under your account.
3.3 All bookings are subject to vehicle availability.4. Cancellation Policy
4.1 Reservations can be cancelled up to 7 days prior to the start of the booking period.
4.2 If the booking period is less than 3 days, cancellations are not permitted.5. Use of Vehicle
5.1 All cars rented from Miles of Smiles must not be used:
for any illegal purpose or in connection with any criminal offense.
for teaching someone to drive.
in any race, rally or contest.
while under the influence of alcohol or drugs.6. Liability
6.1 Users will be held liable for any damage, loss, or theft that occurs during the rental period.
6.2 We do not accept liability for any indirect or consequential loss, damage, or expense including but not limited to loss of profits.7. Governing Law
These terms will be governed by and construed in accordance with the laws of the United States of America, and any disputes relating to these terms will be subject to the exclusive jurisdiction of the courts of United States.8. Changes to These Terms
We may revise these terms of use at any time by amending this page. You are expected to
http://www.lryc.cn/news/2385154.html

相关文章:

  • 机器学习第二十六讲:官方示例 → 跟着菜谱学做经典菜肴
  • 功能强大且易于使用的 JavaScript 音频库howler.js 和AI里如何同时文字跟音频构思想法
  • 品鉴JS的魅力之防抖与节流【JS】
  • 如何使用patch-package给npm包打补丁
  • maxkey单点登录系统
  • windows bat 在目录下(包括子目录)搜索批量指定文件名称复制到另一个文件夹内
  • Notepad++ 下载与安装教程(小白专属)
  • Spring Cloud Gateway 微服务网关实战指南
  • 微服务架构实战:Eureka服务注册发现与Ribbon负载均衡详解
  • 采用多维计算策略(分子动力学模拟+机器学习),显著提升 α-半乳糖苷酶热稳定性
  • 【java】小练习--零钱通
  • 旅游信息检索
  • 贝叶斯理论
  • Docker-mongodb
  • Gartner《Optimize GenAI Strategy for 4 Key ConsumerMindsets》学习心得
  • [ARM][汇编] 02.ARM 汇编常用简单指令
  • 达梦数据库-学习-22-库级物理备份恢复(超详细版)
  • python网络爬虫的基本使用
  • AI Agent开发第74课-解构AI伪需求的魔幻现实主义
  • 【卫星通信】通信卫星链路预算计算及其在3GPP NTN中的应用
  • HTTP请求方法:GET与POST的使用场景解析
  • 第十五章:数据治理之数据目录:摸清家底,建立三大数据目录
  • c++命名空间的作用及命名改编
  • Go核心特性与并发编程
  • echarts实现项目进度甘特图
  • Flutter 中 build 方法为何写在 StatefulWidget 的 State 类中
  • C#串口打印机:控制类开发与实战
  • 2025深圳国际无人机展深度解析:看点、厂商与创新亮点
  • Electron 后台常驻服务实现(托盘 + 开机自启)
  • Spring Boot与Kafka集成实践:从入门到实战