使用 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 文档组成,文档内容如下:

{
  "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?

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)

Windows 11 将在中国区设备上安装「微软电脑管家」 这是Linus最忍不了的一集——虚幻引擎代码规范禁止使用脏话、禁止slave、master RustRover 正式发布,个人非商业免费 新款 iPad Pro 使用了 12GB 内存颗粒,但却声称是 8GB 内存 Kotlin 2.0.0 稳定版发布,K2 编译器已稳定 macOS 终端工具 iTerm2 发布重大更新 3.5.0:集成 ChatGPT、让 AI 帮你写命令 Ubuntu 24.04 LTS 正式“开放升级” 微软必应昨天全球宕机,波及 ChatGPT、Copilot、DuckDuckGo 等服务 开源日报 | Kotlin 2.0.0;简体中文压缩数据只有6TB;AI不是核弹;全球首个开源大规模"NoC IP";华为有哪些能力是当前不可代替的? PostgreSQL 17 Beta 1
{{o.name}}
{{m.name}}

猜你喜欢

转载自my.oschina.net/u/3874284/blog/11193170