Class: LittleGhost::Providers::Gemini::Normalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/little_ghost/providers/gemini.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(model:) ⇒ Normalizer

Returns a new instance of Normalizer.



115
116
117
118
119
120
121
122
123
# File 'lib/little_ghost/providers/gemini.rb', line 115

def initialize(model:)
  @model = model
  @text = +""
  @reasoning = +""
  @tools = []
  @usage = Usage.new
  @started = false
  @terminal = false
end

Instance Method Details

#consume(event) ⇒ Object

Raises:



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/little_ghost/providers/gemini.rb', line 125

def consume(event)
  raise ProviderError, "Google request failed: #{event.dig("error", "message")}" if event["error"]

  events = []
  unless @started
    @started = true
    events << StreamEvent.build(:message_start, id: nil, model: event["modelVersion"] || @model)
  end
  candidate = event.fetch("candidates", []).first
  if candidate
    candidate.dig("content", "parts")&.each { |part| events.concat(part_events(part)) }
    if candidate["finishReason"]
      @stop_reason = normalize_stop(candidate["finishReason"])
      @terminal = true
    end
  end
  if event["usageMetadata"]
    @usage = usage(event.fetch("usageMetadata"))
    events << StreamEvent.build(:usage, usage: @usage)
  end
  events
end

#finishObject

Raises:



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/little_ghost/providers/gemini.rb', line 148

def finish
  raise ProtocolError, "Google stream ended before a finish reason" unless @terminal

  blocks = []
  blocks << Content::Reasoning.new(text: @reasoning) unless @reasoning.empty?
  blocks << Content::Text.new(text: @text) unless @text.empty?
  blocks.concat(@tools)
  response = ModelResponse.new(message: Message.new(role: :assistant, content: blocks),
    stop_reason: @stop_reason, usage: @usage, metadata: {model: @model})
  [StreamEvent.build(:message_stop, response:)]
end