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.
-
#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.
68 69 70 71 |
# File 'lib/llm/buffer.rb', line 68 def <<(item) @messages << item self end |
#[](index) ⇒ LLM::Message?
Returns a message, or nil
79 80 81 |
# File 'lib/llm/buffer.rb', line 79 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
106 107 108 |
# File 'lib/llm/buffer.rb', line 106 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
91 92 93 94 |
# File 'lib/llm/buffer.rb', line 91 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
60 61 62 |
# File 'lib/llm/buffer.rb', line 60 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 |
#size ⇒ Integer
Returns the number of messages in the buffer
99 100 101 |
# File 'lib/llm/buffer.rb', line 99 def size @messages.size end |