Class: Ask::Chunk
- Inherits:
-
Object
- Object
- Ask::Chunk
- Defined in:
- lib/ask/stream.rb
Overview
A single chunk of streaming output from an LLM provider. Yielded by Stream during a streaming response.
Instance Attribute Summary collapse
-
#content ⇒ String?
readonly
Text content of this chunk.
-
#finish_reason ⇒ String?
readonly
Reason the stream finished (“stop”, “length”, “tool_calls”).
-
#raw ⇒ Hash?
readonly
Raw response data from the provider.
-
#tool_calls ⇒ Array<Hash>?
readonly
Tool call invocations in this chunk.
-
#usage ⇒ Hash?
readonly
Token usage metadata.
Instance Method Summary collapse
-
#finished? ⇒ Boolean
True if this is the final chunk in a stream.
-
#initialize(content: nil, tool_calls: nil, finish_reason: nil, usage: nil, raw: nil) ⇒ Chunk
constructor
A new instance of Chunk.
-
#inspect ⇒ String
Human-readable representation.
-
#to_s ⇒ String
Text content as a plain string.
-
#tool_call? ⇒ Boolean
True if this chunk contains tool calls.
Constructor Details
#initialize(content: nil, tool_calls: nil, finish_reason: nil, usage: nil, raw: nil) ⇒ Chunk
Returns a new instance of Chunk.
22 23 24 25 26 27 28 |
# File 'lib/ask/stream.rb', line 22 def initialize(content: nil, tool_calls: nil, finish_reason: nil, usage: nil, raw: nil) @content = content @tool_calls = tool_calls @finish_reason = finish_reason @usage = usage @raw = raw end |
Instance Attribute Details
#content ⇒ String? (readonly)
Returns text content of this chunk.
8 9 10 |
# File 'lib/ask/stream.rb', line 8 def content @content end |
#finish_reason ⇒ String? (readonly)
Returns reason the stream finished (“stop”, “length”, “tool_calls”).
14 15 16 |
# File 'lib/ask/stream.rb', line 14 def finish_reason @finish_reason end |
#raw ⇒ Hash? (readonly)
Returns raw response data from the provider.
20 21 22 |
# File 'lib/ask/stream.rb', line 20 def raw @raw end |
#tool_calls ⇒ Array<Hash>? (readonly)
Returns tool call invocations in this chunk.
11 12 13 |
# File 'lib/ask/stream.rb', line 11 def tool_calls @tool_calls end |
#usage ⇒ Hash? (readonly)
Returns token usage metadata.
17 18 19 |
# File 'lib/ask/stream.rb', line 17 def usage @usage end |
Instance Method Details
#finished? ⇒ Boolean
Returns true if this is the final chunk in a stream.
31 |
# File 'lib/ask/stream.rb', line 31 def finished? = !@finish_reason.nil? |
#inspect ⇒ String
Returns human-readable representation.
42 43 44 |
# File 'lib/ask/stream.rb', line 42 def inspect "#<Ask::Chunk content=#{@content.inspect} finish_reason=#{@finish_reason.inspect}>" end |
#to_s ⇒ String
Returns text content as a plain string.
37 38 39 |
# File 'lib/ask/stream.rb', line 37 def to_s @content.to_s end |
#tool_call? ⇒ Boolean
Returns true if this chunk contains tool calls.
34 |
# File 'lib/ask/stream.rb', line 34 def tool_call? = @tool_calls&.any? == true |