Module: Intelligence::Generic::ChatRequestMethods

Defined in:
lib/intelligence/adapters/generic/chat_request_methods.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

CHAT_COMPLETIONS_PATH =
'chat/completions'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



17
18
19
# File 'lib/intelligence/adapters/generic/chat_request_methods.rb', line 17

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

Instance Method Details

#chat_request_body(conversation, options = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
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
114
115
116
117
118
119
120
# File 'lib/intelligence/adapters/generic/chat_request_methods.rb', line 45

def chat_request_body( conversation, options = nil )
  tools = options.delete( :tools ) || []

  options = merge_options( @options, build_options( options ) )
  
  result = options[ :chat_options ]
  result[ :messages ] = []

  system_message = chat_request_system_message_attributes( conversation[ :system_message ] )
  result[ :messages ] << system_message if system_message

  conversation[ :messages ]&.each do | message |
    return nil unless message[ :contents ]&.any?

    result_message = { role: message[ :role ] }
    result_message_content = []

    message_contents = message[ :contents ]

    # tool calls in the open ai api are not content
    tool_calls, message_contents = message_contents.partition do | content | 
      content[ :type ] == :tool_call 
    end 

    # tool results in the open ai api are not content
    tool_results, message_contents = message_contents.partition do | content |
      content[ :type ] == :tool_result 
    end 

    # many vendor api's, especially when hosting text only models, will only accept a single 
    # text content item; if the content is only text this will coalece multiple text content 
    # items into a single content item
    unless message_contents.any? { | c | c[ :type ] != :text }
      result_message_content = message_contents.map { | c | c[ :text ] || '' }.join( "\n" )
    else  
      message_contents&.each do | content |
        result_message_content << chat_request_message_content_attributes( content )
      end
    end

    if tool_calls.any?
      result_message[ :tool_calls ] = tool_calls.map { | tool_call |
        {
          id: tool_call[ :tool_call_id ],
          type: 'function',
          function: {
            name: tool_call[ :tool_name ],
            arguments: JSON.generate( tool_call[ :tool_parameters ] || {} )
          }
        }
      }
    end 

    result_message[ :content ] = result_message_content 
    unless result_message_content.empty? && tool_calls.empty? 
     result[ :messages ] << result_message 
    end 

    if tool_results.any? 
      result[ :messages ].concat( tool_results.map { | tool_result |
        {
          role: :tool, 
          tool_call_id: tool_result[ :tool_call_id ],
          content: tool_result[ :tool_result ]
        }
      } )
    end 
  end 

  tools_attributes = chat_request_tools_attributes( 
    ( result[ :tools ] || [] ).concat( tools ) 
  )
  result[ :tools ] = tools_attributes if tools_attributes && tools_attributes.length > 0
  
  JSON.generate( result )
end

#chat_request_headers(options = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/intelligence/adapters/generic/chat_request_methods.rb', line 35

def chat_request_headers( options = nil )
  options = merge_options( @options, build_options( options ) )
  result = { 'Content-Type': 'application/json' }

  key = options[ :key ]
  result[ 'Authorization' ] = "Bearer #{key}" if key

  result 
end

#chat_request_message_content_attributes(content) ⇒ Object



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
169
170
171
172
173
# File 'lib/intelligence/adapters/generic/chat_request_methods.rb', line 122

def chat_request_message_content_attributes( content )
  case content[ :type ]
  when :text
    { 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'
        {
          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'
        {
          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

#chat_request_system_message_attributes(system_message) ⇒ Object



175
176
177
178
179
180
181
182
183
184
# File 'lib/intelligence/adapters/generic/chat_request_methods.rb', line 175

def chat_request_system_message_attributes( system_message )
  return nil if system_message.nil?

  result = ''
  system_message[ :contents ].each do | content |
    result += content[ :text ] if content[ :type ] == :text 
  end

  result.empty? ? nil : { role: 'system', content: result } if system_message  
end

#chat_request_tools_attributes(tools) ⇒ Object



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

def chat_request_tools_attributes( tools ) 
  properties_array_to_object = lambda do | properties |
    return nil unless properties&.any?  
    object = {}
    required = []
    properties.each do | property | 
      name = property.delete( :name ) 
      required << name if property.delete( :required )
      if property[ :properties ]&.any?  
        property_properties, property_required = 
          properties_array_to_object.call( property[ :properties ] ) 
        property[ :properties ] = property_properties 
        property[ :required ] = property_required if property_required.any?
      end 
      object[ name ] = property 
    end 
    [ object, required.compact  ]
  end

  tools&.map do | tool |
    function = { 
      type: 'function',
      function: {
        name: tool[ :name ],
        description: tool[ :description ],
      }
    }

    if tool[ :properties ]&.any? 
      properties_object, properties_required = 
        properties_array_to_object.call( tool[ :properties ] ) 
      function[ :function ][ :parameters ] = {
        type: 'object',
        properties: properties_object 
      }
      function[ :function ][ :parameters ][ :required ] = properties_required \
        if properties_required.any?
    else
      function[ :function ][ :parameters ] = {}
    end
    function 
  end
end

#chat_request_uri(options = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/intelligence/adapters/generic/chat_request_methods.rb', line 21

def chat_request_uri( options = nil )
  options = merge_options( @options, build_options( options ) )
  self.class.chat_request_uri || begin 
    base_uri = options[ :base_uri ]
    if base_uri
      # because URI join is dumb
      base_uri = ( base_uri.end_with?( '/' ) ? base_uri : base_uri + '/' ) 
      URI.join( base_uri, CHAT_COMPLETIONS_PATH )
    else
      nil 
    end
  end
end