Spring Boot OpenAI Streaming Integration
This Spring Boot application demonstrates real-time streaming integration with OpenAI's API using Spring AI. It provides a simple endpoint that streams AI responses using Spring WebFlux.
Prerequisites
- Java 17 or higher
- Maven
- OpenAI API key
All necessary dependencies including Spring Boot starters for web, webflux, and OpenAI are declared in the Maven pom.xml.
Configuration
- Create an
application.propertiesorapplication.ymlfile insrc/main/resources - Add your OpenAI API key:
spring.ai.openai.api-key=your-api-key-here
Running the Application
- Clone this repository
- Configure your OpenAI API key as described above
- Run the application:
./mvnw spring-boot:run
The application will start on port 8080 by default.
Usage
The application exposes a streaming endpoint that returns OpenAI responses as a reactive stream.
Endpoint
GET /ai/generateStream
Parameters
message(optional): The input message to send to OpenAI- Default value: "Tell me a joke"
Testing with curl
You can test the streaming endpoint using curl. The following command will stream the response and format it using jq:
curl localhost:8080/ai/generateStream | sed 's/data://' | jq .
Implementation Details
The application uses:
- Spring WebFlux for reactive streaming
- Spring AI's OpenAI integration for AI model interaction
The main components are:
OpenAiStreamingApplication: The Spring Boot application entry pointChatController: REST controller handling the streaming endpoint
Code Details
The streaming endpoint is implemented as follows:
@GetMapping(value = "/generateStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ChatResponse> generateStream(
@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
return chatModel.stream(new Prompt(new UserMessage(message)));
}
Key points:
- The endpoint uses
MediaType.TEXT_EVENT_STREAM_VALUEto indicate that the response will be streamed as text events. - Returns a
Flux<ChatResponse>which is Spring WebFlux's reactive type for handling a stream of multiple elements. - Takes an optional
messageparameter with a default value of "Tell me a joke". - Uses Spring AI's
chatModel.stream()method which returns a reactive stream of responses from OpenAI. - The
PromptandUserMessageclasses are provided by Spring AI to structure the request to OpenAI.
When called, this endpoint:
- Takes the user's input message (or uses the default)
- Wraps it in a Spring AI
Promptobject - Streams the AI's response back to the client in real-time
- Each response chunk is automatically serialized to JSON