×
Community Blog Build RAG Applications with Spring Cloud Alibaba AI

Build RAG Applications with Spring Cloud Alibaba AI

This article uses Spring Cloud Alibaba AI and Spring AI Redis to build a web application with Spring Web.

By Shiwen Ji

Background

RAG (Retrieval Augmented Generation)

Retrieval Augmented Generation (RAG) is a technique for integrating data with artificial intelligence models. In the RAG workflow, the first step is to load the document data into a vector database, such as Redis. When receiving a user query, the vector database retrieves a set of documents similar to the query. This document data then serves as the context for the user's question and is used in conjunction with the user's query to generate a response (typically through the LLM).

In this example, we will use a dataset that contains information about the beers, including attributes such as name, alcohol volume (ABV), and international bitterness unit (IBU), and a description of each beer. The dataset is loaded into Redis and builds a Spring project with Spring Cloud Alibaba AI Starter to demonstrate the workflow of the RAG application.

Redis Vector Database

Vector databases often act as memory for artificial intelligence applications. This is especially the case for those supported by the large language model (LLM). Vector databases allow semantic search, which provides relevant context for LLM. The Spring AI project aims to simplify the development of artificial intelligence-driven applications, including the application of vector databases.

Code and Dependencies

You can find the link to this sample source code in the official SCA blog: sca.aliyun.com

This example uses Spring Cloud Alibaba AI and Spring AI Redis and builds a Web application with Spring Web.

Data Loading

The data used by RAG applications consists of JSON documents. The content of the documents is as follows:

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

In this example, we insert data into Redis with the RagDataLoader class.

RAG Applications

When RAGService class is prompted by the user, it calls the retrieval method and performs the following steps:

  1. Calculate the vector that the user prompts.
  2. Query the Redis database to retrieve the most relevant documents.
  3. Build a prompt using retrieved documents and user prompts.
  4. Call ChatClient and prompt to generate a response.

Calling Sample

You can get the output of the RAG application by calling the Web API through the browser or the curl command. Default prompt parameter: What beer pairs well with smoked meats?

curl $ curl  http://127.0.0.1:8081/rag/chat

# If everything goes well, you will see the response as follows:
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 flavors 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.

If you use the browser to call, you will see the content as follows:

1

In this example, Spring Cloud Alibaba AI is stored with Redis vector, and the RAG application is implemented through only a few classes. Welcome to try Spring Cloud Alibaba AI Starter. If you have any questions, please contact us through Issue.

0 1 0
Share on

You may also like

Comments