Package dev.langchain4j.memory.chat
Class TokenWindowChatMemory
java.lang.Object
dev.langchain4j.memory.chat.TokenWindowChatMemory
- All Implemented Interfaces:
ChatMemory
This chat memory operates as a sliding window of
maxTokens
tokens.
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 (or multiple) is evicted.
Messages are indivisible. If a message doesn't fit, it is evicted completely.
Once added, a SystemMessage
is always retained.
Only one SystemMessage
can be held at a time.
If a new SystemMessage
with the same content is added, it is ignored.
If a new SystemMessage
with different content is added, the previous SystemMessage
is removed.
If an 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
(InMemoryChatMemoryStore
is used by default).
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(ChatMessage message) Adds a message to the chat memory.builder()
void
clear()
Clears the chat memory.id()
The ID of theChatMemory
.messages()
Retrieves messages from the chat memory.static TokenWindowChatMemory
withMaxTokens
(int maxTokens, Tokenizer tokenizer)
-
Method Details
-
id
Description copied from interface:ChatMemory
The ID of theChatMemory
.- Specified by:
id
in interfaceChatMemory
- Returns:
- The ID of the
ChatMemory
.
-
add
Description copied from interface:ChatMemory
Adds a message to the chat memory.- Specified by:
add
in interfaceChatMemory
- Parameters:
message
- TheChatMessage
to add.
-
messages
Description copied from interface:ChatMemory
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.- Specified by:
messages
in interfaceChatMemory
- Returns:
- A list of
ChatMessage
objects that represent the current state of the chat memory.
-
clear
public void clear()Description copied from interface:ChatMemory
Clears the chat memory.- Specified by:
clear
in interfaceChatMemory
-
builder
-
withMaxTokens
-