Class: LLM::Buffer

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/llm/buffer.rb

Overview

LLM::Buffer provides an Enumerable object that yields each message in a conversation on-demand, and only sends a request to the LLM when a response is needed.

Instance Method Summary collapse

Constructor Details

#initialize(provider) ⇒ LLM::Buffer

Parameters:



15
16
17
18
19
# File 'lib/llm/buffer.rb', line 15

def initialize(provider)
  @provider = provider
  @pending = []
  @completed = []
end

Instance Method Details

#<<(item) ⇒ void Also known as: push

This method returns an undefined value.

Parameters:

  • item ([LLM::Message, Hash])

    A message and its parameters



51
52
53
54
# File 'lib/llm/buffer.rb', line 51

def <<(item)
  @pending << item
  self
end

#each {|LLM::Message| ... } ⇒ void

This method returns an undefined value.

Yields:

  • (LLM::Message)

    Yields each message in the conversation thread

Raises:

  • (NotImplementedError)

    When the method is not implemented by a subclass



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

def each
  empty! unless @pending.empty?
  @completed.each { yield(_1) }
end

#findLLM::Message?

Find a message (in descending order)

Returns:



43
44
45
# File 'lib/llm/buffer.rb', line 43

def find(...)
  reverse_each.find(...)
end

#inspectString

Returns:

  • (String)


59
60
61
62
# File 'lib/llm/buffer.rb', line 59

def inspect
  "#<#{self.class.name}:0x#{object_id.to_s(16)} " \
  "completed_count=#{@completed.size} pending_count=#{@pending.size}>"
end

#unreadArray<LLM::Message>

Returns an array of unread messages

Returns:

See Also:



36
37
38
# File 'lib/llm/buffer.rb', line 36

def unread
  reject(&:read?)
end