Skip to content

GH-4089 Fixed the issue where sensitive words in chat memory repeatedly triggered SafeGuardAdvisor interception #4112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.springframework.ai.chat.client.ChatClientRequest;
import org.springframework.ai.chat.client.ChatClientResponse;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.client.advisor.api.CallAdvisor;
import org.springframework.ai.chat.client.advisor.api.CallAdvisorChain;
import org.springframework.ai.chat.client.advisor.api.StreamAdvisor;
Expand All @@ -46,7 +47,12 @@ public class SafeGuardAdvisor implements CallAdvisor, StreamAdvisor {

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?";

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

private final String failureResponse;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,11 @@ Based on the article: [Re-Reading Improves Reasoning in LLMs](https://arxiv.org/
* `SafeGuardAdvisor`
+
A simple advisor designed to prevent the model from generating harmful or inappropriate content.

+
[NOTE]
====
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.
====

=== Streaming vs Non-Streaming

Expand Down