Class: GptFunction::File
- Inherits:
-
Object
- Object
- GptFunction::File
- Defined in:
- lib/gpt_function/file.rb
Instance Attribute Summary collapse
-
#bytes ⇒ Object
readonly
Returns the value of attribute bytes.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#purpose ⇒ Object
readonly
Returns the value of attribute purpose.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#status_details ⇒ Object
readonly
Returns the value of attribute status_details.
Class Method Summary collapse
- .content(file_id) ⇒ Object
- .create(hash_array) ⇒ Object
- .delete(file_id) ⇒ Object
- .from_id(file_id) ⇒ Object
- .jsonl(file_id) ⇒ Object
- .list ⇒ Object
Instance Method Summary collapse
- #content ⇒ Object
- #delete ⇒ Object
-
#initialize(hash) ⇒ File
constructor
A new instance of File.
- #inspect ⇒ Object
- #jsonl ⇒ Object
- #to_hash ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(hash) ⇒ File
Returns a new instance of File.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/gpt_function/file.rb', line 17 def initialize(hash) @object = hash["object"] @id = hash["id"] @purpose = hash["purpose"] @filename = hash["filename"] @bytes = hash["bytes"] @created_at = hash["created_at"] @status = hash["status"] @status_details = hash["status_details"] end |
Instance Attribute Details
#bytes ⇒ Object (readonly)
Returns the value of attribute bytes.
12 13 14 |
# File 'lib/gpt_function/file.rb', line 12 def bytes @bytes end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
13 14 15 |
# File 'lib/gpt_function/file.rb', line 13 def created_at @created_at end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
11 12 13 |
# File 'lib/gpt_function/file.rb', line 11 def filename @filename end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
9 10 11 |
# File 'lib/gpt_function/file.rb', line 9 def id @id end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
8 9 10 |
# File 'lib/gpt_function/file.rb', line 8 def object @object end |
#purpose ⇒ Object (readonly)
Returns the value of attribute purpose.
10 11 12 |
# File 'lib/gpt_function/file.rb', line 10 def purpose @purpose end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
14 15 16 |
# File 'lib/gpt_function/file.rb', line 14 def status @status end |
#status_details ⇒ Object (readonly)
Returns the value of attribute status_details.
15 16 17 |
# File 'lib/gpt_function/file.rb', line 15 def status_details @status_details end |
Class Method Details
.content(file_id) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/gpt_function/file.rb', line 155 def content(file_id) uri = URI("https://api.openai.com/v1/files/#{file_id}/content") request = Net::HTTP::Get.new(uri) request['Authorization'] = "Bearer #{GptFunction.api_key}" response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| http.request(request) end # { # "error": { # "message": "No such File object: #{file_id}", # "type": "invalid_request_error", # "param": "id", # "code": null # } # } raise "File retrieval failed: #{response.body}" unless response.is_a?(Net::HTTPSuccess) response.body end |
.create(hash_array) ⇒ Object
105 106 107 108 109 110 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 |
# File 'lib/gpt_function/file.rb', line 105 def create(hash_array) # 將請求資料轉換為 JSONL 格式的字串 jsonl = hash_array.map(&:to_json).join("\n") # 上傳資料 uri = URI('https://api.openai.com/v1/files') request = Net::HTTP::Post.new(uri) request['Authorization'] = "Bearer #{GptFunction.api_key}" request['Content-Type'] = 'multipart/form-data' # 創建 multipart form data boundary = "CustomBoundary" post_body = [] post_body << "--#{boundary}\r\n" post_body << "Content-Disposition: form-data; name=\"purpose\"\r\n\r\n" post_body << "batch\r\n" post_body << "--#{boundary}\r\n" post_body << "Content-Disposition: form-data; name=\"file\"; filename=\"batchinput.jsonl\"\r\n" post_body << "Content-Type: application/json\r\n\r\n" post_body << jsonl post_body << "\r\n--#{boundary}--\r\n" request.body = post_body.join request['Content-Type'] = "multipart/form-data; boundary=#{boundary}" response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| http.request(request) end raise "File upload failed: #{response.body}" unless response.is_a?(Net::HTTPSuccess) hash = JSON.parse(response.body) File.new(hash) end |
.delete(file_id) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/gpt_function/file.rb', line 180 def delete(file_id) uri = URI("https://api.openai.com/v1/files/#{file_id}") request = Net::HTTP::Delete.new(uri) request['Authorization'] = "Bearer #{GptFunction.api_key}" response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| http.request(request) end # { # "error": { # "message": "No such File object: file-5m1Cn4M36GOfd7bEVAoTCmcC", # "type": "invalid_request_error", # "param": "id", # "code": null # } # } raise "File deletion failed: #{response.body}" unless response.is_a?(Net::HTTPSuccess) # {"object"=>"file", "deleted"=>true, "id"=>"file-vsCH6lJkiFzi6gF9B8un3ZLT"} JSON.parse(response.body) end |
.from_id(file_id) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/gpt_function/file.rb', line 140 def from_id(file_id) uri = URI("https://api.openai.com/v1/files/#{file_id}") request = Net::HTTP::Get.new(uri) request['Authorization'] = "Bearer #{GptFunction.api_key}" response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| http.request(request) end raise "File retrieval failed: #{response.body}" unless response.is_a?(Net::HTTPSuccess) hash = JSON.parse(response.body) File.new(hash) end |
.jsonl(file_id) ⇒ Object
176 177 178 |
# File 'lib/gpt_function/file.rb', line 176 def jsonl(file_id) content(file_id).split("\n").map { |line| JSON.parse(line) } end |
.list ⇒ Object
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 |
# File 'lib/gpt_function/file.rb', line 62 def list uri = URI("https://api.openai.com/v1/files") request = Net::HTTP::Get.new(uri) request['Authorization'] = "Bearer #{GptFunction.api_key}" response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| http.request(request) end raise "File retrieval failed: #{response.body}" unless response.is_a?(Net::HTTPSuccess) # example response body # { # "object": "list", # "data": [ # { # "object": "file", # "id": "file-uYu4HIFAoq0OeZDGBD5Ci8wL", # "purpose": "batch_output", # "filename": "batch_YMZbhJWcBYETMTfOfEf041qF_output.jsonl", # "bytes": 1934, # "created_at": 1722327874, # "status": "processed", # "status_details": null # }, # { # "object": "file", # "id": "file-5AW0tCvRFKomu5s5G90yfWhs", # "purpose": "batch", # "filename": "batchinput.jsonl", # "bytes": 728, # "created_at": 1722327858, # "status": "processed", # "status_details": null # }, # ] # } body_hash = JSON.parse(response.body) body_hash.dig("data").map do |hash| File.new(hash) end end |
Instance Method Details
#content ⇒ Object
41 42 43 |
# File 'lib/gpt_function/file.rb', line 41 def content File.content(id) end |
#inspect ⇒ Object
57 58 59 |
# File 'lib/gpt_function/file.rb', line 57 def inspect to_hash.to_json end |
#to_hash ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/gpt_function/file.rb', line 28 def to_hash { object: object, id: id, purpose: purpose, filename: filename, bytes: bytes, created_at: created_at, status: status, status_details: status_details, } end |
#to_s ⇒ Object
53 54 55 |
# File 'lib/gpt_function/file.rb', line 53 def to_s to_hash.to_json end |