Powered by Blogger.

How to create an AI and RAG chatbot using Spring AI and a Web HTML page.

2 comments :
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
  1.  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).
  2.  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.
  3.  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.
Key RAG Components in Spring AI:
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


Step 2.4:
We implement RAG by taking raw documents (PDF, TXT, HTML) processed into `data.text` format and breaking them down into smaller text chunks.

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.

Step 3:
Finally, we create the web page (`indexAIRAGCHAT.html`) and integrate the REST controller with the web UI.
 
Output: