GEN AI


Few easy steps for integration of AI-Chat-Model to a Spring-Boot Web application using Spring AI.

  • Create a Spring-Boot web-application.
  • create REST-Api in web-application to communicate with an AI model. 
  • Angular/React frontend that provides the user interface to web-application. 

Pre-requisite -

  • JDK 11+ 
  • Maven/Gradle.
  • Spring Boot Setup
  • Basic Understanding of AI/ML Concepts
  • AI-CHAT-Model API_key and details of AI-Model provider such as OpenAI or Gemini.
  • Knowledge of Frontend framework, such as Angular or React.

Steps - 

  • Create a Spring Boot project using http://start.spring.io (Spring-Initilizer).
  • Select and add Spring Web and Spring AI dependencies ex – 
  • spring-boot-starter-web  SpringBoot Starter Web for RESTful services
  • spring-boot-starter-ai / spring-ai-openai-spring-boot-starter  Spring AI Starter for integrating AI models
  • jackson-databind  Jackson for JSON processing
  • Configure the spring-config file as application.yml or application.properties  with the API-key.
  • Create ChatClient Bean or custom classes or interface of ChatClient of Spring-AI.
  • Create a service class using above ChatClient implementation to interact with AI-Model.
    • Add following http-request Headers –
      • ContentType = MediaType.APPLICATION_JSON
      • Authorization = “Bearer AI_Model_API_Key”
    • Spring-AI API_Call Request-Parameters –
      • Model = gpt-3.5-turbo. (AI Model Name)
      • Temperature = 1 (Fine-Tuning-Param)
      • max_tokens = 110 (Fine-Tuning-Param)
      • messages in following json-schema-format –

 [ {“role”: ”user”, “content”: ”<User-Input-Prompt>”}  ]  

(Rest Api Call Request_Body Schema)

  • Create a RestController to expose the ChatClient functionality as API. Ex - “/api/ai/chatbot”.
  • Create a SPA-GUI to invoke RestController using Angular or React etc.
  • Configure CORS to enable communication between the frontend and backend (RestAPI).

Chat_Model interaction steps – 

  • Run Spring Boot Application
  • Open GUI Page.
  • Enter user-prompt as user-input.
  • Get Response from AI_Model-Prpvider. 

Share with