A spring-ai powered spring-boot microservice accessing OpenAI's api
- spring-boot: '3.2.1'
- spring-ai: 0.8.0-SNAPSHOT
- gradle: 8.5
- java: 21
- openai: API_KEY obtained from https://platform.openai.com/api-keys
This spring-boot api example is OpenAI client application by using Spring-AI. The Spring AI project aims to streamline the development of applications that incorporate artificial intelligence functionality without unnecessary complexity.
As mentioned above, the objective of project Spring AI is to focus on enabling smooth AI integration and to do that spring team has created a library spring-ai-openai-spring-boot-starter. The library does not have a release version yet so a snapshot version can be used for now to play around. As of today (28/12/2023), the latest library version is 0.8.0-SNAPSOT.
Following steps can be used to smoothly integrate AI(OpenAI) in a spring-boot application.
Create a spring-boot application using https://start.spring.io/ (spring initializer) with only spring-boot-starter-web dependency to begin with
implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter:0.8.0-SNAPSHOT'
As this library is still not released, the snapshots repository needs to be added to build.gradle to access snapshot versions.
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/snapshot' }
}
The interface org.springframework.ai.chat.ChatClient is the base for all currently supported spring-ai implementations such as OpenAI, Azure OpenAI, Hugging Face and Ollama. Planned implementations are Amazon Bedrock,Google Vertex: 'Bard'.
Currently, OpenAiChatClient which is OpenAI implementation of ChatClient enables communication with gpt-3.5-turbo.
In this example, ChatClient is used in a simplest Rest Controller as below
package com.practice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
private static final Logger LOGGER = LoggerFactory.getLogger(Controller.class);
private final ChatClient chatClient;
public Controller(ChatClient chatClient) {
this.chatClient = chatClient;
}
@GetMapping("greeting")
String greeting(){
LOGGER.info("Greeting request received");
final String aiClientResponse = chatClient.generate(
"""
As a modern generative AI model,
Generate a 5 liner greeting message in your language style for a human in text form.
Thanks in advance.
"""
);
LOGGER.info("AI Response: {}", aiClientResponse);
return aiClientResponse;
}
}
The last step to enable this spring-boot app and OpenAI api communication is to add API to configuration file
application.properties
as shown below.
spring.ai.openai.api-key=${YOUR_OPENAI_API_KEY}
Remember not to add your key in config file, instead store it in your environments variable using below command, and it will be automatically accessed by spring-boot.
export YOUR_OPENAI_API_KEY=<INSERT YOUR KEY HERE>
With these steps, your application is ready to talk to OpenAI using spring-ai library. Run the application and hit url http://localhost:8080/greeting using a browser or curl command.
The response to above request would be a response from OpenAI api in the requested form.
Blog covering this topic: https://medium.com/@krushnatkhawale/unleashing-the-power-of-generative-ai-integrating-openais-api-with-springai-7bbc9ebbe138