Class: Raif::StreamingResponses::Google

Inherits:
Object
  • Object
show all
Defined in:
app/models/raif/streaming_responses/google.rb

Instance Method Summary collapse

Constructor Details

#initializeGoogle

Returns a new instance of Google.



5
6
7
8
# File 'app/models/raif/streaming_responses/google.rb', line 5

def initialize
  @response_json = { "candidates" => [{ "content" => { "parts" => [] } }], "usageMetadata" => {} }
  @finish_reason = nil
end

Instance Method Details

#current_response_jsonObject



38
39
40
# File 'app/models/raif/streaming_responses/google.rb', line 38

def current_response_json
  @response_json
end

#process_streaming_event(event_type, event) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/raif/streaming_responses/google.rb', line 10

def process_streaming_event(event_type, event)
  delta = nil

  # Google streams complete candidate objects, so we need to extract the new text
  candidates = event["candidates"]
  if candidates.present?
    candidate = candidates[0]

    # Check for finish reason. Mirror it into the accumulated response JSON so
    # update_model_completion (which digs candidates/0/finishReason) sees it -
    # otherwise streamed completions would always persist a nil finish reason.
    if candidate["finishReason"].present?
      @finish_reason = candidate["finishReason"]
      @response_json["candidates"][0]["finishReason"] = @finish_reason
    end

    # Process content parts
    parts = candidate.dig("content", "parts")
    delta = process_content_parts(parts) if parts.present?
  end

  # Update usage metadata
   = event["usageMetadata"]
  @response_json["usageMetadata"] =  if .present?

  [delta, @finish_reason]
end