• 艾体宝案例 | 使用Redis和Spring Ai构建rag应用程序


    随着AI技术的不断进步,开发者面临着如何有效利用现有工具和技术来加速开发过程的挑战。Redis与Spring AI的结合为Java开发者提供了一个强大的平台,以便快速构建并部署响应式AI应用。探索这一整合如何通过简化的开发流程,让开发者能够更专注于创新而非底层实现。

    一、Spring AI简介

    由大型语言模型(LLM)驱动的应用程序中,向量数据库常作为人工智能应用程序的核心存储技术。此类数据库需要支持语义搜索,并为LLM提供相关的上下文环境。

    在此之前,通过Spring和Redis来构建人工智能应用程序的选项还相对有限。而最近,Redis作为一种高性能的向量数据库方案,现已引起广泛关注。Spring社区推出了一个名为Spring AI的新项目,旨在简化人工智能应用程序特别是那些涉及向量数据库的应用的开发流程。

    下面将介绍如何使用Redis作为向量数据库构建一个Spring AI应用程序,实现检索增强生成(RAG)工作流。

    二、检索增强生成

    检索增强生成(RAG)是一种结合数据与人工智能模型的技术方法。在RAG工作流中,首先需要将数据加载入向量数据库(例如Redis)。接收到用户查询后,向量数据库会检索出一组与查询相似的文档。这些文档将作为解答用户问题的上下文,并结合用户的查询,通常通过人工智能模型来生成响应。

    本例中,我们将利用一个包含各类啤酒信息的数据集进行演示,数据集中包含啤酒的名称、酒精含量(ABV)、国际苦味单位(IBU)和描述等属性。该数据集将被加载到Redis中,以展示RAG工作流的实际应用。

    三、代码和依赖关系

    可以在GitHub上找到Spring AI和Redis演示的全部代码。

    本项目使用了Spring Boot作为Web应用程序的启动依赖项,并结合了Azure OpenAI和Spring AI Redis。

    四、数据加载

    我们的应用程序将采用提供啤酒信息的JSON文档作为数据来源。每个文档的结构如下:

    1. {
    2.   "id""00gkb9",
    3.   "name""Smoked Porter Ale",
    4.   "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",
    5.   "abv"8,
    6.   "ibu"36
    7. }

    为了将啤酒数据集加载到 Redis 中,我们将使用 RagDataLoader 类。该类包含一个方法,在应用程序启动时执行。在该方法中,我们使用一个 JsonReader 来解析数据集,然后使用自动连接的 VectorStore 将文档插入 Redis。

    1. // Create a JSON reader with fields relevant to our use case
    2. JsonReader loader = new JsonReader(file, "name""abv""ibu""description");
    3. // Use the autowired VectorStore to insert the documents into Redis
    4. vectorStore.add(loader.get());

    至此,我们得到了一个包含约 22,000 种啤酒及其相应嵌入的数据集。

    五、RAG Service

    RagService 类实现了 RAG 工作流程。当收到用户提示时,会调用 retrieve 方法,执行以下步骤:

    1. 计算用户提示的向量
    2. 查询 Redis 数据库,检索最相关的文档
    3. 使用检索到的文档和用户提示构建一个提示信息
    4. 使用提示调用聊天客户端以生成响应
    1. public Generation retrieve(String message) {
    2.     SearchRequest request = SearchRequest.query(message).withTopK(topK);
    3.     // Query Redis for the top K documents most relevant to the input message
    4.     List docs = store.similaritySearch(request);
    5.     Message systemMessage = getSystemMessage(docs);
    6.     UserMessage userMessage = new UserMessage(message);
    7.     // Assemble the complete prompt using a template
    8.     Prompt prompt = new Prompt(List.of(systemMessage, userMessage));
    9.     // Call the autowired chat client with the prompt
    10.     ChatResponse response = client.call(prompt);
    11.     return response.getResult();
    12. }

    六、Controller

    现在我们已经实现了 RAG 服务,可以将其封装在 HTTP 端点中。

    RagController 类将服务作为 POST 端点公开:

    1. @PostMapping("/chat/{chatId}")
    2. @ResponseBody
    3. public Message chatMessage(@PathVariable("chatId") String chatId, @RequestBody Prompt prompt) {
    4.     // Extract the user prompt from the body and pass it to the autowired RagService
    5.     Generation generation = ragService.retrieve(prompt.getPrompt());
    6.     // Reply with the generated message
    7.     return Message.of(generation.getOutput().getContent());
    8. }

    七、用户界面 

    在用户界面方面,创建一个简单的 React 前端,允许用户提出有关啤酒的问题。前端通过向 /chat/{chatId} 端点发送 HTTP 请求并显示响应来与 Spring 后端交互。

    仅通过简单的几个类,我们就用 Spring AI 和 Redis 实现了一个 RAG 应用程序。

    若要更进一步,我们建议您查看 Github 上的示例代码。将 Redis 的高效和易用性与 Spring AI 提供的便捷抽象相结合,Java 开发人员使用 Spring 构建响应式 AI 应用程序将变得更加容易。

    有关向量数据库的更多信息,欢迎与我们沟通交流~

  • 相关阅读:
    【Linux operation 40】Linux中的Localhost
    智能井盖的工作原理是什么?万宾科技智能井盖传感器效果
    Unity URP14.0 自定义后处理框架
    如何为预先数据库创建自定义SQL Server复制
    二叉树概述
    好心情精神心理科医生:轻度抑郁症需要治疗吗?
    设计模式篇---组合模式
    【Linux】进程数据结构
    六、数据仓库详细介绍(ETL)方法篇
    Java游戏修炼手册:2023 最新学习线路图
  • 原文地址:https://blog.csdn.net/HongkeTraining/article/details/138165421