Class: LLM::Buffer
Overview
LLM::Buffer provides an Enumerable object that tracks messages in a conversation thread. Access it through Context#messages.
Instance Method Summary collapse
- #<<(item) ⇒ void (also: #push)
-
#[](index) ⇒ LLM::Message?
Returns a message, or nil.
-
#clear ⇒ LLM::Buffer
Removes all messages.
-
#concat(ary) ⇒ Object
Append an array.
-
#drop(n) ⇒ Array<LLM::Message>
Returns all elements after the first n.
- #each {|LLM::Message| ... } ⇒ void
-
#empty? ⇒ Boolean
Returns true when the buffer is empty.
-
#find ⇒ LLM::Message?
Find a message (in descending order).
-
#first(n = UNDEFINED) ⇒ LLM::Message, ...
Returns the first message(s) in the buffer.
- #initialize(provider) ⇒ LLM::Buffer constructor
- #inspect ⇒ String
-
#last(n = UNDEFINED) ⇒ LLM::Message, ...
Returns the last message(s) in the buffer.
-
#pop ⇒ void
Pop the last element from the tail of the buffer.
-
#reject! {|LLM::Message| ... } ⇒ LLM::Buffer
(also: #delete_if)
Removes messages matching the block in-place.
-
#replace(messages) ⇒ LLM::Buffer
Replace the tracked messages.
-
#reverse ⇒ Array
Returns a reversed copy of the internal array.
-
#rindex {|LLM::Message| ... } ⇒ Integer?
Returns the index of the last message matching the given block.
-
#select! {|LLM::Message| ... } ⇒ LLM::Buffer
Keeps messages matching the block in-place.
-
#shift ⇒ LLM::Message?
Removes and returns the first message.
-
#size ⇒ Integer
Returns the number of messages in the buffer.
-
#slice! ⇒ void
Slice a portion of the internal buffer in-place.
-
#take(n) ⇒ Array<LLM::Message>
Returns the first n elements without removing them.
- #to_json ⇒ String
Constructor Details
#initialize(provider) ⇒ LLM::Buffer
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.
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
177 178 179 |
# File 'lib/llm/buffer.rb', line 177 def [](index) @messages[index] end |
#clear ⇒ LLM::Buffer
Removes all messages.
124 125 126 127 |
# File 'lib/llm/buffer.rb', line 124 def clear @messages.clear self end |
#concat(ary) ⇒ Object
Append an array
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.
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.
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
210 211 212 |
# File 'lib/llm/buffer.rb', line 210 def empty? @messages.empty? end |
#find ⇒ LLM::Message?
Find a message (in descending order)
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
91 92 93 |
# File 'lib/llm/buffer.rb', line 91 def first(n = UNDEFINED) n.equal?(UNDEFINED) ? @messages.first : @messages.first(n) end |
#inspect ⇒ 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
82 83 84 |
# File 'lib/llm/buffer.rb', line 82 def last(n = UNDEFINED) n.equal?(UNDEFINED) ? @messages.last : @messages.last(n) end |
#pop ⇒ void
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.
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
45 46 47 48 |
# File 'lib/llm/buffer.rb', line 45 def replace() @messages.replace() self end |
#reverse ⇒ Array
Returns a reversed copy of the internal 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.
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.
109 110 111 112 |
# File 'lib/llm/buffer.rb', line 109 def select!(&) @messages.select!(&) self end |
#shift ⇒ LLM::Message?
Removes and returns the first message.
117 118 119 |
# File 'lib/llm/buffer.rb', line 117 def shift @messages.shift end |
#size ⇒ 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.
143 144 145 |
# File 'lib/llm/buffer.rb', line 143 def take(n) @messages.take(n) end |