Skip to main content

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

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 MessageWindowChatMemory and an advisor

Dependencies

The same ACP Spring Boot starter as Module 23, plus one Spring AI model starter:

The Agent

The ACP methods are identical to Module 23’s echo bean. The only difference is the body of @Prompt:
Module 25 kept a 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:
To use OpenAI, Gemini, or a local Ollama model, replace the starter and the 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 GitHub

Running the Example

The demo asks for a haiku, then asks for it as a limerick in the same session. The follow-up works because ChatMemory recalls the first answer.

Next Module

Module 27: LangChain4j Chatbot — the same agent on LangChain4j.