Class: Ask::Chunk

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#contentString? (readonly)

Returns text content of this chunk.

Returns:

  • (String, nil)

    text content of this chunk



8
9
10
# File 'lib/ask/stream.rb', line 8

def content
  @content
end

#finish_reasonString? (readonly)

Returns reason the stream finished (“stop”, “length”, “tool_calls”).

Returns:

  • (String, nil)

    reason the stream finished (“stop”, “length”, “tool_calls”)



14
15
16
# File 'lib/ask/stream.rb', line 14

def finish_reason
  @finish_reason
end

#rawHash? (readonly)

Returns raw response data from the provider.

Returns:

  • (Hash, nil)

    raw response data from the provider



20
21
22
# File 'lib/ask/stream.rb', line 20

def raw
  @raw
end

#tool_callsArray<Hash>? (readonly)

Returns tool call invocations in this chunk.

Returns:

  • (Array<Hash>, nil)

    tool call invocations in this chunk



11
12
13
# File 'lib/ask/stream.rb', line 11

def tool_calls
  @tool_calls
end

#usageHash? (readonly)

Returns token usage metadata.

Returns:

  • (Hash, nil)

    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.

Returns:

  • (Boolean)

    true if this is the final chunk in a stream



31
# File 'lib/ask/stream.rb', line 31

def finished? = !@finish_reason.nil?

#inspectString

Returns human-readable representation.

Returns:

  • (String)

    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_sString

Returns text content as a plain string.

Returns:

  • (String)

    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.

Returns:

  • (Boolean)

    true if this chunk contains tool calls



34
# File 'lib/ask/stream.rb', line 34

def tool_call? = @tool_calls&.any? == true