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
-
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)
- Maven: Include the Spring AI starter and specific model dependencies in your
-
Define Beans: Configure AI models as Spring beans.
-
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
spring.ai.openai.api-key=YOUR_API_KEY
spring.ai.azure.api-key=YOUR_API_KEY
spring.ai.azure.endpoint=YOUR_ENDPOINT
# ... other properties depending on the provider
You can also customize these settings on a per-bean basis in your bean definitions.
Creating Model Beans
@Configuration
public class AiConfiguration {
@Bean
public OpenAIClient openAIClient() {
return OpenAIClient.builder()
.apiKey("YOUR_API_KEY")
.build();
}
@Bean
public AzureOpenAIClient azureOpenAIClient() {
return AzureOpenAIClient.builder()
.endpoint("YOUR_ENDPOINT")
.apiKey("YOUR_API_KEY")
.build();
}
// Similar bean definitions for other models
}
Replace placeholders with your actual credentials. These beans can now be injected using @Autowired
.
Using Templates
@Autowired
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
@Autowired
private OpenAIClient openAIClient;
String response = openAIClient.generate("Translate 'hello' into French");
// Azure OpenAI
@Autowired
private AzureOpenAIClient azureOpenAIClient;
String response = azureOpenAIClient.generate("Summarize this article for me.");
// Amazon Bedrock
@Autowired
private BedrockClient bedrockClient;
String response = bedrockClient.generate("Write a poem about nature.");
// Hugging Face
@Autowired
private HuggingFaceClient huggingFaceClient;
String response = huggingFaceClient.generate("Classify this email as spam or not spam.");
// Google Vertex AI
@Autowired
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.