使用 Spring Cloud Alibaba AI 构建 RAG 应用

作者:姬世文

背景介绍

RAG(Retrieval Augmented Generation)

检索增强生成(RAG)是一种用于将数据与人工智能模型集成的技术。在 RAG 工作流程中,第一步将文档数据加载到矢量数据库(例如 Redis)中。当收到用户查询时,矢量数据库会检索一组与该查询相似的文档。然后,这些文档数据充当用户问题的上下文,并与用户的查询结合使用生成响应(通常通过 LLM 模型)。

在此示例中,我们将使用包含啤酒信息的数据集,包括名称、酒精体积(ABV)、国际苦度单位(IBU)等属性以及每种啤酒的描述。该数据集将加载到 Redis 中,之后通过 Spring Cloud Alibaba AI Starter 构建 Spring 项目,以演示 RAG 应用的工作流程。

Redis 矢量数据库

矢量数据库经常充当人工智能应用程序的内存。对于那些由大型语言模型(LLM)支持的人来说尤其如此。矢量数据库允许语义搜索,这为 LLM 提供了相关上下文。Spring AI 项目旨在简化人工智能驱动的应用程序的开发,包括矢量数据库的应用。

代码和依赖关系

您可以在 SCA 的官方博客中找到此示例源码链接:sca.aliyun.com

此示例 example 使用 Spring Cloud Alibaba AI 和 Spring AI Redis,用 Sping Web 构建 Web 应用程序。

数据加载

RAG 应用使用的数据由 JSON 文档组成,文档内容如下:

json 复制代码
{
  "id": "00gkb9",
  "name": "Smoked Porter Ale",
  "description": "The Porter Pounder Smoked Porter is a dark rich flavored ale that is made with 5 malts that include smoked and chocolate roasted malts. It has coffee and mocha notes that create a long finish that ends clean with the use of just a bit of dry hopping",
  "abv": 8,
  "ibu": 36
}

在此示例中,我们通过 RagDataLoader 类将数据插入到 Redis 中。

RAG 应用

RAGService 类在收到用户提示时,将会调用检索方法,执行以下步骤:

  1. 计算用户提示的向量
  2. 查询 Redis 数据库以检索最相关的文档
  3. 使用检索到的文档和用户提示构建提示
  4. 调用 ChatClient 并提示生成响应

调用示例

我们可以通过浏览器或者 curl 命令的方式调用 web api 接口,来获得 RAG 应用的输出。默认的 prompt 参数:What ber pairs well with smoked meats?

erlang 复制代码
curl $ curl  http://127.0.0.1:8081/rag/chat

# 如果一切正常,您将看到如下响应:
Bieré De Ménage would pair well with smoked meats due to its high ABV (8%) and potentially the influence of oak barrels from the winemaking process, which can complement the rich fla
vors of smoked dishes. However, if you prefer a sturdier stout, Son of Berserker Stout with its 6.9% ABV and 20 IBUs could also serve as a good match for smoked foods, especially since it's a substantial stout without additional complexities like bourbon or oak.

如果您使用浏览器调用,将看到以下内容:

在此示例中,将 Spring Cloud Alibaba AI 与 Redis 向量存储,仅通过几个类实现了 RAG 应用。欢迎您试用 Spring Cloud Alibaba AI Starter,如果有任何问题,您可以通过 Issue 的方式与我们联系。

也欢迎通过钉钉搜索群号加入社区钉群。(群号:64485010179)

相关推荐
DolitD10 小时前
云流技术深度剖析:国内云渲染主流技术与开源和海外厂商技术实测对比
功能测试·云原生·开源·云计算·实时云渲染
ghostwritten11 小时前
春节前夕,运维的「年关」:用 Kubeowler 给集群做一次「年终体检」
运维·云原生·kubernetes
liux35281 天前
基于kubeadm部署Kubernetes 1.26.4 集群指南
云原生·容器·kubernetes
Zfox_1 天前
CANN Catlass 算子模板库深度解析:高性能 GEMM 融合计算、Cube Unit Tiling 机制与编程范式实践
docker·云原生·容器·eureka
农民工老王1 天前
K8s 1.31 私有化部署实战:从 Calico 崩溃到 NFS 挂载失败的排坑全记录
云原生·kubernetes
灰子学技术1 天前
istio从0到1:如何解决分布式配置同步问题
分布式·云原生·istio
小马爱打代码1 天前
ZooKeeper:入门实战
分布式·zookeeper·云原生
logocode_li1 天前
OCI/CRI 双标准下:从 dockerd 到 containerd 的 K8s 运行时迭代史
docker·云原生·容器·k8s
天才奇男子2 天前
HAProxy高级功能全解析
linux·运维·服务器·微服务·云原生
人间打气筒(Ada)2 天前
k8s:CNI网络插件flannel与calico
linux·云原生·容器·kubernetes·云计算·k8s