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

[7]. SpringAI Alibaba RAG增强检索生成

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

RAG

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

RAG过程

RAG的索引建立

在这里插入图片描述

RAG的检索

在这里插入图片描述

编码

向量化

package com.atguigu.study.config;import cn.hutool.crypto.SecureUtil;
import jakarta.annotation.PostConstruct;
import org.springframework.ai.document.Document;
import org.springframework.ai.reader.TextReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.AbstractVectorStoreBuilder;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.data.redis.core.RedisTemplate;import java.nio.charset.Charset;
import java.util.List;@Configuration
public class InitVectorDatabaseConfig {@Autowiredprivate VectorStore                   vectorStore;@Autowiredprivate RedisTemplate<String, String> redisTemplate;@Value("classpath:ops.txt")private Resource opsFile;@PostConstructpublic void init() {//1 读取文件TextReader textReader = new TextReader(opsFile);textReader.setCharset(Charset.defaultCharset());//2 文件转换为向量(开启分词)List<Document> list = new TokenTextSplitter().transform(textReader.read());//3 写入向量数据库RedisStack//vectorStore.add(list);// 解决上面第3步,向量数据重复问题,使用redis setnx命令处理//4 去重复版本String sourceMetadata = (String) textReader.getCustomMetadata().get("source");String textHash = SecureUtil.md5(sourceMetadata);String redisKey = "vector-xxx:" + textHash;// 判断是否存入过,redisKey如果可以成功插入表示以前没有过,可以假如向量数据Boolean retFlag = redisTemplate.opsForValue().setIfAbsent(redisKey, "1");System.out.println("****retFlag : " + retFlag);if (Boolean.TRUE.equals(retFlag)) {//键不存在,首次插入,可以保存进向量数据库vectorStore.add(list);} else {//键已存在,跳过或者报错//throw new RuntimeException("---重复操作");System.out.println("------向量初始化数据已经加载过,请不要重复操作");}}}

检索

package com.atguigu.study.controller;import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.rag.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;/*** @auther zzyybs@126.com* @create 2025-07-30 12:21* @Description 知识出处:* https://docs.spring.io/spring-ai/reference/api/retrieval-augmented-generation.html#_advanced_rag*/
@RestController
public class RagController {@Resource(name = "qwenChatClient")private ChatClient  chatClient;@Resourceprivate VectorStore vectorStore;/*** http://localhost:8012/rag4aiops?msg=00000* http://localhost:8012/rag4aiops?msg=C2222** @param msg* @return*/@GetMapping("/rag4aiops")public Flux<String> rag(String msg) {String systemInfo = """你是一个运维工程师,按照给出的编码给出对应故障解释,否则回复找不到信息。""";RetrievalAugmentationAdvisor advisor = RetrievalAugmentationAdvisor.builder().documentRetriever(VectorStoreDocumentRetriever.builder().vectorStore(vectorStore).build()).build();return chatClient.prompt().system(systemInfo).user(msg).advisors(advisor).stream().content();}
}
http://www.dtcms.com/a/599058.html

相关文章:

  • 国内做的比较好的旅游网站免费的网站加速器
  • 知识管理工具:confluence vs 语雀 vs notion vs sward一文纵评
  • 免费网站素材下载wordpress 导入用户
  • php mysql网站开发项目式教程免费的网站制作
  • 网站建设 客户同程网页升级紧急跳转
  • 在做网站的公司做网站运营犯法吗成都网站怎么推广
  • DeepSeekV3-MOE
  • 软文网站推广wordpress 添加视频
  • 杭州网站设计公司哪个好百度关键词怎么优化
  • 网站上怎样做轮播图珠宝钻石网站建站
  • 数据结构入门 (十):“左小右大”的秩序 —— 深入二叉搜索树
  • 能不能模仿百度一样做搜索引擎网站php网站开发职责
  • 如果做局域网影音网站企业服务中心抖音
  • 常见购物网站功能丹东建设银行网站
  • 徐州建网站网站界面设计形考
  • 最新电大网站开发维护WORDPRESS摘要无效
  • 高端定制网站开发买空间哪个网站好
  • Linux 内存管理 (5):buddy 内存分配简要流程
  • C++ 高精度计算:突破数据类型限制的实现与应用
  • 学做招投标的网站上传空间站的注意事项
  • 黑马JAVAWeb -Vue工程化 - Element Plus- 表格-分页条-中文语言包-对话框-Form表单
  • 甘州区建设局网站wordpress谷歌广告
  • 纪检网站建设动态主题国内建站平台
  • 网站页面的大小写国内seo服务商
  • 如何在关键里程碑已延迟的情况下重新规划项目进度
  • 排版好看的网站界面湖北企业响应式网站建设价位
  • 光伏电站运维-可视化大屏带来的便利
  • 张家港保税区建设规划局网站商标注册查询官网入口官方
  • MySQL 四种隔离级别:从脏读到幻读的全过程
  • 人才网网站建设方案河北建设工程信息网登陆