Class: LLM::Buffer
Overview
LLM::Buffer provides an Enumerable object that tracks messages in a conversation thread.
Instance Method Summary collapse
- #<<(item) ⇒ void (also: #push)
-
#[](index) ⇒ LLM::Message?
Returns a message, or nil.
-
#concat(ary) ⇒ Object
Append an array.
- #each {|LLM::Message| ... } ⇒ void
-
#empty? ⇒ Boolean
Returns true when the buffer is empty.
-
#find ⇒ LLM::Message?
Find a message (in descending order).
- #initialize(provider) ⇒ LLM::Buffer constructor
- #inspect ⇒ String
-
#last(n = nil) ⇒ LLM::Message, ...
Returns the last message(s) in the buffer.
-
#replace(messages) ⇒ LLM::Buffer
Replace the tracked messages.
-
#rindex {|LLM::Message| ... } ⇒ Integer?
Returns the index of the last message matching the given block.
-
#size ⇒ Integer
Returns the number of messages in the buffer.
- #to_json ⇒ String
Constructor Details
#initialize(provider) ⇒ LLM::Buffer
13 14 15 16 |
# File 'lib/llm/buffer.rb', line 13 def initialize(provider) @provider = provider @messages = [] end |
Instance Method Details
#<<(item) ⇒ void Also known as: push
This method returns an undefined value.
76 77 78 79 |
# File 'lib/llm/buffer.rb', line 76 def <<(item) @messages << item self end |
#[](index) ⇒ LLM::Message?
Returns a message, or nil
87 88 89 |
# File 'lib/llm/buffer.rb', line 87 def [](index) @messages[index] end |
#concat(ary) ⇒ Object
Append an array
22 23 24 |
# File 'lib/llm/buffer.rb', line 22 def concat(ary) @messages.concat(ary) end |
#each {|LLM::Message| ... } ⇒ void
This method returns an undefined value.
40 41 42 43 44 45 46 |
# File 'lib/llm/buffer.rb', line 40 def each(...) if block_given? @messages.each { yield(_1) } else enum_for(:each, ...) end end |
#empty? ⇒ Boolean
Returns true when the buffer is empty
114 115 116 |
# File 'lib/llm/buffer.rb', line 114 def empty? @messages.empty? end |
#find ⇒ LLM::Message?
Find a message (in descending order)
51 52 53 |
# File 'lib/llm/buffer.rb', line 51 def find(...) reverse_each.find(...) end |
#inspect ⇒ String
99 100 101 102 |
# File 'lib/llm/buffer.rb', line 99 def inspect "#<#{self.class.name}:0x#{object_id.to_s(16)} " \ "message_count=#{@messages.size}>" end |
#last(n = nil) ⇒ LLM::Message, ...
Returns the last message(s) in the buffer
68 69 70 |
# File 'lib/llm/buffer.rb', line 68 def last(n = nil) n.nil? ? @messages.last : @messages.last(n) end |
#replace(messages) ⇒ LLM::Buffer
Replace the tracked messages
31 32 33 34 |
# File 'lib/llm/buffer.rb', line 31 def replace() @messages.replace() self end |
#rindex {|LLM::Message| ... } ⇒ Integer?
Returns the index of the last message matching the given block.
59 60 61 |
# File 'lib/llm/buffer.rb', line 59 def rindex(...) @messages.rindex(...) end |
#size ⇒ Integer
Returns the number of messages in the buffer
107 108 109 |
# File 'lib/llm/buffer.rb', line 107 def size @messages.size end |