Enterprise Java

Google GenAI Chat with Spring AI

Generative Artificial Intelligence has changed the way developers build intelligent applications. Modern applications can use Large Language Models (LLMs) to understand natural language, generate content, answer questions, summarize documents, write code, and interact with users conversationally.

1. Introduction

Generative AI, commonly known as GenAI, refers to artificial intelligence systems capable of generating new content such as text, images, code, summaries, and conversational responses. Large Language Models are one of the most widely used forms of Generative AI. Google’s Gemini models provide generative AI capabilities that developers can integrate into applications. These models can process natural-language prompts and generate useful responses for use cases such as chatbots, virtual assistants, document analysis, question-answering systems, and content-generation applications. For Java and Spring developers, Spring AI provides an application framework and abstractions for working with AI models. Instead of tightly coupling application code to a particular AI provider’s API, developers can use Spring-style interfaces and configuration patterns. Combining Google’s generative AI capabilities with Spring AI makes it possible to create AI-enabled Java applications while following familiar Spring development practices.

2. Understanding Google GenAI Chat

A GenAI chat application enables users to interact with a generative AI model through natural language. The interaction begins when a user enters a question, instruction, or prompt, which is received by the Spring application. The application uses Spring AI to prepare and send the request to the configured Google Gemini model. The Gemini model analyzes the prompt, understands its context and intent, and generates an appropriate response. This generated response is then returned through Spring AI to the Spring application, which delivers the final output to the user. This approach allows Java developers to integrate conversational Generative AI capabilities into Spring-based applications while using familiar Spring programming patterns and abstractions.

2.1 What is a Prompt?

A prompt is the input given to a generative AI model. It can be a simple question or a detailed set of instructions. For example: Explain Spring Boot in simple terms.. The quality and clarity of the prompt can significantly affect the usefulness of the generated response.

2.2 Role of Spring AI

Spring AI acts as an integration layer between a Spring application and AI model providers by offering APIs and abstractions for common AI application patterns. Its important capabilities include chat model integration, prompt creation and management, structured AI responses, conversation handling, embeddings, vector database integration, Retrieval-Augmented Generation (RAG), tool or function calling, and streaming responses. It is important to understand that Spring AI does not itself provide a Large Language Model (LLM); instead, it provides the application framework and abstractions needed to communicate with supported AI model providers. Java developers can call AI provider APIs directly, but Spring AI simplifies the development process by providing familiar Spring concepts such as dependency injection, configuration properties, auto-configuration, and reusable AI abstractions. This approach can make AI integrations easier to develop, organize, and maintain while allowing developers to incorporate Generative AI functionality naturally into existing or new Spring Boot applications.

2.3 Request Flow

At a high level, the request flows through the application from the user to the REST controller, which receives the prompt and forwards it to the GenAiService. The service uses Spring AI’s ChatClient to send the prompt to the configured Google Gemini model. Gemini processes the request and generates a response, which is returned through the ChatClient and service to the REST controller and finally delivered back to the user.

3. Code Example

Before building the application, make sure Java, Maven, and a suitable IDE are installed on your development system. You also need access to Google’s Gemini models. Spring AI’s Google GenAI integration supports authentication through a Gemini Developer API key or through Google Cloud credentials when using Vertex AI. Ensure that the required credentials and project configuration are available before running the application.

3.1 Create a Spring Boot Project

Start by creating a Spring Boot project that will serve as the foundation for the GenAI chat application. Add the Spring AI Google GenAI starter to enable integration with Google’s Gemini models.

<dependencies>
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-starter-model-google-genai</artifactId>
    </dependency>
</dependencies>

The spring-ai-starter-model-google-genai dependency provides the Spring AI components required to integrate Google Gemini models with a Spring Boot application. It enables Spring AI auto-configuration and provides the model integration needed to create components such as ChatClient for sending prompts to Gemini and receiving generated responses. The dependency version can be managed through the Spring AI Bill of Materials (BOM) to keep Spring AI modules on compatible versions.

3.2 Configure the AI Model

Configure the AI model settings in the application.properties file to enable the application to communicate with Google Vertex AI and Gemini. These properties define the Google Cloud project, deployment location, and Gemini model that the application will use.

# Configure the Google/Vertex AI integration required by the Spring AI version used by the application.
spring.ai.google.genai.project-id=YOUR_PROJECT_ID
spring.ai.google.genai.location=YOUR_LOCATION
spring.ai.google.genai.chat.model=YOUR_GEMINI_MODEL

Replace YOUR_PROJECT_ID with your Google Cloud project ID, YOUR_LOCATION with the Vertex AI region where the model is available, and YOUR_GEMINI_MODEL with the Gemini model you want to use. These properties allow Spring AI to connect to the appropriate Vertex AI environment and use the configured Gemini model for chat and AI-powered operations.

3.3 Create a GenAI Service

Create a dedicated service class to handle interactions with the configured AI model. The service uses Spring AI’s ChatClient to send user messages to the model and retrieve the generated responses.

package com.example.genai.service;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.stereotype.Service;

@Service
public class GenAiService {

  private final ChatClient chatClient;

  public GenAiService(ChatClient.Builder chatClientBuilder) {
    this.chatClient = chatClientBuilder.build();
  }

  public String generateResponse(String message) {
    return chatClient
        .prompt()
        .user(message)
        .call()
        .content();
  }
}

The GenAiService class is registered as a Spring service using the @Service annotation and receives a ChatClient.Builder through constructor injection. The builder creates a reusable ChatClient, while the generateResponse() method creates a prompt, adds the supplied message as user input, sends the request to the configured AI model using call(), and returns the generated text response through content().

3.4 Create a Chat Controller

Create a REST controller to expose the GenAI service through an HTTP endpoint. The controller accepts a user message as a request parameter and passes it to the GenAiService for AI-generated processing.

package com.example.genai.controller;

import com.example.genai.service.GenAiService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GenAiController {

  private final GenAiService genAiService;

  public GenAiController(GenAiService genAiService) {
    this.genAiService = genAiService;
  }

  @GetMapping("/chat")
  public String chat(@RequestParam String message) {
    return genAiService.generateResponse(message);
  }
}

The GenAiController class is registered as a REST controller using @RestController and receives the GenAiService through constructor injection. The @GetMapping("/chat") annotation exposes a /chat GET endpoint, while @RequestParam captures the message query parameter. The controller forwards this message to the generateResponse() method and returns the AI-generated response directly to the client.

3.5 Run the Application

Before starting the application, make sure the required Google Cloud or Gemini authentication and Spring AI configuration are available. The exact authentication mechanism depends on whether the application uses the Gemini Developer API or Vertex AI. Once the project is configured, a Maven-based Spring Boot application can typically be started from the project directory with the following command: mvn spring-boot:run. The application will normally start on port 8080 unless a different server port has been configured.

3.6 Test the Chat Endpoint

After the application starts successfully, open a browser or use a REST client such as Postman or cURL to call the chat endpoint. For example:

http://localhost:8080/chat?message=What%20is%20Spring%20AI?

When the request reaches the application, the controller passes the message to GenAiService. The service uses Spring AI’s ChatClient to send the prompt to the configured Gemini model and returns the generated response.

Spring AI is a Spring ecosystem project that provides APIs and abstractions for integrating AI models into Java and Spring applications. It supports capabilities such as chat models, embeddings, vector stores, tool calling, and Retrieval-Augmented Generation (RAG).

3. Conclusion

Generative AI enables developers to add natural-language and intelligent conversational capabilities to modern applications. By combining Google Gemini with Spring AI, Java developers can integrate Large Language Models into Spring Boot applications while continuing to use familiar Spring concepts such as dependency injection, configuration properties, services, and REST controllers. In this article, we explored the basic concepts of GenAI chat, prompts, and the role of Spring AI, and demonstrated how to configure an AI model, create a GenAI service, expose a chat endpoint, and test the generated response. This simple example provides a foundation that can be extended with more advanced capabilities such as conversation memory, streaming responses, structured outputs, embeddings, vector stores, tool calling, and Retrieval-Augmented Generation (RAG) to build more sophisticated AI-powered Java applications.

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button