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 <ilayaperumal.gopinathan@broadcom.com>
This commit is contained in:
committed by
Mark Pollack
parent
c0062dd3b5
commit
fe4284b5f2
@@ -16,15 +16,15 @@
|
||||
|
||||
package org.springframework.ai.chat.memory;
|
||||
|
||||
import org.springframework.ai.chat.messages.Message;
|
||||
import org.springframework.ai.chat.messages.SystemMessage;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.ai.chat.messages.Message;
|
||||
import org.springframework.ai.chat.messages.SystemMessage;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A chat memory implementation that maintains a message window of a specified size,
|
||||
* ensuring that the total number of messages does not exceed the specified limit. When
|
||||
@@ -36,14 +36,13 @@ import java.util.Set;
|
||||
* {@link SystemMessage} messages are preserved while evicting other types of messages.
|
||||
*
|
||||
* @author Thomas Vitale
|
||||
* @author Ilayaperumal Gopinathan
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public final class MessageWindowChatMemory implements ChatMemory {
|
||||
|
||||
private static final int DEFAULT_MAX_MESSAGES = 20;
|
||||
|
||||
private static final ChatMemoryRepository DEFAULT_CHAT_MEMORY_REPOSITORY = new InMemoryChatMemoryRepository();
|
||||
|
||||
private final ChatMemoryRepository chatMemoryRepository;
|
||||
|
||||
private final int maxMessages;
|
||||
@@ -124,7 +123,7 @@ public final class MessageWindowChatMemory implements ChatMemory {
|
||||
|
||||
public static class Builder {
|
||||
|
||||
private ChatMemoryRepository chatMemoryRepository = DEFAULT_CHAT_MEMORY_REPOSITORY;
|
||||
private ChatMemoryRepository chatMemoryRepository;
|
||||
|
||||
private int maxMessages = DEFAULT_MAX_MESSAGES;
|
||||
|
||||
@@ -142,7 +141,10 @@ public final class MessageWindowChatMemory implements ChatMemory {
|
||||
}
|
||||
|
||||
public MessageWindowChatMemory build() {
|
||||
return new MessageWindowChatMemory(chatMemoryRepository, maxMessages);
|
||||
if (this.chatMemoryRepository == null) {
|
||||
this.chatMemoryRepository = new InMemoryChatMemoryRepository();
|
||||
}
|
||||
return new MessageWindowChatMemory(this.chatMemoryRepository, this.maxMessages);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user