Skip to content

Commit a3c2a27

Browse files
committed
fix: GH-4170 Fixed the issue where SystemMessage was not being saved to the chat memory.
Signed-off-by: Sun Yuhan <[email protected]>
1 parent 7e8482e commit a3c2a27

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import org.springframework.ai.chat.messages.SystemMessage;
23+
import org.springframework.util.CollectionUtils;
2224
import reactor.core.publisher.Flux;
2325
import reactor.core.publisher.Mono;
2426
import reactor.core.scheduler.Scheduler;
@@ -91,9 +93,17 @@ public ChatClientRequest before(ChatClientRequest chatClientRequest, AdvisorChai
9193
.prompt(chatClientRequest.prompt().mutate().messages(processedMessages).build())
9294
.build();
9395

94-
// 4. Add the new user message to the conversation memory.
96+
// 4. Add user message and system message to the conversation memory: if the
97+
// current conversation is the first round, or if the `systemMessage` has been
98+
// updated, add both to the conversation memory.
99+
SystemMessage systemMessage = chatClientRequest.prompt().getSystemMessage();
95100
UserMessage userMessage = processedChatClientRequest.prompt().getUserMessage();
96-
this.chatMemory.add(conversationId, userMessage);
101+
if (CollectionUtils.isEmpty(memoryMessages) || !memoryMessages.contains(systemMessage)) {
102+
this.chatMemory.add(conversationId, List.of(systemMessage, userMessage));
103+
}
104+
else {
105+
this.chatMemory.add(conversationId, userMessage);
106+
}
97107

98108
return processedChatClientRequest;
99109
}

0 commit comments

Comments
 (0)