Package com.tnsai.context.compaction
Class LLMContextCompactor
java.lang.Object
com.tnsai.context.compaction.LLMContextCompactor
- All Implemented Interfaces:
ContextCompactor
LLM-based context compactor that summarizes old messages.
Uses an LLM to generate a summary of older messages while preserving the most recent messages intact.
Usage
LLMClient summarizer = LLMClientFactory.create("openai", "gpt-4o-mini");
LLMContextCompactor compactor = new LLMContextCompactor(summarizer);
CompactionConfig config = CompactionConfig.builder()
.thresholdRatio(0.80)
.preserveLastN(5)
.build();
CompactionResult result = compactor.compact(messages, config);
Compaction Process
- Separate system prompt (if preserveSystemPrompt=true)
- Identify messages to preserve (last N)
- Summarize remaining messages using LLM
- Reconstruct context: [system] + [summary] + [preserved]
- Author:
- TnsAI Team
-
Constructor Summary
ConstructorsConstructorDescriptionLLMContextCompactor(com.tnsai.llm.LLMClient llm) Creates a compactor with the given LLM client. -
Method Summary
Modifier and TypeMethodDescriptionCompacts the given messages according to configuration.intestimateTokens(List<Map<String, Object>> messages) Estimates token count for a list of messages.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.context.compaction.ContextCompactor
shouldCompact
-
Constructor Details
-
LLMContextCompactor
public LLMContextCompactor(com.tnsai.llm.LLMClient llm) Creates a compactor with the given LLM client.- Parameters:
llm- the LLM client for summarization
-
-
Method Details
-
compact
Description copied from interface:ContextCompactorCompacts the given messages according to configuration.- Specified by:
compactin interfaceContextCompactor- Parameters:
messages- the conversation messages to compactconfig- the compaction configuration- Returns:
- the compaction result
-
estimateTokens
Description copied from interface:ContextCompactorEstimates token count for a list of messages.- Specified by:
estimateTokensin interfaceContextCompactor- Parameters:
messages- the messages to estimate- Returns:
- estimated token count
-