Class: LLM::Buffer
Overview
LLM::Buffer provides an Enumerable object that tracks messages in a conversation thread.
Instance Method Summary collapse
- #<<(item) (also: #push)
-
#[](index) ⇒ LLM::Message?
Returns a message, or nil.
-
#concat(ary) ⇒ Object
Append an array.
- #each {|LLM::Message| ... }
-
#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.
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) Also known as: push
This method returns an undefined value.
58 59 60 61 |
# File 'lib/llm/buffer.rb', line 58 def <<(item) @messages << item self end |
#[](index) ⇒ LLM::Message?
Returns a message, or nil
69 70 71 |
# File 'lib/llm/buffer.rb', line 69 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| ... }
This method returns an undefined value.
30 31 32 33 34 35 36 |
# File 'lib/llm/buffer.rb', line 30 def each(...) if block_given? @messages.each { yield(_1) } else enum_for(:each, ...) end end |
#empty? ⇒ Boolean
Returns true when the buffer is empty
83 84 85 |
# File 'lib/llm/buffer.rb', line 83 def empty? @messages.empty? end |
#find ⇒ LLM::Message?
Find a message (in descending order)
41 42 43 |
# File 'lib/llm/buffer.rb', line 41 def find(...) reverse_each.find(...) end |
#inspect ⇒ String
75 76 77 78 |
# File 'lib/llm/buffer.rb', line 75 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
50 51 52 |
# File 'lib/llm/buffer.rb', line 50 def last(n = nil) n.nil? ? @messages.last : @messages.last(n) end |