Spring AI Groq Chat Example
Spring AI simplifies integration with AI services in Java applications. Groq Chat is an AI-driven conversational service that allows applications to interact with users intelligently. Let us delve into understanding how to implement an integration between Spring AI and Groq Chat and how it enables intelligent conversational applications.
1. What is Spring AI?
Spring AI is part of the Spring ecosystem, providing abstractions for AI model integration. It supports multiple providers like OpenAI, Azure OpenAI, Hugging Face, and local model servers. It handles prompt management, request formatting, and response parsing.
2. Code Example
In this section, we will provide a step-by-step guide to set up Spring AI to use Groq Chat in a Spring Boot application, including dependencies, configuration, and sample code to interact with the Groq Chat API.
2.1 Add Dependency (pom.xml)
Before you can use Groq Chat in your Spring AI project, you need to add the necessary Maven dependency. This dependency provides the required classes and configurations to interact with the Groq Chat API seamlessly within your Spring Boot application.
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>latest_jar_version</version>
</dependency>
2.2 application.properties
This section defines the configuration for integrating Groq Chat with Spring AI in a Spring Boot application. The application.yml file specifies the application name, Groq OpenAI API base URL, API key, and chat model options. Before adding your API key to the configuration, you need to generate a Groq API key from the Groq developer portal:
- Visit the Groq developer portal at https://console.groq.com/keys.
- Sign in with your account or create a new account if you don’t have one.
- Navigate to the API Keys section in your dashboard.
- Click on “Generate New API Key” and copy the generated key.
- Paste the key into your
application.ymlorapplication.propertiesfile underspring.ai.openai.api-key.
# application.yml
spring:
application:
name: spring-ai-groq-demo
ai:
openai:
base-url: https://api.groq.com/openai
api-key: gsk_XXXX
chat:
options:
model: llama-3.3-70b-versatile
2.3 Creating a Groq Configuration
This configuration class defines a Spring @Bean to create a Chat client by reading the API key, base URL, and model from application.yml, enabling the application to communicate with Groq Chat through Spring AI.
// GroqConfig.java
@Configuration
public class GroqConfig {
@Value("${spring.ai.openai.api-key}")
private String groqApiKey;
@Value("${spring.ai.openai.base-url}")
private String groqApiUrl;
@Value("${spring.ai.openai.chat.options.model}")
private String groqModel;
@Bean
public OpenAiChatModel customGroqChatClient() {
OpenAiApi groqOpenAiApi = new OpenAiApi.Builder()
.apiKey(groqApiKey)
.baseUrl(groqApiUrl)
.build();
return OpenAiChatModel.builder()
.openAiApi(groqOpenAiApi)
.model(groqModel)
.build();
}
}
2.3.1 Code Explanation
The GroqConfig class is a Spring configuration that defines a custom OpenAiChatModel bean for interacting with Groq Chat. It uses @Value annotations to inject the API key, base URL, and chat model from application.yml. Inside the customGroqChatClient() method, an OpenAiApi instance is built using the provided API key and base URL, and then an OpenAiChatModel is created with this API instance and the specified chat model. Registering it as a Spring @Bean makes the client available for dependency injection in controllers or services, allowing the application to send messages to Groq Chat and receive AI responses seamlessly.
2.4 Creating a Controller
Before your Spring Boot application can interact with Groq Chat, you need a REST controller to handle incoming user messages.
// ChatController.java
@RestController
public class ChatController {
@Autowired
private OpenAiChatModel customGroqChatClient;
@Autowired
public ChatController(OpenAiChatModel chatClient) {
this.customGroqChatClient = chatClient;
}
@PostMapping("/chat")
public String chat(@RequestBody String message) {
ChatOptions chatOptions = OpenAiChatOptions.builder()
.temperature(0.8)
.build();
return customGroqChatClient.call(new Prompt(message, chatOptions))
.getResult()
.getOutput()
.getText();
}
}
2.4.1 Code Explanation
The ChatController class is a Spring REST controller that exposes a /chat endpoint for handling user messages. It injects the custom OpenAiChatModel bean using @Autowired, allowing the controller to communicate with Groq Chat. When a POST request is sent to /chat with a message in the request body, the chat method creates ChatOptions with a temperature setting of 0.8, then calls customGroqChatClient.call() with a new Prompt containing the message and options. The method retrieves the result and extracts the generated text from the AI response, returning it to the client as a string, enabling real-time conversational responses.
2.5 Creating a Main class
To run your Spring Boot application and enable all the configured components, you need a main class. The SpringAiGroqApplication class serves as the entry point of the application, bootstrapping the Spring context and initializing all beans, including the OpenAiChatModel and ChatController.
// Spring Boot main application
package com.example.springaigroq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringAiGroqApplication {
public static void main(String[] args) {
SpringApplication.run(SpringAiGroqApplication.class, args);
}
}
2.6 Code Run and Demo
To run the Spring Boot Groq Chat application, use mvn spring-boot:run from the project root or build the jar using mvn clean package and run it with java -jar target/spring-ai-groq-0.0.1-SNAPSHOT.jar. Once the application starts, the /chat endpoint is available. You can send a message using curl or Postman.
Request: curl -X POST http://localhost:8080/chat \ -H "Content-Type: text/plain" \ -d "Hello, how are you?" Response: "Hi! I'm doing great. How can I assist you today?"
3. Conclusion
Integrating Groq Chat with Spring AI in a Spring Boot application allows developers to create intelligent conversational interfaces quickly and efficiently. By adding the Maven dependency, configuring the API key, creating a custom Groq client, and exposing a REST controller, you can send messages to Groq Chat and receive AI-generated responses seamlessly. Spring AI’s auto-configuration and customization options make it easy to manage and extend the integration, enabling robust, scalable, and interactive chat functionality in your Java applications.




