Day 7 of Spring AI Series: How Advisors Form a Filter Chain for AI Applications
This article explains Spring AI Advisors as interceptors that can modify requests and responses in the LLM call chain, demonstrates building a custom logging Advisor with code examples, shows how ordering controls execution, and reviews the built‑in Advisors for memory, RAG, logging, and safety.
Advisor definition
Advisor is an interceptor on the large‑model call chain that can insert logic before a request is sent to the model and after the response is returned.
Built‑in Advisors
MessageChatMemoryAdvisor– injects conversation history before the model call and saves memory after the call (Day 6). QuestionAnswerAdvisor – performs retrieval‑augmented generation by fetching relevant documents and adding them to the prompt before the call (Day 10). SimpleLoggerAdvisor – logs request and response. SafeGuardAdvisor – filters sensitive words and enforces content safety.
Call chain flow
请求 ──► [Logger] ──► [Memory] ──► [RAG] ──► 大模型
│
响应 ◄── [Logger] ◄── [Memory] ◄── [RAG] ◄────┘Each Advisor receives the request, may process it, then calls chain.nextCall(request) to forward to the next element. After the downstream response returns, the Advisor can further process the response.
Example: SimpleLoggerAdvisor
public class SimpleLoggerAdvisor implements CallAdvisor, StreamAdvisor {
private static final Logger logger = LoggerFactory.getLogger(SimpleLoggerAdvisor.class);
@Override
public String getName() {
return this.getClass().getSimpleName();
}
@Override
public int getOrder() {
return 0; // smaller numbers execute earlier
}
@Override
public ChatClientResponse adviseCall(ChatClientRequest request, CallAdvisorChain chain) {
logger.info("请求: {}", request); // before model call
ChatClientResponse response = chain.nextCall(request);
logger.info("响应: {}", response); // after model call
return response;
}
@Override
public Flux<ChatClientResponse> adviseStream(ChatClientRequest request, StreamAdvisorChain chain) {
logger.info("请求: {}", request);
return chain.nextStream(request);
}
}Register the advisor with a ChatClient builder:
ChatClient chatClient = ChatClient.builder(chatModel)
.defaultAdvisors(new SimpleLoggerAdvisor())
.build();Ordering
The getOrder() method determines execution precedence; a smaller return value runs earlier. This controls whether memory runs before RAG, whether logging wraps the whole chain, and similar ordering decisions.
Key insight
Complex applications compose functionality by attaching Advisors to the chain rather than using conditional logic, keeping memory, retrieval, logging, and safety concerns decoupled.
Reference URLs
Advisors API: https://docs.spring.io/spring-ai/reference/api/advisors.html
ChatClient API: https://docs.spring.io/spring-ai/reference/api/chatclient.html
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Tech Ocean
Focused on AI programming, sharing ready-to-use development efficiency solutions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
