Class: Asimov::ApiV1::Files

Inherits:
Base
  • Object
show all
Defined in:
lib/asimov/api_v1/files.rb

Overview

Class interface for API methods in the “/files” URI subspace.

Instance Method Summary collapse

Methods inherited from Base

#initialize, #rest_create_w_json_params, #rest_create_w_multipart_params, #rest_delete, #rest_get, #rest_get_streamed_download, #rest_index

Constructor Details

This class inherits a constructor from Asimov::ApiV1::Base

Instance Method Details

#content(file_id:, writer:) ⇒ Object

Retrieves the contents of the file with the specified file_id from OpenAI and passes those contents to the writer in a chunked manner.

as it is received from the API

Parameters:

  • file_id (String)

    the id of the file to be retrieved

  • writer (Writer)

    the Writer that will process the chunked content



67
68
69
# File 'lib/asimov/api_v1/files.rb', line 67

def content(file_id:, writer:)
  rest_get_streamed_download(resource: [RESOURCE, file_id, "content"], writer: writer)
end

#delete(file_id:) ⇒ Object

Deletes the file with the specified file_id from OpenAI.

Parameters:

  • file_id (String)

    the id of the file to be deleted



55
56
57
# File 'lib/asimov/api_v1/files.rb', line 55

def delete(file_id:)
  rest_delete(resource: RESOURCE, id: file_id)
end

#listObject

Lists files that have been uploaded to OpenAI



19
20
21
# File 'lib/asimov/api_v1/files.rb', line 19

def list
  rest_index(resource: RESOURCE)
end

#retrieve(file_id:) ⇒ Object

Retrieves the file with the specified file_id from OpenAI.

Parameters:

  • file_id (String)

    the id of the file to be retrieved



46
47
48
# File 'lib/asimov/api_v1/files.rb', line 46

def retrieve(file_id:)
  rest_get(resource: RESOURCE, id: file_id)
end

#upload(file:, purpose:, parameters: {}) ⇒ Object

Uploads a file to the /files endpoint with the specified parameters.

Parameters:

  • file (String)

    file name or a File-like object to be uploaded

  • parameters (Hash) (defaults to: {})

    the set of parameters being passed to the API

Raises:



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/asimov/api_v1/files.rb', line 29

def upload(file:, purpose:, parameters: {})
  raise MissingRequiredParameterError.new(:file) unless file
  raise MissingRequiredParameterError.new(:purpose) unless purpose

  validate(file, purpose)

  rest_create_w_multipart_params(
    resource: RESOURCE,
    parameters: parameters.merge(file: Utils::FileManager.open(file), purpose: purpose)
  )
end