Skip to content

Commit f6ab52d

Browse files
committed
fix: GH-4089 Adjusted the default priority of SafeGuardAdvisor to ensure it is always higher than that of MessageChatMemoryAdvisor, preventing sensitive terms in chat memory from repeatedly triggering SafeGuardAdvisor's interception mechanism.
Signed-off-by: Sun Yuhan <[email protected]>
1 parent c7f7b68 commit f6ab52d

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

spring-ai-client-chat/src/main/java/org/springframework/ai/chat/client/advisor/SafeGuardAdvisor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.springframework.ai.chat.client.ChatClientRequest;
2525
import org.springframework.ai.chat.client.ChatClientResponse;
26+
import org.springframework.ai.chat.client.advisor.api.Advisor;
2627
import org.springframework.ai.chat.client.advisor.api.CallAdvisor;
2728
import org.springframework.ai.chat.client.advisor.api.CallAdvisorChain;
2829
import org.springframework.ai.chat.client.advisor.api.StreamAdvisor;
@@ -46,7 +47,12 @@ public class SafeGuardAdvisor implements CallAdvisor, StreamAdvisor {
4647

4748
private static final String DEFAULT_FAILURE_RESPONSE = "I'm unable to respond to that due to sensitive content. Could we rephrase or discuss something else?";
4849

49-
private static final int DEFAULT_ORDER = 0;
50+
/**
51+
* The default priority of {@code SafeGuardAdvisor} should always be higher than that
52+
* of {@code MessageChatMemoryAdvisor} to prevent sensitive words in chat memory from
53+
* repeatedly triggering {@code SafeGuardAdvisor}'s interception mechanism.
54+
*/
55+
private static final int DEFAULT_ORDER = Advisor.DEFAULT_CHAT_MEMORY_PRECEDENCE_ORDER - 1;
5056

5157
private final String failureResponse;
5258

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/advisors.adoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,11 @@ Based on the article: [Re-Reading Improves Reasoning in LLMs](https://arxiv.org/
385385
* `SafeGuardAdvisor`
386386
+
387387
A simple advisor designed to prevent the model from generating harmful or inappropriate content.
388-
388+
+
389+
[NOTE]
390+
====
391+
If you have both `SafeGuardAdvisor` and `MessageChatMemoryAdvisor` enabled and have manually set their `order`, make sure that `SafeGuardAdvisor` has a higher priority than `MessageChatMemoryAdvisor` to prevent sensitive words in chat memory from repeatedly triggering `SafeGuardAdvisor`'s interception mechanism.
392+
====
389393

390394
=== Streaming vs Non-Streaming
391395

0 commit comments

Comments
 (0)