Spring AI Spring Boot Auto Configuration modules providing automatic setup for AI models, vector stores, MCP, and retry capabilities
Get started with Spring AI Spring Boot Auto Configuration in 5 minutes.
Add the Spring AI starter dependency for your chosen provider:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>1.1.2</version>
</dependency><dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-anthropic-spring-boot-starter</artifactId>
<version>1.1.2</version>
</dependency><dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
<version>1.1.2</version>
</dependency>Add configuration to application.properties:
spring.ai.openai.api-key=${OPENAI_API_KEY}
spring.ai.openai.chat.options.model=gpt-4
spring.ai.openai.chat.options.temperature=0.7spring.ai.anthropic.api-key=${ANTHROPIC_API_KEY}
spring.ai.anthropic.chat.options.model=claude-3-5-sonnet-20241022
spring.ai.anthropic.chat.options.temperature=0.7spring.ai.azure.openai.api-key=${AZURE_OPENAI_API_KEY}
spring.ai.azure.openai.endpoint=https://your-resource.openai.azure.com
spring.ai.azure.openai.chat.options.deployment-name=gpt-4import org.springframework.ai.chat.model.ChatModel;
import org.springframework.stereotype.Service;
@Service
public class ChatService {
private final ChatModel chatModel;
public ChatService(ChatModel chatModel) {
this.chatModel = chatModel;
}
public String chat(String message) {
return chatModel.call(message);
}
}import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class ChatRunner implements CommandLineRunner {
private final ChatService chatService;
public ChatRunner(ChatService chatService) {
this.chatService = chatService;
}
@Override
public void run(String... args) {
String response = chatService.chat("What is Spring AI?");
System.out.println("Response: " + response);
}
}./mvnw spring-boot:run<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-spring-boot-autoconfigure</artifactId>
<version>1.1.2</version>
</dependency>spring.ai.chat.memory.repository.jdbc.initialize-schema=always<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pgvector-store-spring-boot-starter</artifactId>
<version>1.1.2</version>
</dependency>spring.ai.vectorstore.pgvector.initialize-schema=true
spring.ai.vectorstore.pgvector.index-type=HNSW
spring.datasource.url=jdbc:postgresql://localhost:5432/vectordbspring.ai.retry.max-attempts=5
spring.ai.retry.backoff.initial-interval=1s
spring.ai.retry.backoff.multiplier=2spring.ai.chat.observations.log-prompt=true
management.metrics.export.prometheus.enabled=true
management.tracing.enabled=trueEnsure your API key is set in environment variables:
export OPENAI_API_KEY=your-keyIncrease the request timeout:
spring.ai.openai.chat.options.timeout=60sConfigure retry for rate limits:
spring.ai.retry.on-http-codes=429
spring.ai.retry.max-attempts=10