Skip to content
Open
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 @@ -19,6 +19,8 @@
import java.util.ArrayList;
import java.util.List;

import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.util.CollectionUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;
Expand Down Expand Up @@ -91,9 +93,17 @@ public ChatClientRequest before(ChatClientRequest chatClientRequest, AdvisorChai
.prompt(chatClientRequest.prompt().mutate().messages(processedMessages).build())
.build();

// 4. Add the new user message to the conversation memory.
// 4. Add user message and system message to the conversation memory: if the
// current conversation is the first round, or if the `systemMessage` has been
// updated, add both to the conversation memory.
SystemMessage systemMessage = chatClientRequest.prompt().getSystemMessage();
UserMessage userMessage = processedChatClientRequest.prompt().getUserMessage();
this.chatMemory.add(conversationId, userMessage);
if (CollectionUtils.isEmpty(memoryMessages) || !memoryMessages.contains(systemMessage)) {
this.chatMemory.add(conversationId, List.of(systemMessage, userMessage));
}
else {
this.chatMemory.add(conversationId, userMessage);
}

return processedChatClientRequest;
}
Expand Down