Class: LLM::Buffer

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

Overview

LLM::Buffer provides an Enumerable object that tracks messages in a conversation thread. Access it through Context#messages.

Examples:

Working with message history

ctx.messages.last           # => most recent message
ctx.messages.first          # => oldest message
ctx.messages.select! { |m| m.assistant? }
ctx.messages.reverse        # => reversed copy
ctx.messages.reject! { |m| m.compaction? }

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(provider) ⇒ LLM::Buffer

Parameters:



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

def initialize(provider)
  @provider = provider
  @messages = []
end

Instance Method Details

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

This method returns an undefined value.

Parameters:



166
167
168
169
# File 'lib/llm/buffer.rb', line 166

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

#[](index) ⇒ LLM::Message?

Returns a message, or nil

Parameters:

  • index (Integer, Range)

    The message index

Returns:



177
178
179
# File 'lib/llm/buffer.rb', line 177

def [](index)
  @messages[index]
end

#clearLLM::Buffer

Removes all messages.

Returns:



124
125
126
127
# File 'lib/llm/buffer.rb', line 124

def clear
  @messages.clear
  self
end

#concat(ary) ⇒ Object

Append an array

Parameters:



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

def concat(ary)
  @messages.concat(ary)
end

#drop(n) ⇒ Array<LLM::Message>

Returns all elements after the first n.

Parameters:

  • n (Integer)

    The number of messages to skip

Returns:



134
135
136
# File 'lib/llm/buffer.rb', line 134

def drop(n)
  @messages.drop(n)
end

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

This method returns an undefined value.

Yields:

  • (LLM::Message)

    Yields each message in the conversation thread



54
55
56
57
58
59
60
# File 'lib/llm/buffer.rb', line 54

def each(...)
  if block_given?
    @messages.each { yield(_1) }
  else
    enum_for(:each, ...)
  end
end

#empty?Boolean

Returns true when the buffer is empty

Returns:

  • (Boolean)


210
211
212
# File 'lib/llm/buffer.rb', line 210

def empty?
  @messages.empty?
end

#findLLM::Message?

Find a message (in descending order)

Returns:



65
66
67
# File 'lib/llm/buffer.rb', line 65

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

#first(n = UNDEFINED) ⇒ LLM::Message, ...

Returns the first message(s) in the buffer

Parameters:

  • n (Integer, nil) (defaults to: UNDEFINED)

    The number of messages to return

Returns:



91
92
93
# File 'lib/llm/buffer.rb', line 91

def first(n = UNDEFINED)
  n.equal?(UNDEFINED) ? @messages.first : @messages.first(n)
end

#inspectString

Returns:

  • (String)


196
197
198
# File 'lib/llm/buffer.rb', line 196

def inspect
  "#<#{LLM::Utils.object_id(self)} message_count=#{@messages.size}>"
end

#last(n = UNDEFINED) ⇒ LLM::Message, ...

Returns the last message(s) in the buffer

Parameters:

  • n (Integer, nil) (defaults to: UNDEFINED)

    The number of messages to return

Returns:



82
83
84
# File 'lib/llm/buffer.rb', line 82

def last(n = UNDEFINED)
  n.equal?(UNDEFINED) ? @messages.last : @messages.last(n)
end

#popvoid

This method returns an undefined value.

Pop the last element from the tail of the buffer



158
159
160
# File 'lib/llm/buffer.rb', line 158

def pop
  @messages.pop
end

#reject! {|LLM::Message| ... } ⇒ LLM::Buffer Also known as: delete_if

Removes messages matching the block in-place.

Yields:

Returns:



99
100
101
102
# File 'lib/llm/buffer.rb', line 99

def reject!(&)
  @messages.reject!(&)
  self
end

#replace(messages) ⇒ LLM::Buffer

Replace the tracked messages

Parameters:

  • messages (Array<LLM::Message>)

    The replacement messages

Returns:



45
46
47
48
# File 'lib/llm/buffer.rb', line 45

def replace(messages)
  @messages.replace(messages)
  self
end

#reverseArray

Returns a reversed copy of the internal array.

Returns:

  • (Array)


184
185
186
# File 'lib/llm/buffer.rb', line 184

def reverse
  @messages.reverse
end

#rindex {|LLM::Message| ... } ⇒ Integer?

Returns the index of the last message matching the given block.

Yields:

Returns:

  • (Integer, nil)


73
74
75
# File 'lib/llm/buffer.rb', line 73

def rindex(...)
  @messages.rindex(...)
end

#select! {|LLM::Message| ... } ⇒ LLM::Buffer

Keeps messages matching the block in-place.

Yields:

Returns:



109
110
111
112
# File 'lib/llm/buffer.rb', line 109

def select!(&)
  @messages.select!(&)
  self
end

#shiftLLM::Message?

Removes and returns the first message.

Returns:



117
118
119
# File 'lib/llm/buffer.rb', line 117

def shift
  @messages.shift
end

#sizeInteger

Returns the number of messages in the buffer

Returns:

  • (Integer)

    Returns the number of messages in the buffer



203
204
205
# File 'lib/llm/buffer.rb', line 203

def size
  @messages.size
end

#slice!void

This method returns an undefined value.

Slice a portion of the internal buffer in-place



150
151
152
153
# File 'lib/llm/buffer.rb', line 150

def slice!(...)
  @messages.slice!(...)
  nil
end

#take(n) ⇒ Array<LLM::Message>

Returns the first n elements without removing them.

Parameters:

  • n (Integer)

    The number of messages to return

Returns:



143
144
145
# File 'lib/llm/buffer.rb', line 143

def take(n)
  @messages.take(n)
end

#to_jsonString

Returns:

  • (String)


190
191
192
# File 'lib/llm/buffer.rb', line 190

def to_json(...)
  LLM.json.dump(@messages, ...)
end