Class: Ask::Stream
Overview
Streaming response from an LLM provider. Wraps an enumerable of Chunks and provides accumulation into a single string or message.
stream = Ask::Stream.new { |chunk| puts chunk.content }
stream.each { |chunk| ... }
stream.accumulated_text # => "full response text"
Instance Method Summary collapse
-
#accumulated_text ⇒ String
(also: #to_s)
Full accumulated text from all chunks.
-
#accumulated_usage ⇒ Hash
Accumulated usage across all chunks, merged by key.
-
#add(chunk) ⇒ Chunk
Add a chunk to the stream.
-
#chunks ⇒ Array<Chunk>
A copy of all accumulated chunks.
-
#each {|Chunk| ... } ⇒ Enumerator
Iterate over all accumulated chunks.
-
#finish! ⇒ Object
Mark the stream as finished.
-
#finished? ⇒ Boolean
True if the stream has been finished.
-
#initialize(&chunk_handler) ⇒ Stream
constructor
A new instance of Stream.
-
#inspect ⇒ String
Human-readable representation.
-
#length ⇒ Integer
Number of chunks accumulated.
Constructor Details
#initialize(&chunk_handler) ⇒ Stream
Returns a new instance of Stream.
58 59 60 61 62 63 |
# File 'lib/ask/stream.rb', line 58 def initialize(&chunk_handler) @chunks = [] @chunk_handler = chunk_handler @accumulated = +"" @finished = false end |
Instance Method Details
#accumulated_text ⇒ String Also known as: to_s
Returns full accumulated text from all chunks.
96 97 98 |
# File 'lib/ask/stream.rb', line 96 def accumulated_text @accumulated.dup end |
#accumulated_usage ⇒ Hash
Returns accumulated usage across all chunks, merged by key.
102 103 104 105 106 107 108 109 |
# File 'lib/ask/stream.rb', line 102 def accumulated_usage @chunks .map(&:usage) .compact .reduce({}) do |acc, u| acc.merge(u) { |_key, old, new| old + new } end end |
#add(chunk) ⇒ Chunk
Add a chunk to the stream.
78 79 80 81 82 83 84 85 |
# File 'lib/ask/stream.rb', line 78 def add(chunk) chunk = Chunk.new(content: chunk) if chunk.is_a?(String) @chunks << chunk @accumulated << chunk.content.to_s if chunk.content @chunk_handler&.call(chunk) chunk end |
#chunks ⇒ Array<Chunk>
Returns a copy of all accumulated chunks.
112 |
# File 'lib/ask/stream.rb', line 112 def chunks = @chunks.dup |
#each {|Chunk| ... } ⇒ Enumerator
Iterate over all accumulated chunks.
68 69 70 71 72 73 |
# File 'lib/ask/stream.rb', line 68 def each(&block) return enum_for(:each) unless block @chunks.each(&block) self end |
#finish! ⇒ Object
Mark the stream as finished.
88 89 90 |
# File 'lib/ask/stream.rb', line 88 def finish! @finished = true end |
#finished? ⇒ Boolean
Returns true if the stream has been finished.
93 |
# File 'lib/ask/stream.rb', line 93 def finished? = @finished |
#inspect ⇒ String
Returns human-readable representation.
118 119 120 121 |
# File 'lib/ask/stream.rb', line 118 def inspect finished = @finished ? "finished" : "streaming" "#<Ask::Stream #{finished} chunks=#{@chunks.length}>" end |
#length ⇒ Integer
Returns number of chunks accumulated.
115 |
# File 'lib/ask/stream.rb', line 115 def length = @chunks.length |