Spring AI is a new framework aimed at making AI development easier for Java developers. It provides a simple, Spring-friendly way to integrate AI models into your applications, including the use of templates for more structured prompts. It currently supports models from various providers.

Why Spring AI?

  • Simplified Integration: Easily incorporate AI models into your Spring Boot apps.
  • Abstractions: Work with AI concepts without getting bogged down in the details.
  • Extensibility: Customize and extend to fit your specific needs.
  • Templates: Create reusable prompt structures for consistent AI interactions.
  • Multiple Model Support: Integrate models from different providers seamlessly.

Getting Started

  1. Add Dependency:

    • Maven: Include the Spring AI starter and specific model dependencies in your pom.xml.
    • Gradle: Add them to your build.gradle. (See Spring AI documentation for the latest versions)
  2. Define Beans: Configure AI models as Spring beans.

  3. Use in Your Code: Inject AI models into your components and use their APIs.

Configuring Spring AI Starter

In your application.properties or application.yml file, you can configure the default settings for Spring AI:

# application.properties
spring.ai.provider=openai # or azure, bedrock, huggingface, vertex
# ... other properties depending on the provider

You can also customize these settings on a per-bean basis in your bean definitions.

Creating Model Beans

public class AiConfiguration {

    public OpenAIClient openAIClient() {
        return OpenAIClient.builder()

    public AzureOpenAIClient azureOpenAIClient() {
        return AzureOpenAIClient.builder()

    // Similar bean definitions for other models

Replace placeholders with your actual credentials. These beans can now be injected using @Autowired.

Using Templates

private PromptTemplate promptTemplate;

Map<String, Object> parameters = new HashMap<>();
parameters.put("subject", "spring");
parameters.put("language", "English");

String generatedJoke = promptTemplate.generate("Tell me a joke about {subject} in {language}", parameters);

Model Examples

// OpenAI
private OpenAIClient openAIClient;
String response = openAIClient.generate("Translate 'hello' into French");

// Azure OpenAI
private AzureOpenAIClient azureOpenAIClient;
String response = azureOpenAIClient.generate("Summarize this article for me.");

// Amazon Bedrock
private BedrockClient bedrockClient;
String response = bedrockClient.generate("Write a poem about nature.");

// Hugging Face
private HuggingFaceClient huggingFaceClient;
String response = huggingFaceClient.generate("Classify this email as spam or not spam.");

// Google Vertex AI
private VertexAIClient vertexAIClient;
String response = vertexAIClient.generate("Generate an image of a cat riding a bicycle.");

Remember to replace placeholders like YOUR_API_KEY and YOUR_MODEL_ID with your actual credentials.

Spring AI supports various AI tasks like text generation, language translation, and image analysis. You can even integrate with popular AI frameworks like TensorFlow and PyTorch. Templates enhance your control over AI interactions, ensuring consistent and reliable results.

Discover more from GhostProgrammer - Jeff Miller

Subscribe to get the latest posts sent to your email.

By Jeffery Miller

I am known for being able to quickly decipher difficult problems to assist development teams in producing a solution. I have been called upon to be the Team Lead for multiple large-scale projects. I have a keen interest in learning new technologies, always ready for a new challenge.