Module 26: Spring AI Chatbot
The Module 25 chatbot rebuilt as a Spring Boot@AcpAgent bean that talks to the model through Spring AI’s ChatClient.
Prerequisites
- Completed Module 23: Spring Boot Agent and Module 25: AI Chatbot Agent
- Java 21+ (Spring Boot 4.x requirement)
ANTHROPIC_API_KEYexported (read throughspring.ai.anthropic.api-key)
What You’ll Learn
- Calling the model through Spring AI’s provider-neutral
ChatClient - Switching model providers by swapping a starter dependency, without touching the agent
- Per-session conversation memory with
MessageWindowChatMemoryand an advisor
Dependencies
The same ACP Spring Boot starter as Module 23, plus one Spring AI model starter:As of 0.80.0, the starter is a module of the ACP Java SDK itself (
com.agentclientprotocol, replacing
org.springaicommunity); see the 0.80.0 migration guide. The
downloadable module builds against the released starter (SDK 0.18.0) by default; build with
-Psdk-candidate to use the SDK’s own starter at the coordinates shown above.The Agent
The ACP methods are identical to Module 23’s echo bean (and, as of 0.80.0, need no@Initialize
either). The only difference is the body of @Prompt:
List<MessageParam> per session by hand. Here a sliding window of the last 20 messages lives in a ChatMemory, and the advisor injects it into every call. Keying it on the ACP session ID gives each editor chat its own history.
Configuration
stdout carries the JSON-RPC protocol, so the banner is off and there is no web server:spring.ai.<provider>.* properties. ChatbotAgentBean does not change.
Streaming
ChatClient also streams. .stream().content() returns a Flux<String>:
Three Flavors, One Agent
Source Code
View on GitHubRunning the Example
ChatMemory recalls the first answer.