Getting Started
This guide will help you get started with Spring AI Watsonx.ai integration in your Spring Boot application.
Prerequisites
Before you begin, ensure you have:
-
JDK 17 or later
-
Maven 3.9+ or Gradle 7.5+
-
An IBM Cloud account with Watsonx.ai access
-
Watsonx.ai API credentials
Create IBM Cloud Account
-
Visit IBM Cloud
-
Sign up for a free account or log in
-
Navigate to the Watsonx.ai service
-
Create a new Watsonx.ai instance
Obtain API Credentials
-
Go to your Watsonx.ai service instance
-
Navigate to Service Credentials
-
Create new credentials or use existing ones
-
Note down:
-
API Key
-
Service URL
-
Project ID
-
Add Dependencies
Add the Spring AI Watsonx.ai starter to your project:
Maven
Add to your pom.xml:
<dependency>
<groupId>org.springaicommunity</groupId>
<artifactId>spring-ai-starter-model-watsonx-ai</artifactId>
<version>1.0.0</version>
</dependency>
Gradle
Add to your build.gradle:
implementation 'org.springaicommunity:spring-ai-starter-model-watsonx-ai:1.0.0'
Configure Application
Add your Watsonx.ai credentials to application.properties:
spring.ai.watsonx.ai.api-key=${WATSONX_AI_API_KEY}
spring.ai.watsonx.ai.url=${WATSONX_AI_URL}
spring.ai.watsonx.ai.project-id=${WATSONX_AI_PROJECT_ID}
Or use application.yml:
spring:
ai:
watsonx:
ai:
api-key: ${WATSONX_AI_API_KEY}
url: ${WATSONX_AI_URL}
project-id: ${WATSONX_AI_PROJECT_ID}
Set Environment Variables
Set your credentials as environment variables:
export WATSONX_AI_API_KEY=your_api_key_here
export WATSONX_AI_URL=https://us-south.ml.cloud.ibm.com
export WATSONX_AI_PROJECT_ID=your_project_id_here
Create Your First Chat Application
Create a simple Spring Boot application:
package com.example.watsonxdemo;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class WatsonxDemoApplication {
private final ChatModel chatModel;
public WatsonxDemoApplication(ChatModel chatModel) {
this.chatModel = chatModel;
}
@GetMapping("/chat")
public String chat(@RequestParam(defaultValue = "Hello!") String message) {
return chatModel.call(message);
}
public static void main(String[] args) {
SpringApplication.run(WatsonxDemoApplication.class, args);
}
}
Run Your Application
Start your application:
mvn spring-boot:run
Or with Gradle:
gradle bootRun
Test Your Application
Test the chat endpoint:
curl "http://localhost:8080/chat?message=What is Spring AI?"
You should receive a response from the Watsonx.ai model!
Next Steps
Now that you have a basic application running, explore more features:
-
Chat Models - Learn about chat model capabilities
-
Embeddings - Generate text embeddings
-
Configuration - Advanced configuration options
-
Samples - Explore sample applications
Common Issues
Authentication Errors
If you see authentication errors:
-
Verify your API key is correct
-
Check that the URL matches your region
-
Ensure the project ID is valid
-
Confirm your IBM Cloud account has Watsonx.ai access
Connection Timeouts
If you experience timeouts:
-
Check your network connection
-
Verify the Watsonx.ai service is available
-
Increase timeout settings in configuration
Dependency Conflicts
If you have dependency conflicts:
-
Check Spring Boot version compatibility
-
Exclude conflicting transitive dependencies
-
Use dependency management to align versions