Bug description Firstly, when I was using the custom Advisor, I found that the debugging did not take effect. Then, in order to verify whether it was a problem with my code, I used the SimpleLoggerAdvisor from the dependencies for debugging, but still found that it did not work.
SimpleLoggerAdvisor Configuration Example
@Bean
public ChatClient zhipuAiChatClient(@Qualifier("zhiPuAiChatModel") ChatModel chatModel, ToolCallback[] toolCallbacks) {
ChatClient chatClient = ChatClient.builder(chatModel)
// 实现 Logger 的 Advisor
.defaultAdvisors(
new SimpleLoggerAdvisor()
)
.defaultToolCallbacks(toolCallbacks)
.build();
return chatClient;
}
Custom Adviso Configuration Example
@Bean
public ChatClient zhipuAiChatClient(@Qualifier("zhiPuAiChatModel") ChatModel chatModel, ToolCallback[] toolCallbacks) {
ChatClient chatClient = ChatClient.builder(chatModel)
// 实现 Logger 的 Advisor
.defaultAdvisors(
new MyLogAdvisor()
)
.defaultToolCallbacks(toolCallbacks)
.build();
return chatClient;
}
Custom Adviso Code Example
/**
* @author mi
* @data 2025/6/12 9:07
* @version 1.0
*/
@Slf4j
public class MyLogAdvisor implements CallAdvisor, StreamAdvisor {
private void before(ChatClientRequest chatClientRequest) {
log.info("请求prompt:\n{}", chatClientRequest.prompt().getContents());
}
@Override
public ChatClientResponse adviseCall(ChatClientRequest chatClientRequest, CallAdvisorChain callAdvisorChain) {
this.before(chatClientRequest);
ChatClientResponse chatClientResponse = callAdvisorChain.nextCall(chatClientRequest);
if (chatClientResponse.chatResponse() != null) {
String responseText = chatClientResponse.chatResponse().getResult().getOutput().getText();
log.info("AI响应:\n{}", responseText);
} else {
log.error("AI响应失败:\n{}", JSONUtil.toJsonStr(chatClientResponse));
}
return chatClientResponse;
}
@Override
public Flux<ChatClientResponse> adviseStream(ChatClientRequest chatClientRequest, StreamAdvisorChain streamAdvisorChain) {
this.before(chatClientRequest);
Flux<ChatClientResponse> chatClientResponseFlux = streamAdvisorChain.nextStream(chatClientRequest);
Mono<List<ChatClientResponse>> collectedFlux = chatClientResponseFlux.collectList();
collectedFlux
.doOnNext(responses -> {
StringBuilder resContent = new StringBuilder();
responses.forEach(resContent::append);
log.info("AI响应:\n{}", resContent);
})
.subscribe();
return chatClientResponseFlux;
}
@Override
public String getName() {
return "MyLogAdvisor";
}
@Override
public int getOrder() {
return Integer.MAX_VALUE;
}
}
Environment spring.ai.version 1.0.0 jdk 17
Minimal Complete Reproducible example
chatClient.prompt("hello world").advisors(new SimpleLoggerAdvisor()).stream().content();
Comment From: sunyuhan1998
If you wish to use SimpleLoggerAdvisor
, you will need to adjust the project's logging level to debug
.