Class DefaultLLMMemoryManager
- All Implemented Interfaces:
LLMMemoryManager
- Since:
- 0.6.0
-
Constructor Summary
ConstructorsConstructorDescriptionDefaultLLMMemoryManager(MemoryStore memoryStore, TokenEstimator tokenEstimator, String agentId) -
Method Summary
Modifier and TypeMethodDescriptionaddMessage(LLMMessage message) Add a message to the conversation history.addMessages(List<LLMMessage> messages) Add multiple messages to the conversation history.Clear conversation history (short-term memory).Get the agent ID this manager is associated with.Get all messages in conversation history (no token limit).getConversationHistory(int maxTokens, ContextWindowStrategy strategy) Get conversation history for LLM prompt with token budget.intGet current token count of conversation history.intGet number of messages in conversation history.Get the token estimator used by this manager.Store important context in long-term memory.retrieveRelevantContext(String query, int maxTokens) Retrieve relevant context from long-term memory.summarizeOldMessages(int messagesToSummarize) Summarize old messages to save tokens.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface dev.agenor.core.memory.llm.LLMMemoryManager
remember
-
Constructor Details
-
DefaultLLMMemoryManager
public DefaultLLMMemoryManager(MemoryStore memoryStore, TokenEstimator tokenEstimator, String agentId)
-
-
Method Details
-
addMessage
Description copied from interface:LLMMemoryManagerAdd a message to the conversation history.The message is stored in both short-term (conversation) and optionally long-term (MemoryStore) memory depending on importance.
Example:
llmMemory.addMessage(LLMMessage.user("What's the weather?")) .thenRun(() -> log.info("Message added"));- Specified by:
addMessagein interfaceLLMMemoryManager- Parameters:
message- the LLM message to add- Returns:
- future that completes when message is stored
-
addMessages
Description copied from interface:LLMMemoryManagerAdd multiple messages to the conversation history.More efficient than calling addMessage() multiple times.
- Specified by:
addMessagesin interfaceLLMMemoryManager- Parameters:
messages- the messages to add- Returns:
- future that completes when all messages are stored
-
getConversationHistory
public CompletableFuture<List<LLMMessage>> getConversationHistory(int maxTokens, ContextWindowStrategy strategy) Description copied from interface:LLMMemoryManagerGet conversation history for LLM prompt with token budget.Uses the specified strategy to select which messages to include within the token budget. Older messages may be summarized or excluded.
Strategies:
Strategy implementations determine which messages to include:
- Fixed window - Last N messages that fit in budget
- Sliding window - Most recent + important messages
- Summarized - Recent messages + summary of old ones
Example:
// Obtain strategy from runtime (e.g., via dependency injection) ContextWindowStrategy strategy = ...; // Provided by runtime // Get conversation with token budget and strategy List<LLMMessage> history = llmMemory.getConversationHistory( 2000, // Max tokens strategy // Selection strategy ).join();- Specified by:
getConversationHistoryin interfaceLLMMemoryManager- Parameters:
maxTokens- maximum tokens for the contextstrategy- strategy for selecting messages- Returns:
- future with list of messages that fit in budget
-
getAllMessages
Description copied from interface:LLMMemoryManagerGet all messages in conversation history (no token limit).Warning: This may return many messages. Use with caution and prefer token-limited methods for LLM prompts.
- Specified by:
getAllMessagesin interfaceLLMMemoryManager- Returns:
- future with all conversation messages
-
remember
Description copied from interface:LLMMemoryManagerStore important context in long-term memory.This stores a key-value pair in the MemoryStore for later retrieval. Use this for facts, preferences, or important information that should persist beyond the current conversation.
Example:
llmMemory.remember("user-name", "Alice", Map.of( "category", "profile", "confidence", "high" ));- Specified by:
rememberin interfaceLLMMemoryManager- Parameters:
key- the memory keycontent- the content to remembermetadata- optional metadata for the memory- Returns:
- future that completes when stored
-
retrieveRelevantContext
Description copied from interface:LLMMemoryManagerRetrieve relevant context from long-term memory.Searches the MemoryStore for entries relevant to the query and returns them formatted for LLM context. Results are limited by token budget.
Example:
// Get user preferences (up to 500 tokens) List<MemoryEntry> context = llmMemory.retrieveRelevantContext( "user preferences", 500 ).join();- Specified by:
retrieveRelevantContextin interfaceLLMMemoryManager- Parameters:
query- the search querymaxTokens- maximum tokens for retrieved context- Returns:
- future with relevant memory entries
-
summarizeOldMessages
Description copied from interface:LLMMemoryManagerSummarize old messages to save tokens.Takes the oldest N messages and creates a summary using the LLM. The original messages are replaced with a single summary message.
Example:
// Summarize oldest 20 messages String summary = llmMemory.summarizeOldMessages(20).join(); System.out.println("Summary: " + summary);- Specified by:
summarizeOldMessagesin interfaceLLMMemoryManager- Parameters:
messagesToSummarize- number of oldest messages to summarize- Returns:
- future with summary text
-
clearConversationHistory
Description copied from interface:LLMMemoryManagerClear conversation history (short-term memory).Removes all messages from the current conversation. Long-term memories (from
LLMMemoryManager.remember(java.lang.String, java.lang.String, java.util.Map<java.lang.String, java.lang.Object>)) are not affected.- Specified by:
clearConversationHistoryin interfaceLLMMemoryManager- Returns:
- future that completes when history is cleared
-
getCurrentTokenCount
public int getCurrentTokenCount()Description copied from interface:LLMMemoryManagerGet current token count of conversation history.Returns the estimated total tokens used by all messages in the conversation history.
- Specified by:
getCurrentTokenCountin interfaceLLMMemoryManager- Returns:
- current token count
-
getMessageCount
public int getMessageCount()Description copied from interface:LLMMemoryManagerGet number of messages in conversation history.- Specified by:
getMessageCountin interfaceLLMMemoryManager- Returns:
- message count
-
getTokenEstimator
Description copied from interface:LLMMemoryManagerGet the token estimator used by this manager.- Specified by:
getTokenEstimatorin interfaceLLMMemoryManager- Returns:
- the token estimator
-
getAgentId
Description copied from interface:LLMMemoryManagerGet the agent ID this manager is associated with.- Specified by:
getAgentIdin interfaceLLMMemoryManager- Returns:
- the agent ID
-
toString
-