Spring AI: Build AI-Driven Applications with Spring Boot and Amazon Bedrock

The integration of AI capabilities into modern applications has become a critical trend in software development. With frameworks like Spring Boot and AWS Bedrock, developers can efficiently build robust, scalable, and AI-driven applications. This guide provides a step-by-step process to create a Spring Boot application integrated with Bedrock AI using the spring-ai-bedrock-ai-spring-boot-starter library.

Technologies Involved

1. Spring Boot

Spring Boot is a popular framework for building Java-based microservices and applications. It simplifies application setup with its opinionated approach and provides essential features such as dependency management, embedded servers, and auto-configuration.

  • Advantages:
    • Rapid development with minimal configuration.
    • Rich ecosystem of libraries and extensions.
    • Production-ready tools like Actuator for monitoring and metrics.

2. AWS Bedrock

AWS Bedrock provides developers with the ability to build and scale generative AI applications using pre-trained foundational models from providers like Anthropic, Stability AI, and more.

  • Key Features:
    • Pre-trained foundational models for text, images, and more.
    • Scalable, serverless infrastructure for AI workloads.
    • Easy integration with AWS services for data storage, monitoring, and security.

3. spring-ai-bedrock-ai-spring-boot-starter

This Spring Boot starter simplifies the integration of Bedrock AI into Spring applications. It abstracts the complexities of interacting with AI models, enabling developers to focus on building business logic.

  • Highlights:
    • Pre-configured APIs for AI requests and responses.
    • Built-in support for handling prompts, tokens, and configurations.
    • Seamless integration with Spring Boot's dependency injection and configuration management.

Why Combine These Technologies?

  • Spring Boot provides the foundation for building scalable, maintainable applications.
  • AWS Bedrock brings the power of generative AI to these applications.
  • The spring-ai-bedrock-ai-spring-boot-starter acts as the bridge, ensuring seamless integration and reducing the development effort.

Step 1: Create the Project

  1. Visit Spring Initializr.
  2. Select the following options:
    • Project: Maven
    • Language: Java
    • Spring Boot: 3.4.x (or the latest compatible version)
    • Dependencies:
      • Spring Web
      • Amazon Bedrock
  3. Generate the project and download the ZIP file.
  4. Extract the project and import it into your IDE (e.g., IntelliJ IDEA or Eclipse).

Step 2: Dependency

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-bedrock-ai-spring-boot-starter</artifactId>
    </dependency>
</dependencies>

Step 3: Configure application.properties

To configure spring.ai.api-key and spring.ai.endpoint for your application, you need to obtain them from the AI service provider, such as Amazon Bedrock. Follow these steps:

  1. Log in to the AWS Management Console and ensure Amazon Bedrock is enabled for your account.
  2. Create an IAM user or role with programmatic access, attach the AmazonBedrockFullAccess policy, and download the access key ID and secret access key (this is your spring.ai.api-key).
  3. Locate the Bedrock endpoint URL in the AWS Management Console (e.g., https://bedrock.<region>.amazonaws.com) and replace <region> with your AWS region (this is your spring.ai.endpoint).

Add the following configurations in src/main/resources/application.properties:

spring.application.name=spring-ai-demo
server.port=8080

# AI-related configuration
spring.ai.api-key=your-bedrock-api-key
spring.ai.endpoint=https://your-ai-endpoint-url
spring.ai.model=gpt-4-turbo

Replace your-bedrock-api-key and your-ai-endpoint-url with your actual credentials and endpoint URL.


Step 4: Create the Controller

Create a REST controller to handle requests and interact with the AI API.

import org.springframework.ai.core.AiClient;
import org.springframework.ai.core.model.AiRequest;
import org.springframework.ai.core.model.AiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/ai")
public class AiController {

    private final AiClient aiClient;

    @Autowired
    public AiController(AiClient aiClient) {
        this.aiClient = aiClient;
    }

    @PostMapping("/generate")
    public AiResponse generateResponse(@RequestBody String prompt) {
        AiRequest aiRequest = new AiRequest();
        aiRequest.setPrompt(prompt);
        aiRequest.setMaxTokens(150);

        return aiClient.generate(aiRequest);
    }
}

Step 5: Test the Application

  1. Run the application using your IDE or by executing the following command:
    ./mvnw spring-boot:run
  2. Test the endpoint using Postman or curl:
    • Endpoint: POST http://localhost:8080/api/v1/ai/generate
    • Body (JSON):
      {
        "prompt": "Explain the significance of Spring Boot."
      }
  3. You should receive an AI-generated response from the configured Bedrock AI model.

Next Steps

  • Logging and Error Handling: Add proper exception handling for better debugging.
  • Environment Variables: Secure API keys using environment variables or a secrets manager.
  • Advanced Features: Explore more AI capabilities like fine-tuning and custom prompts.

To access the source code or download the project, visit the GitHub repository:

🔗 GitHub - Spring AI Demo Project

Feel free to clone or fork the repository to get started quickly!

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete