The Spring AI Client Library is a simple and efficient library for interacting with the OpenAI API from your Spring Boot application. It provides a streamlined way to send requests and receive responses from OpenAI's models, making it easy to integrate AI capabilities into your Spring Boot applications. I plan on adding support for other AI models and services in the future.
NOTE: You should check out the official Spring AI project at https://spring.io/projects/spring-ai to see if it meets your needs before using this library. I created this library because I wanted a simpler way to interact with the OpenAI API in my Spring Boot projects, and I wanted to be able to support new OpenAI models and features as they are released.
- Easy configuration using Spring Boot's
application.yml
. - Supports multiple OpenAI models.
- Handles API requests and responses seamlessly.
- Provides a clean and maintainable code structure.
- Java 17 or later
- Gradle 8.10.1 or Maven 3.8.1+
- OpenAI API Key
The library is available through the Maven Central Repository. You can include it in your Spring Boot project using either Maven or Gradle.
Add the following dependency to your pom.xml
:
<dependency>
<groupId>com.digitalsanctuary</groupId>
<artifactId>ds-spring-ai-client</artifactId>
<version>1.1.4</version>
</dependency>
Add the following dependency to your build.gradle
:
dependencies {
implementation 'com.digitalsanctuary:ds-spring-ai-client:1.1.4'
}
Configure the library using the application.yml
file located in
src/main/resources
ds:
ai:
openai:
api-key: ${OPENAI_API_KEY} # OpenAI API key
This is the only required configuration. You can also configure optional properties described in the Full Example section below.
Create a service that uses the OpenAIService
to send requests to OpenAI.
package com.digitalsanctuary.springaiclient.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.digitalsanctuary.springaiclient.adapters.openai.dto.OpenAIRequest;
import com.digitalsanctuary.springaiclient.adapters.openai.dto.OpenAIResponse;
import com.digitalsanctuary.springaiclient.adapters.openai.service.OpenAIService;
@Service
public class ExampleService {
@Autowired
private OpenAIService openAIService;
public String getAIResponse(String userInput) {
// Create a simple request
OpenAIRequest request = openAIService.createRequestBuilder().userMessage("Tell me a joke.").build();
// Send the request and get the response
OpenAIResponse response = openAIService.sendRequest(request);
// Return the response message (this is a helper method that extracts the message from the response)
return response.getMessage();
}
}
Configure the library using the application.yml
file located in
src/main/resources
ds:
ai:
openai:
api-key: ${OPENAI_API_KEY} # OpenAI API key
model: gpt-4o # OpenAI model
output-tokens: 4096 # OpenAI max output tokens
api-endpoint: https://api.openai.com/v1/chat/completions
system-prompt: "You are a helpful assistant."
Create a service that uses the OpenAIService
to send requests to OpenAI.
package com.digitalsanctuary.springaiclient.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.digitalsanctuary.springaiclient.adapters.openai.dto.OpenAIRequest;
import com.digitalsanctuary.springaiclient.adapters.openai.dto.OpenAIResponse;
import com.digitalsanctuary.springaiclient.adapters.openai.service.OpenAIService;
@Service
public class ExampleService {
@Autowired
private OpenAIService openAIService;
public String getAIResponse(String userInput) {
// Create a simple request
OpenAIRequest request = openAIService.createRequestBuilder()
.userMessage("Tell me a joke.")
.model("chatgpt-4o-latest")
.outputTokens(8000)
.systemPrompt("You only tell jokes about birds")
.build();
// Send the request and get the response
OpenAIResponse response = openAIService.sendRequest(request);
// Return the response message (skipping null checks, etc. for brevity)
return response.getChoices().get(0).getMessage().getContent();
}
}
Contributions are welcome! Please fork the repository and submit a pull request with your changes. See the Developer Guide file for more information on how to contribute.
This project is licensed under the MIT License. See the LICENSE file for details.
For any questions or support, please open an issue on the GitHub repository.