Powered by Blogger.

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

1 comment :
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:
 
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:
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 3:
Finally, we create the web page (`indexAI_CHAT.html`) and integrate the REST controller with the web UI.
 
Output:


Step-by-step development video:


How to receive a JWT-based access token from the client, verify the token, and push data using the client's URL?

1 comment :
JWT OAuth2 Integration and JSON Payload Handling in Java: JWT (JSON Web Token) is a token format used in OAuth2.
A JWT is:
  • Stateless
  • Digitally signed
  • Self-contained
Integrating with client APIs using APIs and JSON-based payloads involves an end-to-end process that includes making API calls with JSON payloads and pushing master/other data to REST API endpoints.

Step 1: Obtain the authentication details/Requirements from the client as shown in the example below.

############_MK_TOKEN_CLIENT_SECRET_#################### 

TOKEN_URL=https://a143mk-uat.client.com/api/v1/client/authentication/generate_token CLIENT_ID=AA143Mkbolgs  
CLIENT_SECRET=testa143mk

 ####Example of Configure URL ############# 
CUST_URL=https://a143mk-uat.client.com/api/master/push_customer 
CUST_USER_PASS_AUTH=useradmin:password //if it is required or not


Step 2: Making an API call with an Json payload and pushing master data to the location.
  1. A REST API Controller is responsible for handling HTTP requests (such as GET, POST, PUT, DELETE) from clients and routing them to the appropriate service methods. It acts as the entry point for external clients to interact with the backend application. 
  2. Purpose: The main role of the controller is to manage incoming HTTP requests, map them to specific methods (often called endpoints), and return appropriate responses to the client. 
  3. Responsibilities: Handle HTTP requests (e.g., GET, POST, PUT, DELETE). Map the incoming requests to service methods. Return HTTP responses, often in JSON or Json format. Perform basic request validation or authentication. Act as the "interface" between the client and the service layer. In a Spring Boot application, for example, the controller is typically annotated with @RestController and the request mappings are handled using annotations like @GetMapping, @PostMapping, etc. 
Example "ClientApiController.java" of a REST Controller in Spring Boot with handlined Json Payload :
 
 
The Service class is a part of the business logic layer of your application. It contains methods that implement the core functionality of the application (e.g., creating, reading, updating, deleting data). The service class is called by the controller to perform operations that are not directly related to the web request itself. 
  1. Purpose: The service layer acts as a mediator between the controller and the data layer (such as a repository or database). It focuses on business logic and operations. 
  2. Responsibilities: Perform the core business logic of the application. Interact with the database (via repositories or DAOs). Provide methods for the controller to use (i.e., encapsulate business operations). Handle any necessary validation or transformation before sending data back to the controller. In a Spring Boot application, service classes are typically annotated with @Service, and they are injected into the controller using @Autowired (or constructor injection). 
Example "CustMastSystemNameService.java" of a Service Class in Spring Boot: 
 Method Internal Working ->geta143mkCustMasterFOrSYS() ->pusha143mkCustToSystemNameSystem().
 


Step 3: The authorization server issues an access token in JWT format The client sends this token in requests using Authorization: Bearer<Token>
The API validates the JWT by checking its signature, expiration, issuer, and audience See the example below for how to obtain an access token for the client: Use this for server-to-server authentication.
 Method Internal Working ->geta143mkCustMasterFOrSYS() ->pusha143mkCustToSystemNameSystem().
 

JWT-based access tokens are secure, stateless, and scalable, making them ideal for modern authentication and authorization systems.