Class: LLM::Compactor

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/compactor.rb,
lib/llm/compactor/null.rb,
lib/llm/compactor/truncate.rb

Overview

LLM::Compactor is the superclass for context compaction strategies in llm.rb.

A compactor is bound to a context and decides whether and how to compact the conversation history when #call is invoked. Each subclass implements a different strategy: Truncate drops the oldest messages, and Null is a no-op (the default).

The compactor does not have a separate compact? predicate. It inspects the context internally and returns nil when nothing needs to happen. Callers invoke #call unconditionally.

Direct Known Subclasses

Null, Truncate

Defined Under Namespace

Classes: Null, Truncate

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctx) ⇒ LLM::Compactor

Parameters:



27
28
29
# File 'lib/llm/compactor.rb', line 27

def initialize(ctx)
  @ctx = LLM::Agent === ctx ? ctx.instance_variable_get(:@ctx) : ctx
end

Instance Attribute Details

#ctxLLM::Context (readonly)

Returns:



22
23
24
# File 'lib/llm/compactor.rb', line 22

def ctx
  @ctx
end

Instance Method Details

#call(**opts) ⇒ Object?

This method is abstract.

Parameters:

  • opts (Hash)

    Per-call options

Returns:

Raises:

  • (NotImplementedError)


35
36
37
# File 'lib/llm/compactor.rb', line 35

def call(**opts)
  raise NotImplementedError
end