Class: LLM::Compactor::Truncate
- Inherits:
-
LLM::Compactor
- Object
- LLM::Compactor
- LLM::Compactor::Truncate
- Defined in:
- lib/llm/compactor/truncate.rb
Overview
An LLM::Compactor::Truncate drops the oldest messages when the conversation grows beyond a configured size, keeping only the N most recent messages.
No LLM call is made but this strategy is purely lossy. It also fast - no network required and operates purely on memory.
Instance Attribute Summary
Attributes inherited from LLM::Compactor
Instance Method Summary collapse
Methods inherited from LLM::Compactor
Constructor Details
This class inherits a constructor from LLM::Compactor
Instance Method Details
#call(keep: 64) ⇒ Array<LLM::Message>?
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/llm/compactor/truncate.rb', line 20 def call(keep: 64) keep = parse(keep) if keep <= 0 || keep > .reject(&:system?).size nil else stream.on_compaction(self) kept = take(, keep) .replace([.select(&:system?).first, *kept].compact) ctx.compacted = true stream.on_compaction_finish(self) kept end end |