Class: OmniAI::OpenAI::File

Inherits:
Object
  • Object
show all
Defined in:
lib/omniai/openai/file.rb

Overview

An OpenAI file implementation.

Defined Under Namespace

Modules: Purpose

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: Client.new, io: nil, id: nil, bytes: nil, filename: nil, purpose: Purpose::ASSISTANTS) ⇒ File

Returns a new instance of File.

Parameters:

  • client (OmniAI::OpenAI::Client) (defaults to: Client.new)

    optional

  • io (IO) (defaults to: nil)

    optional

  • id (String) (defaults to: nil)

    optional

  • bytes (Integer) (defaults to: nil)

    optional

  • filename (String) (defaults to: nil)

    optional

  • purpose (String) (defaults to: Purpose::ASSISTANTS)

    optional



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/omniai/openai/file.rb', line 37

def initialize(
  client: Client.new,
  io: nil,
  id: nil,
  bytes: nil,
  filename: nil,
  purpose: Purpose::ASSISTANTS
)
  @client = client
  @io = io
  @id = id
  @bytes = bytes
  @filename = filename
  @purpose = purpose
end

Instance Attribute Details

#bytesInteger?

Returns:

  • (Integer, nil)


13
14
15
# File 'lib/omniai/openai/file.rb', line 13

def bytes
  @bytes
end

#deletedBoolean?

Returns:

  • (Boolean, nil)


25
26
27
# File 'lib/omniai/openai/file.rb', line 25

def deleted
  @deleted
end

#filenameString?

Returns:

  • (String, nil)


17
18
19
# File 'lib/omniai/openai/file.rb', line 17

def filename
  @filename
end

#idString?

Returns:

  • (String, nil)


9
10
11
# File 'lib/omniai/openai/file.rb', line 9

def id
  @id
end

#purposeString?

Returns:

  • (String, nil)


21
22
23
# File 'lib/omniai/openai/file.rb', line 21

def purpose
  @purpose
end

Class Method Details

.all(client: Client.new) ⇒ Array<OmniAI::OpenAI::File>

Parameters:

Returns:

Raises:

  • (HTTPError)


86
87
88
89
90
91
92
93
94
# File 'lib/omniai/openai/file.rb', line 86

def self.all(client: Client.new)
  response = client.connection
    .accept(:json)
    .get("/#{OmniAI::OpenAI::Client::VERSION}/files")

  raise HTTPError, response.flush unless response.status.ok?

  response.parse['data'].map { |data| parse(data:, client:) }
end

.destroy!(id:, client: Client.new) ⇒ Hash

Parameters:

Returns:

  • (Hash)

Raises:

  • (HTTPError)


99
100
101
102
103
104
105
106
107
# File 'lib/omniai/openai/file.rb', line 99

def self.destroy!(id:, client: Client.new)
  response = client.connection
    .accept(:json)
    .delete("/#{OmniAI::OpenAI::Client::VERSION}/files/#{id}")

  raise HTTPError, response.flush unless response.status.ok?

  response.parse
end

.find(id:, client: Client.new) ⇒ OmniAI::OpenAI::Assistant

Parameters:

Returns:

Raises:

  • (HTTPError)


74
75
76
77
78
79
80
81
82
# File 'lib/omniai/openai/file.rb', line 74

def self.find(id:, client: Client.new)
  response = client.connection
    .accept(:json)
    .get("/#{OmniAI::OpenAI::Client::VERSION}/files/#{id}")

  raise HTTPError, response.flush unless response.status.ok?

  parse(data: response.parse)
end

Instance Method Details

#content {|String| ... } ⇒ Object

Yields:

  • (String)

Raises:

  • (OmniAI::Error)


60
61
62
63
64
65
66
67
68
69
# File 'lib/omniai/openai/file.rb', line 60

def content(&)
  raise OmniAI::Error, 'cannot fetch content without ID' unless @id

  response = @client.connection
    .get("/#{OmniAI::OpenAI::Client::VERSION}/files/#{@id}/content")

  raise HTTPError, response.flush unless response.status.ok?

  response.body.each(&)
end

#destroy!OmniAI::OpenAI::Assistant

Returns:

Raises:

  • (OmniAI::Error)


125
126
127
128
129
130
131
# File 'lib/omniai/openai/file.rb', line 125

def destroy!
  raise OmniAI::Error, 'cannot destroy w/o ID' unless @id

  data = self.class.destroy!(id: @id, client: @client)
  @deleted = data['deleted']
  self
end

#inspectString

Returns:

  • (String)


54
55
56
# File 'lib/omniai/openai/file.rb', line 54

def inspect
  "#<#{self.class.name} id=#{@id.inspect} filename=#{@filename.inspect}>"
end

#save!OmniAI::OpenAI::Assistant

Returns:

Raises:

  • (HTTPError)


111
112
113
114
115
116
117
118
119
120
121
# File 'lib/omniai/openai/file.rb', line 111

def save!
  raise OmniAI::Error, 'cannot save a file without IO' unless @io

  response = @client.connection
    .accept(:json)
    .post("/#{OmniAI::OpenAI::Client::VERSION}/files", form: payload)
  raise HTTPError, response.flush unless response.status.ok?

  parse(data: response.parse)
  self
end