Module: Intelligence::OpenAi::ChatMethods
- Included in:
- Adapter
- Defined in:
- lib/intelligence/adapters/open_ai/chat_methods.rb
Constant Summary collapse
- CHAT_REQUEST_URI =
"https://api.openai.com/v1/chat/completions"- SUPPORTED_CONTENT_TYPES =
[ 'image/jpeg', 'image/png' ]
Instance Method Summary collapse
- #chat_request_body(conversation, options = {}) ⇒ Object
- #chat_request_headers(options = {}) ⇒ Object
- #chat_request_uri(options) ⇒ Object
- #chat_result_attributes(response) ⇒ Object
- #chat_result_error_attributes(response) ⇒ Object (also: #stream_result_error_attributes)
- #stream_result_attributes(context) ⇒ Object
- #stream_result_chunk_attributes(context, chunk) ⇒ Object
Instance Method Details
#chat_request_body(conversation, options = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 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 |
# File 'lib/intelligence/adapters/open_ai/chat_methods.rb', line 33 def chat_request_body( conversation, = {} ) = ? self.class.configure( ) : {} = @options.merge( ) result = [ :chat_options ]&.compact || {} result[ :messages ] = [] = ( conversation[ :system_message ] ) result[ :messages ] << { role: 'system', content: } if conversation[ :messages ]&.each do | | = { role: [ :role ] } = [] [ :contents ]&.each do | 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 if SUPPORTED_CONTENT_TYPES.include?( content_type ) << { type: 'image_url', image_url: { url: "data:#{content_type};base64,#{Base64.strict_encode64( bytes )}".freeze } } else raise UnsupportedContentError.new( :open_ai, "only supports content of type #{SUPPORTED_CONTENT_TYPES.join( ', ' )}" ) end else raise UnsupportedContentError.new( :open_ai, '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 if SUPPORTED_CONTENT_TYPES.include?( content_type ) << { type: 'image_url', image_url: { url: uri } } else raise UnsupportedContentError.new( :open_ai, "only supports content of type #{SUPPORTED_CONTENT_TYPES.join( ', ' )}" ) end else raise UnsupportedContentError.new( :open_ai, 'requires file content to include content type and uri' ) end else raise InvalidContentError.new( :open_ai ) end end [ :content ] = result[ :messages ] << end JSON.generate( result ) end |
#chat_request_headers(options = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/intelligence/adapters/open_ai/chat_methods.rb', line 13 def chat_request_headers( = {} ) = ? self.class.configure( ) : {} = @options.merge( ) result = {} key = [ :key ] organization = [ :organization ] project = [ :project ] raise ArgumentError.new( "An OpenAI key is required to build an OpenAI chat request." ) \ if key.nil? result[ 'Content-Type' ] = 'application/json' result[ 'Authorization' ] = "Bearer #{key}" result[ 'OpenAI-Organization' ] = organization unless organization.nil? result[ 'OpenAI-Project' ] = project unless project.nil? result end |
#chat_request_uri(options) ⇒ Object
9 10 11 |
# File 'lib/intelligence/adapters/open_ai/chat_methods.rb', line 9 def chat_request_uri( ) CHAT_REQUEST_URI end |
#chat_result_attributes(response) ⇒ Object
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 |
# File 'lib/intelligence/adapters/open_ai/chat_methods.rb', line 111 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_choice[ :message ] result[ :choices ].push( { end_reason: translate_end_result( json_choice[ :finish_reason ] ), message: { role: [ :role ], contents: [ { type: 'text', text: [ :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
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/intelligence/adapters/open_ai/chat_methods.rb', line 149 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
265 266 267 268 269 270 271 272 273 274 |
# File 'lib/intelligence/adapters/open_ai/chat_methods.rb', line 265 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
168 169 170 171 172 173 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 |
# File 'lib/intelligence/adapters/open_ai/chat_methods.rb', line 168 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 ].nil? last_content[ :type ] = :text last_content[ :text ] = data_choice_content elsif 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' ] metrics[ :input_tokens ] += usage[ 'prompt_tokens' ] metrics[ :output_tokens ] += usage[ 'completion_tokens' ] end end end context[ :buffer ] = buffer context[ :metrics ] = metrics context[ :choices ] = choices [ context, choices.empty? ? nil : { choices: choices.dup } ] end |