Class: Geminize::Models::StreamResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/geminize/models/stream_response.rb

Overview

Represents a streaming response chunk from the Gemini API

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chunk_data) ⇒ StreamResponse

Initialize a new streaming response chunk

Parameters:

  • chunk_data (Hash)

    The raw API response chunk



21
22
23
24
# File 'lib/geminize/models/stream_response.rb', line 21

def initialize(chunk_data)
  @raw_chunk = chunk_data
  parse_chunk
end

Instance Attribute Details

#finish_reasonString? (readonly)

Returns The finish reason if this is the last chunk.

Returns:

  • (String, nil)

    The finish reason if this is the last chunk



14
15
16
# File 'lib/geminize/models/stream_response.rb', line 14

def finish_reason
  @finish_reason
end

#raw_chunkHash (readonly)

Returns The raw API response data for this chunk.

Returns:

  • (Hash)

    The raw API response data for this chunk



8
9
10
# File 'lib/geminize/models/stream_response.rb', line 8

def raw_chunk
  @raw_chunk
end

#textString? (readonly)

Returns The text content in this chunk.

Returns:

  • (String, nil)

    The text content in this chunk



11
12
13
# File 'lib/geminize/models/stream_response.rb', line 11

def text
  @text
end

#usage_metricsHash? (readonly)

Returns Token usage metrics (only available in final chunk).

Returns:

  • (Hash, nil)

    Token usage metrics (only available in final chunk)



17
18
19
# File 'lib/geminize/models/stream_response.rb', line 17

def usage_metrics
  @usage_metrics
end

Class Method Details

.from_hash(chunk_data) ⇒ StreamResponse

Create a StreamResponse object from a raw API response chunk

Parameters:

  • chunk_data (Hash)

    The raw API response chunk

Returns:



62
63
64
# File 'lib/geminize/models/stream_response.rb', line 62

def self.from_hash(chunk_data)
  new(chunk_data)
end

Instance Method Details

#completion_tokensInteger?

Get the completion token count if available

Returns:

  • (Integer, nil)

    Completion token count or nil if not available



47
48
49
50
# File 'lib/geminize/models/stream_response.rb', line 47

def completion_tokens
  return nil unless @usage_metrics
  @usage_metrics["candidatesTokenCount"]
end

#final_chunk?Boolean

Check if this is the final chunk in the stream

Returns:

  • (Boolean)

    True if this is the final chunk



28
29
30
# File 'lib/geminize/models/stream_response.rb', line 28

def final_chunk?
  !@finish_reason.nil?
end

#has_usage_metrics?Boolean

Check if this chunk has usage metrics

Returns:

  • (Boolean)

    True if this chunk contains usage metrics



34
35
36
# File 'lib/geminize/models/stream_response.rb', line 34

def has_usage_metrics?
  !@usage_metrics.nil?
end

#prompt_tokensInteger?

Get the prompt token count if available

Returns:

  • (Integer, nil)

    Prompt token count or nil if not available



40
41
42
43
# File 'lib/geminize/models/stream_response.rb', line 40

def prompt_tokens
  return nil unless @usage_metrics
  @usage_metrics["promptTokenCount"]
end

#total_tokensInteger?

Get the total token count if available

Returns:

  • (Integer, nil)

    Total token count or nil if not available



54
55
56
57
# File 'lib/geminize/models/stream_response.rb', line 54

def total_tokens
  return nil unless @usage_metrics
  (prompt_tokens || 0) + (completion_tokens || 0)
end