Class: BddOpenai::Services::Files::Upload

Inherits:
Base
  • Object
show all
Defined in:
lib/bdd_openai/services/files/upload.rb

Instance Method Summary collapse

Methods inherited from Base

#default_headers, #initialize

Constructor Details

This class inherits a constructor from BddOpenai::Services::Files::Base

Instance Method Details

#upload_files(purpose, file_path) ⇒ BddOpenai::Mapper::File, BddOpenai::ErrorResponse

Parameters:

  • purpose (String)

    The intended purpose of the file. One of: “fine-tune”, “assistants”.

  • file_path (String)

    The path of the file to upload.rb

Returns:



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/bdd_openai/services/files/upload.rb', line 12

def upload_files(purpose, file_path)
  uri = URI.parse("#{@openai_api_domain}/files")
  body, boundary = @http_client.create_multipart_body({ purpose: purpose }, { file: file_path })
  headers = default_headers
            .merge({
                     "Content-Type": "multipart/form-data; boundary=#{boundary}"
                   })
  response = @http_client.call_post(uri, body, headers)
  return BddOpenai::ErrorResponse.from_json(response.body) unless response.code == '200'

  BddOpenai::Mapper::File.from_json(response.body)
end