Module: Intelligence::Generic::ChatMethods

Included in:
Adapter
Defined in:
lib/intelligence/adapters/generic/chat_methods.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



15
16
17
# File 'lib/intelligence/adapters/generic/chat_methods.rb', line 15

def self.included( base )
  base.extend( ClassMethods )
end

Instance Method Details

#chat_request_body(conversation, options = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/intelligence/adapters/generic/chat_methods.rb', line 39

def chat_request_body( conversation, options = nil )
  options = options ? self.class.configure( options ) : {}
  options = @options.merge( options )
  
  result = options[ :chat_options ]
  result[ :messages ] = []

  system_message = system_message_to_s( conversation[ :system_message ] )
  result[ :messages ] << { role: 'system', content: system_message } if system_message

  conversation[ :messages ]&.each do | message |
    result[ :messages ] << chat_request_message_attributes( message )
  end

  JSON.generate( result )
end

#chat_request_headers(options = nil) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/intelligence/adapters/generic/chat_methods.rb', line 23

def chat_request_headers( options = nil )
  options = options ? self.class.configure( options ) : {}
  options = @options.merge( options )
  result = {}

  key = options[ :key ]

  raise ArgumentError.new( "An API key is required to build a chat request." ) \
    if key.nil?

  result[ 'Content-Type' ] = 'application/json'
  result[ 'Authorization' ] = "Bearer #{key}"

  result 
end

#chat_request_message_attributes(message) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/intelligence/adapters/generic/chat_methods.rb', line 56

def chat_request_message_attributes( message )
  result_message = { role: message[ :role ] }
  result_message_content = []
  message[ :contents ]&.each do | content |
    case content[ :type ]
    when :text
      result_message_content << { type: 'text', text: content[ :text ] }
    when :binary 
      content_type = content[ :content_type ]
      bytes = content[ :bytes ]
      if content_type && bytes
        mime_type = MIME::Types[ content_type ].first
        if mime_type&.media_type == 'image'
          result_message_content << {
            type: 'image_url',
            image_url: {
              url: "data:#{content_type};base64,#{Base64.strict_encode64( bytes )}".freeze
            }
          }
        else
          raise UnsupportedContentError.new( 
            :generic, 
            'only support content of type image/*' 
          ) 
        end
      else 
        raise UnsupportedContentError.new(
          :generic, 
          'requires binary content to include content type and ( packed ) bytes'  
        )
      end
    when :file 
      content_type = content[ :content_type ]
      uri = content[ :uri ]
      if content_type && uri
        mime_type = MIME::Types[ content_type ].first
        if mime_type&.media_type == 'image'
          result_message_content << {
            type: 'image_url',
            image_url: { url: uri }
          }
        else
          raise UnsupportedContentError.new( 
            :generic, 
            'only support content of type image/*' 
          ) 
        end
      else 
        raise UnsupportedContentError.new(
          :generic, 
          'requires binary content to include content type and ( packed ) bytes'  
        )
      end
    end
  end
  result_message[ :content ] = result_message_content
  result_message
end

#chat_request_uri(options) ⇒ Object



19
20
21
# File 'lib/intelligence/adapters/generic/chat_methods.rb', line 19

def chat_request_uri( options )
  self.class.chat_request_uri
end

#chat_result_attributes(response) ⇒ Object



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
# File 'lib/intelligence/adapters/generic/chat_methods.rb', line 115

def chat_result_attributes( response )

  return nil unless response.success?
  response_json = JSON.parse( response.body, symbolize_names: true ) rescue nil
  return nil if response_json.nil? || response_json[ :choices ].nil?
  
  result = {}
  result[ :choices ] = []

  ( response_json[ :choices ] || [] ).each do | json_choice |
    json_message = json_choice[ :message ]
    result[ :choices ].push(
      { 
        end_reason: translate_end_result( json_choice[ :finish_reason ] ), 
        message: { 
          role: json_message[ :role ],
          contents: [ { type: 'text', text: json_message[ :content ] } ]
        } 
      }
    )
  end

  metrics_json = response_json[ :usage ]
  unless metrics_json.nil?

    metrics = {}
    metrics[ :input_tokens ] = metrics_json[ :prompt_tokens ] 
    metrics[ :output_tokens ] = metrics_json[ :completion_tokens ]
    metrics = metrics.compact

    result[ :metrics ] = metrics unless metrics.empty?

  end

  result

end

#chat_result_error_attributes(response) ⇒ Object Also known as: stream_result_error_attributes



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/intelligence/adapters/generic/chat_methods.rb', line 153

def chat_result_error_attributes( response )
  
  error_type, error_description = translate_error_response_status( response.status )
  result = {
    error_type: error_type.to_s,
    error_description: error_description
  }

  parsed_body = JSON.parse( response.body, symbolize_names: true ) rescue nil 
  if parsed_body && parsed_body.respond_to?( :include? ) && parsed_body.include?( :error )
    result = {
      error_type: error_type.to_s,
      error: parsed_body[ :error ][ :code ] || error_type.to_s,
      error_description: parsed_body[ :error ][ :message ] || error_description
    }
  end

  result

end

#stream_result_attributes(context) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/intelligence/adapters/generic/chat_methods.rb', line 275

def stream_result_attributes( context )

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

  choices = choices.map do | choice |
    { end_reason: choice[ :end_reason ] }
  end

  { choices: choices, metrics: context[ :metrics ] }

end

#stream_result_chunk_attributes(context, chunk) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/intelligence/adapters/generic/chat_methods.rb', line 174

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 ] = ''
        when :tool_call 
          content[ :tool_parameters ] = ''
        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 ]

    next if line.end_with?( '[DONE]' )
    data = JSON.parse( line )
    if data.is_a?( Hash )
      
      data[ 'choices' ]&.each do | data_choice |

        data_choice_index = data_choice[ 'index' ]
        data_choice_delta = data_choice[ 'delta' ]
        data_choice_finish_reason = data_choice[ 'finish_reason' ]
        
        choices.fill( { message: {} }, choices.size, data_choice_index + 1 ) \
          if choices.size <= data_choice_index 
        contents = choices[ data_choice_index ][ :message ][ :contents ] || []
        last_content = contents&.last 
        
        if data_choice_delta.include?( 'content' ) 
          data_choice_content = data_choice_delta[ 'content' ] || ''
          if last_content.nil? || last_content[ :type ] == :tool_call
            contents.push( { type: :text, text: data_choice_content } )
          elsif last_content[ :type ] == :text || last_content[ :type ].nil?
            last_content[ :type ] = :text 
            last_content[ :text ] = ( last_content[ :text ] || '' ) + data_choice_content
          end
        elsif data_choice_delta.include?( 'function_call' )
          data_choice_tool_call = data_choice_delta[ 'function_call' ] 
          data_choice_tool_name = data_choice_tool_call[ 'name' ]
          data_choice_tool_parameters = data_choice_tool_call[ 'arguments' ]
          if last_content.nil? || last_content[ :type ] == :text
            contents.push( { 
              type: :tool_call, 
              tool_name: data_choice_tool_name,
              tool_parameters: data_choice_tool_parameters
            } )
          elsif last_content[ :type ].nil?
            last_content[ :type ] = :tool_call 
            last_content[ :tool_name ] = data_choice_tool_name \
              if data_choice_tool_name.present?
            last_content[ :tool_parameters ] = tool_parameters
          elsif last_content[ :type ] == :tool_call 
            last_content[ :tool_parameters ] = 
              ( last_content[ :tool_parameters ] || '' ) + data_choice_tool_parameters
          end
        end
        choices[ data_choice_index ][ :message ][ :contents ] = contents
        choices[ data_choice_index ][ :end_reason ] ||= 
          translate_end_result( data_choice_finish_reason )
      end
  
      if usage = data[ 'usage' ]
        # note: A number of providers will resend the input tokens as part of their usage 
        #       payload.
        metrics[ :input_tokens ] = usage[ 'prompt_tokens' ] \
          if usage.include?( 'prompt_tokens' )
        metrics[ :output_tokens ] += usage[ 'completion_tokens' ] \
           if usage.include?( 'completion_tokens' )
      end 

    end

  end

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

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

end

#translate_end_result(end_result) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/intelligence/adapters/generic/chat_methods.rb', line 290

def translate_end_result( end_result )
  case end_result
    when 'stop'
      :ended
    when 'length'
      :token_limit_exceeded
    when 'function_call'
      :tool_called
    when 'content_filter'
      :filtered
    else
      nil
  end
end

#translate_error_response_status(status) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/intelligence/adapters/generic/chat_methods.rb', line 305

def translate_error_response_status( status )
  case status
    when 400
      [ :invalid_request_error, 
        "There was an issue with the format or content of your request." ]
    when 401
      [ :authentication_error, 
        "There's an issue with your API key." ]
    when 403
      [ :permission_error, 
        "Your API key does not have permission to use the specified resource." ]
    when 404
      [ :not_found_error, 
        "The requested resource was not found." ]
    when 413
      [ :request_too_large, 
        "Request exceeds the maximum allowed number of bytes." ]
    when 422
      [ :invalid_request_error, 
        "There was an issue with the format or content of your request." ]
    when 429
      [ :rate_limit_error, 
        "Your account has hit a rate limit." ]
    when 500, 502, 503
      [ :api_error, 
        "An unexpected error has occurred internal to the providers systems." ]
    when 529
      [ :overloaded_error, 
        "The providers server is temporarily overloaded." ]
    else
      [ :unknown_error, "
        An unknown error occurred." ]
    end
end