Class: GptFunction::Batch
- Inherits:
-
Object
- Object
- GptFunction::Batch
- Defined in:
- lib/gpt_function/batch.rb
Instance Attribute Summary collapse
-
#cancelled_at ⇒ Object
readonly
Returns the value of attribute cancelled_at.
-
#cancelling_at ⇒ Object
readonly
Returns the value of attribute cancelling_at.
-
#completed_at ⇒ Object
readonly
Returns the value of attribute completed_at.
-
#completion_window ⇒ Object
readonly
Returns the value of attribute completion_window.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#error_file_id ⇒ Object
readonly
Returns the value of attribute error_file_id.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#expired_at ⇒ Object
readonly
Returns the value of attribute expired_at.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#failed_at ⇒ Object
readonly
Returns the value of attribute failed_at.
-
#finalizing_at ⇒ Object
readonly
Returns the value of attribute finalizing_at.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#in_progress_at ⇒ Object
readonly
Returns the value of attribute in_progress_at.
-
#input_file_id ⇒ Object
readonly
Returns the value of attribute input_file_id.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#output_file_id ⇒ Object
readonly
Returns the value of attribute output_file_id.
-
#request_counts_completed ⇒ Object
readonly
Returns the value of attribute request_counts_completed.
-
#request_counts_failed ⇒ Object
readonly
Returns the value of attribute request_counts_failed.
-
#request_counts_total ⇒ Object
readonly
Returns the value of attribute request_counts_total.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
- .cancel(batch_id) ⇒ Object
- .create(requests, metadata: nil) ⇒ Object
- .dequeue ⇒ Object
- .from_id(batch_id) ⇒ Object
- .list(limit: 20, after: nil) ⇒ Object
-
.process(count: 1, &block) ⇒ Object
進行批次請求處理 count: 處理批次請求的數量 block: 處理批次請求的 block 返回值: 是否還有批次請求需要處理.
Instance Method Summary collapse
- #auto_delete ⇒ Object
- #cancel ⇒ Object
- #enqueue ⇒ Object
- #error_file ⇒ Object
-
#initialize(hash) ⇒ Batch
constructor
A new instance of Batch.
- #input_file ⇒ Object
- #input_jsonl ⇒ Object
- #inputs ⇒ Object
- #inspect ⇒ Object
-
#is_processed ⇒ Object
validating the input file is being validated before the batch can begin failed the input file has failed the validation process in_progress the input file was successfully validated and the batch is currently being run finalizing the batch has completed and the results are being prepared completed the batch has been completed and the results are ready expired the batch was not able to be completed within the 24-hour time window cancelling the batch is being cancelled (may take up to 10 minutes) cancelled the batch was cancelled.
- #output_file ⇒ Object
- #output_jsonl ⇒ Object
- #outputs ⇒ Object
- #pairs ⇒ Object
- #to_hash ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(hash) ⇒ Batch
Returns a new instance of Batch.
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 |
# File 'lib/gpt_function/batch.rb', line 33 def initialize(hash) @id = hash["id"] @object = hash["object"] @endpoint = hash["endpoint"] @errors = hash["errors"] @input_file_id = hash["input_file_id"] @completion_window = hash["completion_window"] @status = hash["status"] @output_file_id = hash["output_file_id"] @error_file_id = hash["error_file_id"] @created_at = hash["created_at"] @in_progress_at = hash["in_progress_at"] @expires_at = hash["expires_at"] @finalizing_at = hash["finalizing_at"] @completed_at = hash["completed_at"] @failed_at = hash["failed_at"] @expired_at = hash["expired_at"] @cancelling_at = hash["cancelling_at"] @cancelled_at = hash["cancelled_at"] @request_counts_total = hash.dig("request_counts", "total") @request_counts_completed = hash.dig("request_counts", "completed") @request_counts_failed = hash.dig("request_counts", "failed") @metadata = hash.dig("metadata") end |
Instance Attribute Details
#cancelled_at ⇒ Object (readonly)
Returns the value of attribute cancelled_at.
25 26 27 |
# File 'lib/gpt_function/batch.rb', line 25 def cancelled_at @cancelled_at end |
#cancelling_at ⇒ Object (readonly)
Returns the value of attribute cancelling_at.
24 25 26 |
# File 'lib/gpt_function/batch.rb', line 24 def cancelling_at @cancelling_at end |
#completed_at ⇒ Object (readonly)
Returns the value of attribute completed_at.
21 22 23 |
# File 'lib/gpt_function/batch.rb', line 21 def completed_at @completed_at end |
#completion_window ⇒ Object (readonly)
Returns the value of attribute completion_window.
13 14 15 |
# File 'lib/gpt_function/batch.rb', line 13 def completion_window @completion_window end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
17 18 19 |
# File 'lib/gpt_function/batch.rb', line 17 def created_at @created_at end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
10 11 12 |
# File 'lib/gpt_function/batch.rb', line 10 def endpoint @endpoint end |
#error_file_id ⇒ Object (readonly)
Returns the value of attribute error_file_id.
16 17 18 |
# File 'lib/gpt_function/batch.rb', line 16 def error_file_id @error_file_id end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
11 12 13 |
# File 'lib/gpt_function/batch.rb', line 11 def errors @errors end |
#expired_at ⇒ Object (readonly)
Returns the value of attribute expired_at.
23 24 25 |
# File 'lib/gpt_function/batch.rb', line 23 def expired_at @expired_at end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
19 20 21 |
# File 'lib/gpt_function/batch.rb', line 19 def expires_at @expires_at end |
#failed_at ⇒ Object (readonly)
Returns the value of attribute failed_at.
22 23 24 |
# File 'lib/gpt_function/batch.rb', line 22 def failed_at @failed_at end |
#finalizing_at ⇒ Object (readonly)
Returns the value of attribute finalizing_at.
20 21 22 |
# File 'lib/gpt_function/batch.rb', line 20 def finalizing_at @finalizing_at end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
8 9 10 |
# File 'lib/gpt_function/batch.rb', line 8 def id @id end |
#in_progress_at ⇒ Object (readonly)
Returns the value of attribute in_progress_at.
18 19 20 |
# File 'lib/gpt_function/batch.rb', line 18 def in_progress_at @in_progress_at end |
#input_file_id ⇒ Object (readonly)
Returns the value of attribute input_file_id.
12 13 14 |
# File 'lib/gpt_function/batch.rb', line 12 def input_file_id @input_file_id end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
31 32 33 |
# File 'lib/gpt_function/batch.rb', line 31 def @metadata end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
9 10 11 |
# File 'lib/gpt_function/batch.rb', line 9 def object @object end |
#output_file_id ⇒ Object (readonly)
Returns the value of attribute output_file_id.
15 16 17 |
# File 'lib/gpt_function/batch.rb', line 15 def output_file_id @output_file_id end |
#request_counts_completed ⇒ Object (readonly)
Returns the value of attribute request_counts_completed.
28 29 30 |
# File 'lib/gpt_function/batch.rb', line 28 def request_counts_completed @request_counts_completed end |
#request_counts_failed ⇒ Object (readonly)
Returns the value of attribute request_counts_failed.
29 30 31 |
# File 'lib/gpt_function/batch.rb', line 29 def request_counts_failed @request_counts_failed end |
#request_counts_total ⇒ Object (readonly)
Returns the value of attribute request_counts_total.
27 28 29 |
# File 'lib/gpt_function/batch.rb', line 27 def request_counts_total @request_counts_total end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
14 15 16 |
# File 'lib/gpt_function/batch.rb', line 14 def status @status end |
Class Method Details
.cancel(batch_id) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/gpt_function/batch.rb', line 255 def cancel(batch_id) uri = URI("https://api.openai.com/v1/batches/#{batch_id}/cancel") request = Net::HTTP::Post.new(uri) request['Authorization'] = "Bearer #{GptFunction.api_key}" request['Content-Type'] = 'application/json' response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| http.request(request) end # { # "error": { # "message": "Cannot cancel a batch with status 'completed'.", # "type": "invalid_request_error", # "param": null, # "code": null # } # } raise "Batch cancel failed: #{response.body}" unless response.is_a?(Net::HTTPSuccess) response.body end |
.create(requests, metadata: nil) ⇒ Object
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 |
# File 'lib/gpt_function/batch.rb', line 198 def create(requests, metadata: nil) requests = requests.each_with_index.map do |request, index| { custom_id: "request-#{index + 1}", method: "POST", url: "/v1/chat/completions", body: request, } end # 上傳資料 file = File.create(requests) # 創建批次請求 uri = URI('https://api.openai.com/v1/batches') request = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json') request['Authorization'] = "Bearer #{GptFunction.api_key}" body = { input_file_id: file.id, endpoint: '/v1/chat/completions', completion_window: '24h' } body[:metadata] = unless .nil? request.body = body.to_json response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| http.request(request) end raise "Batch creation failed: #{response.body}" unless response.is_a?(Net::HTTPSuccess) hash = JSON.parse(response.body) batch = Batch.new(hash) batch.enqueue batch rescue => e file&.delete raise e end |
.dequeue ⇒ Object
278 279 280 281 282 |
# File 'lib/gpt_function/batch.rb', line 278 def dequeue hash = GptFunction::Storage.batch_storage&.dequeue id = hash&.dig("id") || hash&.dig(:id) from_id(id) if id end |
.from_id(batch_id) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/gpt_function/batch.rb', line 238 def from_id(batch_id) # 檢查批次狀態 uri = URI("https://api.openai.com/v1/batches/#{batch_id}") request = Net::HTTP::Get.new(uri) request['Authorization'] = "Bearer #{GptFunction.api_key}" request['Content-Type'] = 'application/json' response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| http.request(request) end raise "Batch status check failed: #{response.body}" unless response.is_a?(Net::HTTPSuccess) hash = JSON.parse(response.body) Batch.new(hash) end |
.list(limit: 20, after: nil) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/gpt_function/batch.rb', line 180 def list(limit: 20, after: nil) # 創建批次請求 uri = URI('https://api.openai.com/v1/batches') request = Net::HTTP::Get.new(uri, 'Content-Type' => 'application/json') request['Authorization'] = "Bearer #{GptFunction.api_key}" response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| http.request(request) end raise "Batch creation failed: #{response.body}" unless response.is_a?(Net::HTTPSuccess) body_hash = JSON.parse(response.body) body_hash.dig("data").map do |hash| Batch.new(hash) end end |
.process(count: 1, &block) ⇒ Object
進行批次請求處理 count: 處理批次請求的數量 block: 處理批次請求的 block 返回值: 是否還有批次請求需要處理
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/gpt_function/batch.rb', line 288 def process(count: 1, &block) # 從 Storage 取出 count 個批次請求 count.times do batch = dequeue # 如果沒有批次請求,則跳出迴圈 return false if batch.nil? yield batch # 如果 batch 還未處理完成,將批次請求重新加入 Storage if batch.is_processed && batch.auto_delete batch&.input_file&.delete rescue nil batch&.output_file&.delete rescue nil batch&.error_file&.delete rescue nil else batch.enqueue end end true end |
Instance Method Details
#auto_delete ⇒ Object
175 176 177 |
# File 'lib/gpt_function/batch.rb', line 175 def auto_delete &.dig("auto_delete") == "true" end |
#cancel ⇒ Object
153 154 155 |
# File 'lib/gpt_function/batch.rb', line 153 def cancel Batch.cancel(id) end |
#enqueue ⇒ Object
157 158 159 160 161 |
# File 'lib/gpt_function/batch.rb', line 157 def enqueue return false if GptFunction::Storage.batch_storage.nil? GptFunction::Storage.batch_storage.enqueue(self.to_hash) end |
#error_file ⇒ Object
105 106 107 108 |
# File 'lib/gpt_function/batch.rb', line 105 def error_file return nil if error_file_id.nil? @error_file ||= File.from_id(error_file_id) end |
#input_file ⇒ Object
95 96 97 98 |
# File 'lib/gpt_function/batch.rb', line 95 def input_file return nil if input_file_id.nil? @input_file ||= File.from_id(input_file_id) end |
#input_jsonl ⇒ Object
110 111 112 |
# File 'lib/gpt_function/batch.rb', line 110 def input_jsonl @input_jsonl ||= input_file&.jsonl || [] end |
#inputs ⇒ Object
118 119 120 121 122 123 124 125 |
# File 'lib/gpt_function/batch.rb', line 118 def inputs @inputs ||= input_jsonl.map do |hash| { "custom_id" => hash.dig("custom_id"), "content" => hash.dig("body", "messages", -1, "content") } end end |
#inspect ⇒ Object
91 92 93 |
# File 'lib/gpt_function/batch.rb', line 91 def inspect to_hash.to_json end |
#is_processed ⇒ Object
validating the input file is being validated before the batch can begin failed the input file has failed the validation process in_progress the input file was successfully validated and the batch is currently being run finalizing the batch has completed and the results are being prepared completed the batch has been completed and the results are ready expired the batch was not able to be completed within the 24-hour time window cancelling the batch is being cancelled (may take up to 10 minutes) cancelled the batch was cancelled
171 172 173 |
# File 'lib/gpt_function/batch.rb', line 171 def is_processed ["failed", "completed", "expired", "cancelled"].include? status end |
#output_file ⇒ Object
100 101 102 103 |
# File 'lib/gpt_function/batch.rb', line 100 def output_file return nil if output_file_id.nil? @output_file ||= File.from_id(output_file_id) end |
#output_jsonl ⇒ Object
114 115 116 |
# File 'lib/gpt_function/batch.rb', line 114 def output_jsonl @output_jsonl ||= output_file&.jsonl || [] end |
#outputs ⇒ Object
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/gpt_function/batch.rb', line 127 def outputs @outputs ||= output_jsonl.map do |hash| content = hash.dig("response", "body", "choices", 0, "message", "content") content = JSON.parse(content)["output"] rescue content { "custom_id" => hash.dig("custom_id"), "content" => content } end end |
#pairs ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/gpt_function/batch.rb', line 138 def pairs hash = {} outputs.each do |output| hash[output["custom_id"]] = [nil ,output["content"]] end inputs.each do |input| next if hash[input["custom_id"]].nil? hash[input["custom_id"]][0] = input["content"] end hash.values end |
#to_hash ⇒ Object
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 |
# File 'lib/gpt_function/batch.rb', line 60 def to_hash { id: id, object: object, endpoint: endpoint, errors: errors, input_file_id: input_file_id, completion_window: completion_window, status: status, output_file_id: output_file_id, error_file_id: error_file_id, created_at: created_at, in_progress_at: in_progress_at, expires_at: expires_at, finalizing_at: finalizing_at, completed_at: completed_at, failed_at: failed_at, expired_at: expired_at, cancelling_at: cancelling_at, cancelled_at: cancelled_at, request_counts_total: request_counts_total, request_counts_completed: request_counts_completed, request_counts_failed: request_counts_failed, metadata: } end |
#to_s ⇒ Object
87 88 89 |
# File 'lib/gpt_function/batch.rb', line 87 def to_s to_hash.to_json end |