Spring AI is a revolutionary framework that integrates artificial intelligence capabilities into the popular Spring Boot platform. Whether you're a seasoned developer looking to explore AI or a curious beginner, understanding Spring AI's sample code is a great way to start. This article will provide an in-depth look at Spring AI's sample code, guiding you through its intricacies and helping you get started with AI-powered applications.

Before diving into the code, it's essential to understand what makes Spring AI unique. Spring AI simplifies AI integration by providing out-of-the-box Deep Java Library (DJL) support, allowing developers to build, train, and deploy AI models directly within their Spring Boot applications. Now, let's explore the Spring AI sample code and see what it entails.

Setting Up a Spring AI Project
The first step in understanding the Spring AI sample code is to set up a Spring AI project. Spring AI uses Spring Boot's starter dependency management to get started quickly. In your Maven or Gradle build file, add the Spring AI starter dependency:

```xml
Once the dependency is added, you're ready to explore the Spring AI sample code. Let's dive into some of the essential components and examples provided by Spring AI.
NDarrays and Tensors in Spring AI

Spring AI's sample code introduction begins with exploring NDarrays and tensors, fundamental data structures in AI. NDarrays are multi-dimensional arrays, while tensors represent complex data structures consisting of arrays. Spring AI's sample code demonstrates creating and manipulating NDarrays and tensors using DJL:
```java import org.nd4j.jita.conf.CUDAExecution; import org.nd4j.jita.conf.NativeTag; import org.springframework.ai.ndarray.NDManager; CUDAExecution.getInstance().getConfiguration().allowMultiGPU(false); CUDAExecution.getInstance().getConfiguration().blocking(false); NDManager ndManager = NDManager.newBaseManager(); NDArray tensor = ndManager.create(new float[][]{{1, 2, 3}, {4, 5, 6}}, 'f'); ```
The above code snippet demonstrates creating an NDManager instance and initializing an NDArray representing a 2D tensor with float data type.
IAE peripherals and NNservices in Spring AI

IAE (Intelligent Application Environment) peripherals and NNs (Neural Networks) services are essential components in Spring AI that facilitate AI model training and inference. The sample code demonstrates how to create IAE peripherals and NNs services, enabling communication between your Spring Boot application and AI models:
```java import org.springframework.ai.iae.peripherals.file.FilePeripheral; import org.springframework.ai.nnservices.NNService; FilePeripheral filePeripheral = new FilePeripheral("model.ndjson"); NNService nnService = new NNService(filePeripheral); ```
The above code snippet creates a FilePeripheral for loading a pre-trained AI model and initializes an NNService to interact with the model.
AI Model Training and Inference with Spring AI

Another crucial aspect of the Spring AI sample code is the demonstration of AI model training and inference. Spring AI utilizes DJL to build and train AI models, making the process accessible to Spring Boot developers.
Building and Training an AI Model with Spring AI




















The sample code illustrates how to create a simple feed-forward neural network using DJL and train it using the MNIST handwritten digit dataset:
```java import org.deeplearning4j.nn.conf.WorkspaceMode; import org.deeplearning4j.nn.graph.ComputationGraph; import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; import org.springframework.ai.nn.recipes.mnist.MnistDataIterator; DataSetIterator mnistTrain = new MnistDataIterator(60000, 10000, true); DataSetIterator mnistTest = new MnistDataIterator(20000, 10000, false); ComputationGraph model = new ComputationGraph(cg); model.fit(mnistTrain); ```
The above code snippet demonstrates using the MNISTDataIterator to load the MNIST dataset, creating a simple neural network, and training the model using the training data.
AI Model Inference with Spring AI
After training your AI model, Spring AI enables seamless integration with your Spring Boot application for model inference:
```java import org.nd4j.jita.conf.CUDAExecution; import org.springframework.ai.nn.recipes.mnist.MnistInference; CUDAExecution.getInstance().getConfiguration().blocking(false); CUDAExecution.getInstance().getConfiguration().allowMultiGPU(false); NNService nnService = new NNService(new FilePeripheral("model.ndjson")); MnistInference mnistInference = new MnistInference(nnService); int result = mnistInference.infer(imageData); // imageData is the input image to classify ```
The above code snippet demonstrates using the pre-trained MNIST model for inference, passing an input image, and retrieving the predicted digit.
Spring AI's sample code provides an extensive range of examples, guiding developers through various aspects of AI integration using Spring Boot. By exploring the Spring AI sample code, you'll gain a solid understanding of the framework and be well-equipped to build AI-powered applications. Start your journey with Spring AI today and unlock the full potential of AI integration with your favorite application platform.