Interface ChatMemory
- All Known Implementing Classes:
MessageWindowChatMemory, TokenWindowChatMemory
public interface ChatMemory
Represents the memory (history) of a chat conversation.
Since language models do not keep the state of the conversation, it is necessary to provide all previous messages
on every interaction with the language model.
ChatMemory helps with keeping track of the conversation and ensuring that messages fit within language model's context window.-
Method Summary
Modifier and TypeMethodDescriptionvoidadd(ChatMessage message) Adds a message to the chat memory.default voidadd(ChatMessage... messages) Adds messages to the chat memorydefault voidadd(Iterable<ChatMessage> messages) Adds messages to the chat memoryvoidclear()Clears the chat memory.id()The ID of theChatMemory.messages()Retrieves messages from the chat memory.default voidset(ChatMessage... messages) Replaces all messages in the chat memory with the specified messages.default voidset(Iterable<ChatMessage> messages) Replaces all messages in the chat memory with the specified messages.
-
Method Details
-
id
-
add
Adds a message to the chat memory.- Parameters:
message- TheChatMessageto add.
-
add
Adds messages to the chat memory- Parameters:
messages- TheChatMessages to add
-
add
Adds messages to the chat memory- Parameters:
messages- TheChatMessages to add
-
set
Replaces all messages in the chat memory with the specified messages. Unlikeadd, this method replaces the entire message history rather than appending to it.The default implementation delegates to
set(Iterable<ChatMessage>).NOTE: This method is never called automatically by LangChain4j.
- Parameters:
messages- TheChatMessages to set. Must not benullor empty.- Since:
- 1.11.0
-
set
Replaces all messages in the chat memory with the specified messages. Unlikeadd, this method replaces the entire message history rather than appending to it.Implementations should override this method to provide more efficient atomic operations if possible. The default implementation calls
clear()followed byadd(Iterable<ChatMessage>)which is not atomic.This method will typically be used when chat memory needs to be re-written to implement things like memory compaction.
NOTE: This method is never called automatically by LangChain4j.
- Parameters:
messages- TheChatMessages to set. Must not benullor empty.- Since:
- 1.11.0
-
messages
List<ChatMessage> messages()Retrieves messages from the chat memory. Depending on the implementation, it may not return all previously added messages, but rather a subset, a summary, or a combination thereof.- Returns:
- A list of
ChatMessageobjects that represent the current state of the chat memory.
-
clear
void clear()Clears the chat memory.
-