Class: LLM::Buffer
Overview
LLM::Buffer provides an Enumerable object that yields each message in a conversation on-demand, and only sends a request to the LLM when a response is needed.
Instance Method Summary collapse
-
#drain ⇒ Array<LLM::Message>
Drains the buffer and returns all messages as an array.
-
#each {|LLM::Message| ... } ⇒ void
-
#unread ⇒ Array<LLM::Message>
Returns an array of unread messages.
-
#find ⇒ LLM::Message?
Find a message (in descending order).
-
#last ⇒ LLM::Message?
Returns the last message in the buffer.
-
#<<(item) ⇒ void
(also: #push)
-
#[](index) ⇒ LLM::Message?
Returns a message, or nil.
-
#inspect ⇒ String
-
#empty? ⇒ Boolean
Returns true when the buffer is empty.
-
#initialize(provider) ⇒ LLM::Buffer
constructor
Constructor Details
#initialize(provider) ⇒ LLM::Buffer
14 15 16 17 18 |
# File 'lib/llm/buffer.rb', line 14 def initialize(provider) @provider = provider @pending = [] @completed = [] end |
Instance Method Details
#drain ⇒ Array<LLM::Message>
This method is especially useful when using the streaming API.
Drains the buffer and returns all messages as an array
100 101 102 |
# File 'lib/llm/buffer.rb', line 100 def drain to_a end |
#each {|LLM::Message| ... } ⇒ void
This method returns an undefined value.
25 26 27 28 29 30 31 32 |
# File 'lib/llm/buffer.rb', line 25 def each(...) if block_given? empty! unless @pending.empty? @completed.each { yield(_1) } else enum_for(:each, ...) end end |
#unread ⇒ Array<LLM::Message>
Returns an array of unread messages
39 40 41 |
# File 'lib/llm/buffer.rb', line 39 def unread reject(&:read?) end |
#find ⇒ LLM::Message?
Find a message (in descending order)
46 47 48 |
# File 'lib/llm/buffer.rb', line 46 def find(...) reverse_each.find(...) end |
#last ⇒ LLM::Message?
Returns the last message in the buffer
53 54 55 |
# File 'lib/llm/buffer.rb', line 53 def last to_a[-1] end |
#<<(item) ⇒ void Also known as: push
This method returns an undefined value.
61 62 63 64 |
# File 'lib/llm/buffer.rb', line 61 def <<(item) @pending << item self end |
#[](index) ⇒ LLM::Message?
Returns a message, or nil
72 73 74 |
# File 'lib/llm/buffer.rb', line 72 def [](index) @completed[index.to_i] || to_a[index.to_i] end |
#inspect ⇒ String
78 79 80 81 |
# File 'lib/llm/buffer.rb', line 78 def inspect "#<#{self.class.name}:0x#{object_id.to_s(16)} " \ "completed_count=#{@completed.size} pending_count=#{@pending.size}>" end |
#empty? ⇒ Boolean
Returns true when the buffer is empty
86 87 88 |
# File 'lib/llm/buffer.rb', line 86 def empty? @pending.empty? and @completed.empty? end |