gpt-function 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cdeb301d12f6247b694accec4654d4d65bab0ca071cb4bf7ae73631bf540bc56
4
- data.tar.gz: dd5cb067931ac6411a545641b96f40ef81d929bd1d388cfe93808d5c76b6e2ec
3
+ metadata.gz: 61133cf0bb081bfcc03dc1e0b2a69bbc7f043c7dc6fd9c5e56d0d9734c975575
4
+ data.tar.gz: b153cad22540e8b2ad46293c8dffc1aa63b27d48f87a60cb46a9d6cc869abc5d
5
5
  SHA512:
6
- metadata.gz: 665474c59064f7ae3401b0fea4e8ca562cfa2e91ff285523bbcdff9b367480f2d137b619b15d30c396df92a07c506bd73ed121bd8c54292d301785957486aa37
7
- data.tar.gz: f1072362a33e579c4f52eda7b8abbadc3970807eaca91c5aa39010ca130c3e935a9ee5194b207a8c4de8b11c2079a68bbc3675d63ddeb74d811f9c2de7c33aac
6
+ metadata.gz: 69fed6886b79e1b3e0034d3de36273b0bddc6b1ff59bb03067b737621ee85030ad334ceea48d72b9173025661f92f3f05d396d921720a4ee45a9f74dc7e9da50
7
+ data.tar.gz: f375fa50fc60c54663e8c488385541caa711ee97cae76495c885b734ce826fe79dc703d13f3594e54a7d44371be6e1f7458990a527b1095fe562a7195b011df4
data/README.md CHANGED
@@ -4,11 +4,6 @@
4
4
 
5
5
  你可以確保每次呼叫 GPT 函數時,接近 100% 都會得到相同的結果。
6
6
 
7
- 目前能夠使用的模型有:
8
-
9
- - gpt-4-1106-preview
10
- - gpt-3.5-turbo-1106
11
-
12
7
 
13
8
  ## Installation
14
9
 
@@ -99,20 +94,31 @@ end
99
94
  Batch Storage 整合 Active Record 的範例:
100
95
 
101
96
  ```ruby
102
- class Model < ApplicationRecord
97
+ class BatchStatus < ApplicationRecord
103
98
  class << self
104
99
  def enqueue(hash)
105
- create!(hash)
100
+ model = BatchStatus.new
101
+ model.batch_id = hash[:id]
102
+ model.status = hash[:status]
103
+ model.request_counts_completed = hash[:request_counts_completed]
104
+ model.request_counts_failed = hash[:request_counts_failed]
105
+ model.request_counts_total = hash[:request_counts_total]
106
+ model.metadata = hash[:metadata]
107
+ model.payload = hash
108
+ model.save
106
109
  true
107
110
  end
108
111
 
109
112
  def dequeue
110
- first&.destroy
113
+ model = first
114
+ model.destroy
115
+ model.payload
111
116
  end
112
117
  end
113
118
  end
114
119
 
115
- GptFunction.configure(api_key: '...', model: 'gpt-4o-mini', batch_storage: Model)
120
+
121
+ GptFunction.configure(api_key: '...', model: 'gpt-4o-mini', batch_storage: BatchStatus)
116
122
  ```
117
123
 
118
124
  ## License
@@ -28,8 +28,7 @@ class GptFunction
28
28
  attr_reader :request_counts_completed
29
29
  attr_reader :request_counts_failed
30
30
 
31
- attr_reader :metadata_customer_id
32
- attr_reader :metadata_batch_description
31
+ attr_reader :metadata
33
32
 
34
33
  def initialize(hash)
35
34
  @id = hash["id"]
@@ -55,8 +54,7 @@ class GptFunction
55
54
  @request_counts_completed = hash.dig("request_counts", "completed")
56
55
  @request_counts_failed = hash.dig("request_counts", "failed")
57
56
 
58
- @metadata_customer_id = hash.dig("metadata", "customer_id")
59
- @metadata_batch_description = hash.dig("metadata", "batch_description")
57
+ @metadata = hash.dig("metadata")
60
58
  end
61
59
 
62
60
  def to_hash
@@ -82,8 +80,7 @@ class GptFunction
82
80
  request_counts_total: request_counts_total,
83
81
  request_counts_completed: request_counts_completed,
84
82
  request_counts_failed: request_counts_failed,
85
- metadata_customer_id: metadata_customer_id,
86
- metadata_batch_description: metadata_batch_description,
83
+ metadata: metadata
87
84
  }
88
85
  end
89
86
 
@@ -105,6 +102,11 @@ class GptFunction
105
102
  @output_file ||= File.from_id(output_file_id)
106
103
  end
107
104
 
105
+ def error_file
106
+ return nil if error_file_id.nil?
107
+ @error_file ||= File.from_id(error_file_id)
108
+ end
109
+
108
110
  def input_jsonl
109
111
  @input_jsonl ||= input_file&.jsonl || []
110
112
  end
@@ -170,6 +172,10 @@ class GptFunction
170
172
  ["failed", "completed", "expired", "cancelled"].include? status
171
173
  end
172
174
 
175
+ def auto_delete
176
+ metadata&.dig("auto_delete") || false
177
+ end
178
+
173
179
  class << self
174
180
  def list(limit: 20, after: nil)
175
181
  # 創建批次請求
@@ -189,7 +195,7 @@ class GptFunction
189
195
  end
190
196
  end
191
197
 
192
- def create(requests)
198
+ def create(requests, metadata: nil)
193
199
  requests = requests.each_with_index.map do |request, index|
194
200
  {
195
201
  custom_id: "request-#{index + 1}",
@@ -206,11 +212,13 @@ class GptFunction
206
212
  uri = URI('https://api.openai.com/v1/batches')
207
213
  request = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
208
214
  request['Authorization'] = "Bearer #{GptFunction.api_key}"
209
- request.body = {
215
+ body = {
210
216
  input_file_id: file.id,
211
217
  endpoint: '/v1/chat/completions',
212
218
  completion_window: '24h'
213
- }.to_json
219
+ }
220
+ body[:metadata] = metadata unless metadata.nil?
221
+ request.body = body.to_json
214
222
 
215
223
  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
216
224
  http.request(request)
@@ -288,7 +296,13 @@ class GptFunction
288
296
  yield batch
289
297
 
290
298
  # 如果 batch 還未處理完成,將批次請求重新加入 Storage
291
- batch.enqueue unless batch.is_processed
299
+ if batch.is_processed && auto_delete
300
+ batch&.input_file&.delete rescue nil
301
+ batch&.output_file&.delete rescue nil
302
+ batch&.error_file&.delete rescue nil
303
+ else
304
+ batch.enqueue
305
+ end
292
306
  end
293
307
 
294
308
  true
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class GptFunction
4
- VERSION = "0.4.0"
4
+ VERSION = "0.6.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gpt-function
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - etrex kuo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-08-03 00:00:00.000000000 Z
11
+ date: 2024-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dotenv