How to create an AI and RAG chatbot using Spring AI and a Web HTML page.
Retrieval-Augmented Generation (RAG)
First, you need to select an LLM (Large Language Model) that Spring AI will use to communicate: Instead of relying solely on an LLM's pre-trained knowledge, RAG fetches relevant context from your own data sources (like databases, PDFs, or internal documentation) at runtime and passes that information along with the user prompt to the LLM.
Step 1: Core Concepts of RAG
Spring AI simplifies RAG integration through familiar Spring abstractions: DocumentReader & TextSplitter: Interfaces to ingest documents (e.g., using Apache Tika) and break them into processable chunks.
VectorStore: A unified Java abstraction for vector databases (supports pgvector, Redis, Pinecone, Qdrant, Milvus, ChromaDB, etc.).
Advisor API (QuestionAnswerAdvisor): A high-level abstraction that automatically intercepts ChatClient prompts, queries the VectorStore, appends retrieved documents to the prompt context, and passes it to the LLM.
RetrievalAugmentationAdvisor: A modular API for advanced RAG patterns, enabling features like multi-query expansion, document re-ranking, and dynamic metadata filtering.
Step 2: Implementing Basic RAG with Spring AI
In this step, we configure the Spring Boot AI application using Spring Initializr (https://start.spring.io/); we have previously covered how to perform this configuration. Here, we will explain the coding process step-by-step.
First, you need to select an LLM (Large Language Model) that Spring AI will use to communicate: Instead of relying solely on an LLM's pre-trained knowledge, RAG fetches relevant context from your own data sources (like databases, PDFs, or internal documentation) at runtime and passes that information along with the user prompt to the LLM.
Step 1: Core Concepts of RAG
- Ingestion & Embedding (Offline / Pre-processing): Raw documents (PDF, TXT, HTML) are read and split into smaller text chunks. An Embedding Model converts these text chunks into numerical vectors. The vectors and text chunks are stored in a Vector Store (e.g., PgVector, ChromaDB, Pinecone, Redis).
- Retrieval (Runtime): When a user submits a query, Spring AI converts the query into a vector and searches the Vector Store for the most semantically relevant document chunks.
- Augmentation & Generation: Spring AI automatically combines the user query and the retrieved documents into an augmented prompt. The augmented prompt is sent to the LLM, which uses the provided context to generate an accurate, hallucination-free answer.
Spring AI simplifies RAG integration through familiar Spring abstractions: DocumentReader & TextSplitter: Interfaces to ingest documents (e.g., using Apache Tika) and break them into processable chunks.
VectorStore: A unified Java abstraction for vector databases (supports pgvector, Redis, Pinecone, Qdrant, Milvus, ChromaDB, etc.).
Advisor API (QuestionAnswerAdvisor): A high-level abstraction that automatically intercepts ChatClient prompts, queries the VectorStore, appends retrieved documents to the prompt context, and passes it to the LLM.
RetrievalAugmentationAdvisor: A modular API for advanced RAG patterns, enabling features like multi-query expansion, document re-ranking, and dynamic metadata filtering.
Step 2: Implementing Basic RAG with Spring AI
In this step, we configure the Spring Boot AI application using Spring Initializr (https://start.spring.io/); we have previously covered how to perform this configuration. Here, we will explain the coding process step-by-step.
Step 2.1: Check the dependencies in the `pom.xml` file and ensure they match your project setup.
Step 2.2: We create a REST controller named `RAGController.java`.
Here, we will see how to implement the LLM packages and classes, and we will also use `@CrossOrigin` to facilitate the design of the web UI.
Step 2.3: We will configure the LLM model settings within the `application.properties` file.
spring.application.name=aiChatclient
server.port=8082
# Use the local Ollama instance for both the LLM and embeddings.
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.options.model=codellama:latest
spring.ai.ollama.chat.options.num-predict=256
spring.ai.ollama.chat.options.temperature=0.2
spring.ai.ollama.embedding.options.model=mxbai-embed-large:latest
spring.ai.ollama.embedding.enabled=true
# Local knowledge base file used by the RAG controller.
file.path=classpath:data/data.text
Details:
Name: Manoj kumar Vishwakarms
address: 123 Main Street, City, Country
phone: +910000000000
email: manoj@example.com
job: Software Engineer
skills: Java, Spring Boot, SQL, JavaScript
//Something else can also be written in this RAG data file.
How to create an AI chatbot using Spring AI and a Web HTML page.
Spring AI is an open-source application framework designed to easily integrate Artificial Intelligence (AI) features into Spring Boot applications. It functions as a standard middleware layer; rather than training or building AI models from scratch, it enables developers to seamlessly connect their enterprise code with existing AI models.
Below, we outline the step-by-step process for configuring and developing a chatbot.
First, you need to select an LLM (Large Language Model) that Spring AI will use to communicate:
First, you need to select an LLM (Large Language Model) that Spring AI will use to communicate:
Step 1:We will use the standalone Ollama LLM model. To do this, install Ollama on your system (download via https://ollama.com/download/windows)
and then download a free model from https://ollama.com/search.
CMD:
Check Ollama Run using URL: http://localhost:11434/
Step 2:
In this step, we configure the Spring Boot AI application using Spring Initializr (https://start.spring.io/); we have previously covered how to perform this configuration. Here, we will explain the coding process step-by-step.
CMD:
1. widows CMD:
irm https://ollama.com/install.ps1 | iex
2. mocOS CMD:
curl -fsSL https://ollama.com/install.sh | sh
3. Linux CMD:
curl -fsSL https://ollama.com/install.sh | sh
Check Ollama Run using URL: http://localhost:11434/
Step 2:
In this step, we configure the Spring Boot AI application using Spring Initializr (https://start.spring.io/); we have previously covered how to perform this configuration. Here, we will explain the coding process step-by-step.
Step 2.1: Check the dependencies in the `pom.xml` file and ensure they match your project setup.
Step 2.2:
We create a REST controller named `ChatClientController.java`.
Here, we will see how to implement the LLM packages and classes, and we will also use `@CrossOrigin` to facilitate the design of the web UI.
Step 2.3: We will configure the LLM model settings within the `application.properties` file.
spring.application.name=aiChatclient
server.port=8081
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.options.model=codellama:latest
spring.ai.ollama.chat.options.num-predict=256
spring.ai.ollama.chat.options.temperature=0.2
Step-by-step development video:
Subscribe to:
Posts
(
Atom
)
