Class MessageWindowChatMemory
- All Implemented Interfaces:
ChatMemory
maxMessagesProvider.
It retains as many of the most recent messages as can fit into the window.
If there isn't enough space for a new message, the oldest one is evicted.
The maximum number of messages can be provided either statically or dynamically
through the maxMessagesProvider. When supplied dynamically, the effective
window size can change at runtime, and the sliding-window behavior always respects
the most recent value returned by the provider.
The rules for SystemMessage:
- Once added, a
SystemMessageis always retained, it cannot be removed. - Only one
SystemMessagecan be held at a time. - If a new
SystemMessagewith the same content is added, it is ignored. - If a new
SystemMessagewith different content is added, the previousSystemMessageis removed. UnlessMessageWindowChatMemory.Builder.alwaysKeepSystemMessageFirst(Boolean)is set totrue, the newSystemMessageis added to the end of the message list.
AiMessage containing ToolExecutionRequest(s) is evicted,
the following orphan ToolExecutionResultMessage(s) are also automatically evicted
to avoid problems with some LLM providers (such as OpenAI)
that prohibit sending orphan ToolExecutionResultMessage(s) in the request.
The state of chat memory is stored in ChatMemoryStore (SingleSlotChatMemoryStore is used by default).
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(ChatMessage message) Adds a message to the chat memory.addAsync(List<ChatMessage> messagesToAdd) Non-blocking counterpart ofChatMemory.add(Iterable), used by the asynchronous (CompletableFuture/CompletionStage) and reactive (Flow.Publisher) AI Service APIs.builder()voidclear()Clears the chat memory.id()The ID of theChatMemory.messages()Retrieves messages from the chat memory.Non-blocking counterpart ofChatMemory.messages(), used by the asynchronous (CompletableFuture/CompletionStage) and reactive (Flow.Publisher) AI Service APIs.voidset(Iterable<ChatMessage> iter) Replaces all messages in the chat memory with the specified messages.setAsync(List<ChatMessage> messages) Non-blocking counterpart ofChatMemory.set(Iterable), used by the asynchronous (CompletableFuture/CompletionStage) and reactive (Flow.Publisher) AI Service APIs (e.g. to rewrite memory for tool compensation without blocking the model-delivery thread).static MessageWindowChatMemorywithMaxMessages(int maxMessages) Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ChatMemory
add, add, set
-
Method Details
-
id
Description copied from interface:ChatMemoryThe ID of theChatMemory.- Specified by:
idin interfaceChatMemory- Returns:
- The ID of the
ChatMemory.
-
add
Description copied from interface:ChatMemoryAdds a message to the chat memory.- Specified by:
addin interfaceChatMemory- Parameters:
message- TheChatMessageto add.
-
addAsync
Description copied from interface:ChatMemoryNon-blocking counterpart ofChatMemory.add(Iterable), used by the asynchronous (CompletableFuture/CompletionStage) and reactive (Flow.Publisher) AI Service APIs. It takes a list (rather than a single message) so that adding several messages at once is a single operation; to add one message, pass a singleton list.The default implementation returns a failed future carrying
AsyncNotSupportedException: a memory backed by a blockingChatMemoryStoreis not silently offloaded to a worker thread. Implementations should compose the store'sgetMessagesAsync/updateMessagesAsync, persisting all messages in a single read-modify-write (fewer round trips and an atomic update).Callers must not invoke this method concurrently for the same memory: implementations typically read, modify and write the store, so concurrent calls would race. The AI Service chains its calls sequentially. Note that this race window is wider than with the blocking
ChatMemory.add(Iterable): here the read and the write are separated by future composition and thread hops rather than a single uninterrupted call, so sharing one memory across concurrent invocations is more likely to silently drop an update than it was with the synchronous API.- Specified by:
addAsyncin interfaceChatMemory- Parameters:
messagesToAdd- TheChatMessages to add.- Returns:
- A future that completes when the messages have been added.
-
set
Description copied from interface:ChatMemoryReplaces 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
ChatMemory.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.
- Specified by:
setin interfaceChatMemory- Parameters:
iter- TheChatMessages to set. Must not benullor empty.
-
setAsync
Description copied from interface:ChatMemoryNon-blocking counterpart ofChatMemory.set(Iterable), used by the asynchronous (CompletableFuture/CompletionStage) and reactive (Flow.Publisher) AI Service APIs (e.g. to rewrite memory for tool compensation without blocking the model-delivery thread).Replaces the entire message history with
messages. Like the other async methods, callers must not invoke it concurrently for the same memory. The default implementation returns a failed future carryingAsyncNotSupportedException; seeChatMemory.addAsync(List)for the rationale.- Specified by:
setAsyncin interfaceChatMemory- Parameters:
messages- TheChatMessages to set.- Returns:
- A future that completes when the messages have been set.
-
messages
Description copied from interface:ChatMemoryRetrieves 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.- Specified by:
messagesin interfaceChatMemory- Returns:
- A list of
ChatMessageobjects that represent the current state of the chat memory.
-
messagesAsync
Description copied from interface:ChatMemoryNon-blocking counterpart ofChatMemory.messages(), used by the asynchronous (CompletableFuture/CompletionStage) and reactive (Flow.Publisher) AI Service APIs.The default implementation returns a failed future carrying
AsyncNotSupportedException; seeChatMemory.addAsync(List)for the rationale.- Specified by:
messagesAsyncin interfaceChatMemory- Returns:
- A future that completes with the current state of the chat memory.
-
clear
public void clear()Description copied from interface:ChatMemoryClears the chat memory.- Specified by:
clearin interfaceChatMemory
-
builder
-
withMaxMessages
-