Method: Intelligence::Google::ChatResponseMethods#stream_result_chunk_attributes

Defined in:
lib/intelligence/adapters/google/chat_response_methods.rb

#stream_result_chunk_attributes(context, chunk) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/intelligence/adapters/google/chat_response_methods.rb', line 92

def stream_result_chunk_attributes( context, chunk )

  context ||= {}
  buffer = context[ :buffer ] || ''
  metrics = context[ :metrics ] || {
    input_tokens: 0,
    output_tokens:  0
  }
  choices = context[ :choices ] || Array.new( 1 , { message: {} } )

  choices.each do | choice |
    choice[ :message ][ :contents ] = choice[ :message ][ :contents ]&.map do | content |
      case content[ :type ] 
        when :text 
          content[ :text ] = ''
        else 
          content.clear 
      end
      content
    end
  end

  buffer += chunk
  while ( eol_index = buffer.index( "\n" ) )

    line = buffer.slice!( 0..eol_index )        
    line = line.strip 
    next if line.empty? || !line.start_with?( 'data:' )
    line = line[ 6..-1 ]

    data = JSON.parse( line, symbolize_names: true )
    if data.is_a?( Hash )
      
      data[ :candidates ]&.each do | data_candidate |

        data_candidate_index = data_candidate[ :index ] || 0
        data_candidate_content = data_candidate[ :content ]
        data_candidate_finish_reason = data_candidate[ :finishReason ]
        choices.fill( { message: { role: 'assistant' } }, choices.size, data_candidate_index + 1 ) \
          if choices.size <= data_candidate_index 
        contents = choices[ data_candidate_index ][ :message ][ :contents ] || []
        last_content = contents&.last 
        
        if data_candidate_content&.include?( :parts ) 
          data_candidate_content_parts = data_candidate_content[ :parts ]
          data_candidate_content_parts&.each do | data_candidate_content_part |
            if data_candidate_content_part.key?( :text )
              if last_content.nil? || last_content[ :type ] != :text
                contents.push( { type: :text, text: data_candidate_content_part[ :text ] } )
              else 
                last_content[ :text ] = 
                  ( last_content[ :text ] || '' ) + data_candidate_content_part[ :text ]
              end
            end
          end
        end
        choices[ data_candidate_index ][ :message ][ :contents ] = contents
        choices[ data_candidate_index ][ :end_reason ] = 
          translate_finish_reason( data_candidate_finish_reason )
      end
  
      if usage = data[ :usageMetadata ]
        metrics[ :input_tokens ] = usage[ :promptTokenCount ]
        metrics[ :output_tokens ] = usage[ :candidatesTokenCount ]
      end 

    end

  end

  context[ :buffer ] = buffer 
  context[ :metrics ] = metrics
  context[ :choices ] = choices

  [ context, choices.empty? ? nil : { choices: choices.dup } ]

end