Skip to content

Commit fe4284b

Browse files
ilayaperumalgmarkpollack
authored andcommitted
Make default Chat memory repository non-static
- Update the builder to initialize the default chatmemory respository if it isn't set by the builder method Signed-off-by: Ilayaperumal Gopinathan <[email protected]>
1 parent c0062dd commit fe4284b

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

spring-ai-model/src/main/java/org/springframework/ai/chat/memory/MessageWindowChatMemory.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717
package org.springframework.ai.chat.memory;
1818

19-
import org.springframework.ai.chat.messages.Message;
20-
import org.springframework.ai.chat.messages.SystemMessage;
21-
import org.springframework.util.Assert;
22-
2319
import java.util.ArrayList;
2420
import java.util.HashSet;
2521
import java.util.List;
2622
import java.util.Set;
2723

24+
import org.springframework.ai.chat.messages.Message;
25+
import org.springframework.ai.chat.messages.SystemMessage;
26+
import org.springframework.util.Assert;
27+
2828
/**
2929
* A chat memory implementation that maintains a message window of a specified size,
3030
* ensuring that the total number of messages does not exceed the specified limit. When
@@ -36,14 +36,13 @@
3636
* {@link SystemMessage} messages are preserved while evicting other types of messages.
3737
*
3838
* @author Thomas Vitale
39+
* @author Ilayaperumal Gopinathan
3940
* @since 1.0.0
4041
*/
4142
public final class MessageWindowChatMemory implements ChatMemory {
4243

4344
private static final int DEFAULT_MAX_MESSAGES = 20;
4445

45-
private static final ChatMemoryRepository DEFAULT_CHAT_MEMORY_REPOSITORY = new InMemoryChatMemoryRepository();
46-
4746
private final ChatMemoryRepository chatMemoryRepository;
4847

4948
private final int maxMessages;
@@ -124,7 +123,7 @@ public static Builder builder() {
124123

125124
public static class Builder {
126125

127-
private ChatMemoryRepository chatMemoryRepository = DEFAULT_CHAT_MEMORY_REPOSITORY;
126+
private ChatMemoryRepository chatMemoryRepository;
128127

129128
private int maxMessages = DEFAULT_MAX_MESSAGES;
130129

@@ -142,7 +141,10 @@ public Builder maxMessages(int maxMessages) {
142141
}
143142

144143
public MessageWindowChatMemory build() {
145-
return new MessageWindowChatMemory(chatMemoryRepository, maxMessages);
144+
if (this.chatMemoryRepository == null) {
145+
this.chatMemoryRepository = new InMemoryChatMemoryRepository();
146+
}
147+
return new MessageWindowChatMemory(this.chatMemoryRepository, this.maxMessages);
146148
}
147149

148150
}

0 commit comments

Comments
 (0)