openai-client 0.5.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: be7dc55119b7220165c1025d64ddf0f9a9f4a05efb79f4ffd03fece3c7cd60be
4
- data.tar.gz: 591cdc0fe81065d49db7907a0b9f740a17962744cdb9cc3ecdf1b9d40967c50e
3
+ metadata.gz: a80665d3e20f6c56644b2dd0b03b5f7a4231b3e0ef1d947af3094917944c885c
4
+ data.tar.gz: fd1bb8de6a7d955be8b2265e637a3bad4b09823e538a1c5b49ae8e3b4c8f067a
5
5
  SHA512:
6
- metadata.gz: 8d6f2dc7fa5cc014cf77a2defd2e0efce3f6da336612d3812c9b7df74befba4fc9a069c24c2491d16fc518fd22b985422d6a9c7e06f63c217728d70a6caf4eb4
7
- data.tar.gz: ba737dc9d234a49fb20afc98223354dee5a66f1efbac4d95df3b0676b0a8cad95688fbd1b4c38a49d24f5fe8f9001269b33596b03b7fd979aaaa1c39225e300b
6
+ metadata.gz: 5e6414ba8bf6453fb683870b8a90a7ffaea0101461d73baded7c0572a47bddabca443b1854b42f31db9a1ce95f957111a93bde585692d1f69a15a942d6b42ddd
7
+ data.tar.gz: '043982ac4ac832a6d8c8078996469bbddc794d56f113a35e2d3869338a34d49f2db58888d15cdd6713cb0e36a254d69e5eb9d26ab301bd9442de44e75298fba6'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- openai-client (0.5.0)
4
+ openai-client (0.8.0)
5
5
  faraday (~> 1.8)
6
6
  faraday_middleware (~> 1.2)
7
7
 
data/README.md CHANGED
@@ -12,6 +12,20 @@ This gem is a wrapper for calling the OpenAI and GPT-3 APIs.
12
12
  * [Create an Image Edit](#create-an-image-edit)
13
13
  * [Create an Image Variation](#create-an-image-variation)
14
14
  * [OpenAI Embeddings API](#openai-embeddings-api)
15
+ * [OpenAI Moderations API](#openai-moderations-api)
16
+ * [OpenAI Files API](#openai-files-api)
17
+ * [List Files](#list-files)
18
+ * [Find File](#find-file)
19
+ * [Find File Content](#find-file-content)
20
+ * [Upload File](#upload-file)
21
+ * [Delete File](#delete-file)
22
+ * [OpenAI Fine-Tunes API](#openai-fine-tunes-api)
23
+ * [Create Fine-Tune](#create-fine-tune)
24
+ * [List Fine-Tunes](#list-fine-tunes)
25
+ * [Find Fine-Tune](#find-fine-tune)
26
+ * [List Fine-Tune Events](#list-fine-tune-events)
27
+ * [Cancel Fine-Tune](#cancel-fine-tune)
28
+ * [Delete Fine-Tune Model](#delete-fine-tune-model)
15
29
 
16
30
  ## Installation
17
31
 
@@ -74,7 +88,7 @@ request_body = {
74
88
  Openai::Client.completions.create(request_body)
75
89
  ```
76
90
 
77
- [Request body documentation](https://platform.openai.com/docs/api-reference/completions/create)
91
+ [API Documentation](https://platform.openai.com/docs/api-reference/completions/create)
78
92
 
79
93
  ## OpenAI Edits API
80
94
 
@@ -87,7 +101,7 @@ request_body = {
87
101
  Openai::Client.edits.create(request_body)
88
102
  ```
89
103
 
90
- [Request body documentation](https://platform.openai.com/docs/api-reference/edits/create)
104
+ [API Documentation](https://platform.openai.com/docs/api-reference/edits/create)
91
105
 
92
106
  ## OpenAI Image API
93
107
 
@@ -103,7 +117,7 @@ request_body = {
103
117
  response = Openai::Client.images.create(request_body)
104
118
  ```
105
119
 
106
- [Request body documentation](https://platform.openai.com/docs/api-reference/images/create)
120
+ [API Documentation](https://platform.openai.com/docs/api-reference/images/create)
107
121
 
108
122
  ### Create an Image Edit
109
123
 
@@ -122,7 +136,7 @@ response = Openai::Client.images.edit(request_body)
122
136
  - `image` - must be a valid PNG file, less than 4MB, and square. If mask is not provided, image must have transparency, which will be used as the mask.
123
137
  - `mask` - an additional image whose fully transparent areas (e.g. where alpha is zero) indicate where image should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
124
138
 
125
- [Request body documentation](https://platform.openai.com/docs/api-reference/images/create-edit)
139
+ [API Documentation](https://platform.openai.com/docs/api-reference/images/create-edit)
126
140
 
127
141
  ### Create an Image Variation
128
142
 
@@ -138,7 +152,7 @@ response = Openai::Client.images.variations(request_body)
138
152
 
139
153
  - `image` - must be a valid PNG file, less than 4MB, and square.
140
154
 
141
- [Request body documentation](https://platform.openai.com/docs/api-reference/images/create-variation)
155
+ [API Documentation](https://platform.openai.com/docs/api-reference/images/create-variation)
142
156
 
143
157
  ## OpenAI Embeddings API
144
158
 
@@ -150,7 +164,131 @@ request_body = {
150
164
  Openai::Client.embeddings.create(request_body)
151
165
  ```
152
166
 
153
- [Request body documentation](https://platform.openai.com/docs/api-reference/embeddings/create)
167
+ [API Documentation](https://platform.openai.com/docs/api-reference/embeddings/create)
168
+
169
+ ## OpenAI Moderations API
170
+
171
+ ```ruby
172
+ request_body = {
173
+ model: 'text-moderation-latest', # text-moderation-stable or text-moderation-latest
174
+ input: 'I want to kill them.'
175
+ }
176
+ Openai::Client.moderations.create(request_body)
177
+ ```
178
+
179
+ [API Documentation](https://platform.openai.com/docs/api-reference/moderations/create)
180
+
181
+ ## OpenAI Files API
182
+
183
+ ### List Files
184
+
185
+ ```ruby
186
+ Openai::Client.files.list
187
+ ```
188
+
189
+ [API Documentation](https://platform.openai.com/docs/api-reference/files/list)
190
+
191
+ ### Find File
192
+
193
+ ```ruby
194
+ Openai::Client.files.find(file_id)
195
+ ```
196
+
197
+ [API Documentation](https://platform.openai.com/docs/api-reference/files/retrieve)
198
+
199
+ ### Find File Content
200
+
201
+ ```ruby
202
+ Openai::Client.files.find_content(file_id)
203
+ ```
204
+
205
+ [API Documentation](https://platform.openai.com/docs/api-reference/files/retrieve-content)
206
+
207
+ ### Upload File
208
+
209
+ ```ruby
210
+ request_body = {
211
+ file: '/absolute/path/to/file.jsonl',
212
+ purpose: 'fine-tune'
213
+ }
214
+ Openai::Client.files.upload(request_body)
215
+ ```
216
+ > The file format must be jsonl, where each line contains the prompt and completion properties.
217
+
218
+ Example (file.jsonl):
219
+
220
+ ```json
221
+ {"prompt": "<prompt text>", "completion": "<ideal generated text>"}
222
+ {"prompt": "<prompt text>", "completion": "<ideal generated text>"}
223
+ ...
224
+ ```
225
+
226
+ [API Documentation](https://platform.openai.com/docs/api-reference/files/upload)
227
+
228
+ ### Delete File
229
+
230
+ ```ruby
231
+ Openai::Client.files.delete(file_id)
232
+ ```
233
+
234
+ [API Documentation](https://platform.openai.com/docs/api-reference/files/delete)
235
+
236
+ ## OpenAI Fine-Tunes API
237
+
238
+ ### Create Fine-Tune
239
+
240
+ ```ruby
241
+ request_body = {
242
+ training_file: "file-XGinujblHPwGLSztz8cPS8XY"
243
+ }
244
+
245
+ Openai::Client.fine_tunes.create(request_body)
246
+ ```
247
+
248
+ [API Documentation](https://platform.openai.com/docs/api-reference/fine-tunes/create)
249
+
250
+ ### List Fine-Tunes
251
+
252
+ ```ruby
253
+ Openai::Client.fine_tunes.list
254
+ ```
255
+
256
+ [API Documentation](https://platform.openai.com/docs/api-reference/fine-tunes/list)
257
+
258
+ ### Find Fine-Tune
259
+
260
+ ```ruby
261
+ Openai::Client.fine_tunes.find(fine_tune_id)
262
+ ```
263
+
264
+ [API Documentation](https://platform.openai.com/docs/api-reference/fine-tunes/retrieve)
265
+
266
+ ### List Fine-Tune Events
267
+
268
+ ```ruby
269
+ Openai::Client.fine_tunes.find_events(fine_tune_id)
270
+ ```
271
+
272
+ [API Documentation](https://platform.openai.com/docs/api-reference/fine-tunes/events)
273
+
274
+ ### Cancel Fine-Tune
275
+
276
+ ```ruby
277
+ Openai::Client.fine_tunes.cancel(fine_tune_id)
278
+ ```
279
+
280
+ [API Documentation](https://platform.openai.com/docs/api-reference/fine-tunes/cancel)
281
+
282
+ ### Delete Fine-Tune Model
283
+
284
+ ```ruby
285
+ Openai::Client.models.delete(model_id)
286
+ ```
287
+
288
+ [API Documentation](https://platform.openai.com/docs/api-reference/fine-tunes/delete-model)
289
+
290
+ > - You must have the Owner role in your organization.
291
+ > - Make sure you provide the Model ID and not the Fine-Tune ID.
154
292
 
155
293
  ## Contributing
156
294
 
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openai/client/utils'
4
+
5
+ module Openai
6
+ module Client
7
+ class Files
8
+ PATH = 'files'
9
+
10
+ # @api public
11
+ # Public: Makes an API call to return all files that belong to the user's organization.
12
+ #
13
+ # @return [Hash] a list of files
14
+ def list
15
+ Http.new.get(PATH).body
16
+ rescue Faraday::Error
17
+ nil
18
+ end
19
+
20
+ # @api public
21
+ # Public: Makes an API call to upload a file.
22
+ #
23
+ # @param [Hash] body request body
24
+ #
25
+ # @return [Hash] the file
26
+ def upload(body)
27
+ Http.new.multipart_post(PATH, Utils.upload_io(body, %i[file], 'jsonl')).body
28
+ rescue Faraday::Error
29
+ nil
30
+ end
31
+
32
+ # @api public
33
+ # Public: Makes an API call to delete the file.
34
+ #
35
+ # @param [String] id file ID
36
+ #
37
+ # @return [Hash] the file
38
+ def delete(id)
39
+ Http.new.delete("#{PATH}/#{id}").body
40
+ rescue Faraday::Error
41
+ nil
42
+ end
43
+
44
+ # @api public
45
+ # Public: Makes an API call to find the file by the ID.
46
+ #
47
+ # @param [String] id file ID
48
+ #
49
+ # @return [Hash] found file
50
+ def find(id)
51
+ Http.new.get("#{PATH}/#{id}").body
52
+ rescue Faraday::Error
53
+ nil
54
+ end
55
+
56
+ # @api public
57
+ # Public: Makes an API call to find the contents of the specified file.
58
+ #
59
+ # @param [String] id file ID
60
+ #
61
+ # @return [Hash] the contents of the specified file
62
+ def find_content(id)
63
+ Http.new.get("#{PATH}/#{id}/content").body
64
+ rescue Faraday::Error
65
+ nil
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openai
4
+ module Client
5
+ class FineTunes
6
+ PATH = 'fine-tunes'
7
+
8
+ # @api public
9
+ # Public: Makes an API call to return list the organization's fine-tuning jobs.
10
+ #
11
+ # @return [Hash] a list of fine-tuning jobs
12
+ def list
13
+ Http.new.get(PATH).body
14
+ rescue Faraday::Error
15
+ nil
16
+ end
17
+
18
+ # @api public
19
+ # Public: Makes an API call to find info about the fine-tune job.
20
+ #
21
+ # @param [String] id the ID of the fine-tune job
22
+ #
23
+ # @return [Hash] found fine-tune info
24
+ def find(id)
25
+ Http.new.get("#{PATH}/#{id}").body
26
+ rescue Faraday::Error
27
+ nil
28
+ end
29
+
30
+ # @api public
31
+ # Public: Makes an API call to find fine-grained status updates for a fine-tune job.
32
+ #
33
+ # @param [String] id the ID of the fine-tune job
34
+ #
35
+ # @return [Hash] found fine-grained status updates
36
+ def find_events(id)
37
+ Http.new.get("#{PATH}/#{id}/events").body
38
+ rescue Faraday::Error
39
+ nil
40
+ end
41
+
42
+ # @api public
43
+ # Public: Makes an API call to cancel a fine-tune job.
44
+ #
45
+ # @param [String] id the ID of the fine-tune job
46
+ #
47
+ # @return [Hash] the fine-tune related info
48
+ def cancel(id)
49
+ Http.new.post("#{PATH}/#{id}/cancel").body
50
+ rescue Faraday::Error
51
+ nil
52
+ end
53
+
54
+ # @api public
55
+ # Public: Makes an API call to create a job that fine-tunes a specified model from a given dataset.
56
+ #
57
+ # @param [Hash] body request body
58
+ #
59
+ # @return [Hash] details of the enqueued job including job status and the name of the fine-tuned models
60
+ def create(body)
61
+ Http.new.post(PATH, body).body
62
+ rescue Faraday::Error
63
+ nil
64
+ end
65
+ end
66
+ end
67
+ end
@@ -61,6 +61,20 @@ module Openai
61
61
  log_error(e) && raise
62
62
  end
63
63
 
64
+ # @api public
65
+ # Public: Makes a DELETE request using the Faraday HTTP Client.
66
+ #
67
+ # @param [String] path API path
68
+ #
69
+ # @raise [Faraday::Error] on failure API call
70
+ #
71
+ # @return [Faraday::Response] instance of Faraday::Response class
72
+ def delete(path)
73
+ connection.delete(path)
74
+ rescue Faraday::Error => e
75
+ log_error(e) && raise
76
+ end
77
+
64
78
  private
65
79
 
66
80
  attr_reader :connection, :logger
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'openai/client/utils'
4
+
3
5
  module Openai
4
6
  module Client
5
7
  class Images
@@ -26,7 +28,7 @@ module Openai
26
28
  #
27
29
  # @return [Hash] an edited or extended image
28
30
  def edit(body)
29
- Http.new.multipart_post(EDIT_PATH, upload_io(body, %i[image mask])).body
31
+ Http.new.multipart_post(EDIT_PATH, Utils.upload_io(body, %i[image mask], 'png')).body
30
32
  rescue Faraday::Error
31
33
  nil
32
34
  end
@@ -38,26 +40,10 @@ module Openai
38
40
  #
39
41
  # @return [Hash] a variation of a given image
40
42
  def variations(body)
41
- Http.new.multipart_post(VARIATION_PATH, upload_io(body, %i[image])).body
43
+ Http.new.multipart_post(VARIATION_PATH, Utils.upload_io(body, %i[image], 'png')).body
42
44
  rescue Faraday::Error
43
45
  nil
44
46
  end
45
-
46
- private
47
-
48
- # @api private
49
- # Internal: Creates the open IO object for the uploaded file.
50
- #
51
- # @param [Hash] data request body
52
- # @param [Array] param_names parameter names
53
- #
54
- # @return [Hash] request body
55
- def upload_io(data, param_names)
56
- param_names.each do |param|
57
- data[param] = Faraday::FilePart.new(data[param], 'rb') if data[param]
58
- end
59
- data
60
- end
61
47
  end
62
48
  end
63
49
  end
@@ -20,12 +20,24 @@ module Openai
20
20
  #
21
21
  # @param [String] id model id
22
22
  #
23
- # @return [Hash] found model or nil
23
+ # @return [Hash] found model
24
24
  def find(id)
25
25
  Http.new.get("#{PATH}/#{id}").body
26
26
  rescue Faraday::Error
27
27
  nil
28
28
  end
29
+
30
+ # @api public
31
+ # Public: Makes an API call to delete a model.
32
+ #
33
+ # @param [String] id model id
34
+ #
35
+ # @return [Hash] the model
36
+ def delete(id)
37
+ Http.new.delete("#{PATH}/#{id}").body
38
+ rescue Faraday::Error
39
+ nil
40
+ end
29
41
  end
30
42
  end
31
43
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openai
4
+ module Client
5
+ class Moderations
6
+ PATH = 'moderations'
7
+
8
+ # @api public
9
+ # Public: Makes an API call to create a moderation.
10
+ #
11
+ # @param [Hash] body request body
12
+ #
13
+ # @return [Hash] a moderation
14
+ def create(body)
15
+ Http.new.post(PATH, body).body
16
+ rescue Faraday::Error
17
+ nil
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openai
4
+ module Client
5
+ module Utils
6
+ class << self
7
+ # @api private
8
+ # Internal: Creates the open IO object for the uploaded file.
9
+ #
10
+ # @param [Hash] data request body
11
+ # @param [Array] param_names parameter names
12
+ # @param [String] content_type content type of the file data
13
+ #
14
+ # @return [Hash] request body
15
+ def upload_io(data, param_names, content_type)
16
+ param_names.each do |param|
17
+ data[param] = Faraday::FilePart.new(data[param], content_type) if data[param]
18
+ end
19
+ data
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Openai
4
4
  module Client
5
- VERSION = '0.5.0'
5
+ VERSION = '0.8.0'
6
6
  end
7
7
  end
data/lib/openai/client.rb CHANGED
@@ -13,18 +13,30 @@ require 'openai/client/edits'
13
13
  require 'openai/client/completions'
14
14
  require 'openai/client/images'
15
15
  require 'openai/client/embeddings'
16
+ require 'openai/client/moderations'
17
+ require 'openai/client/files'
18
+ require 'openai/client/fine_tunes'
16
19
 
17
20
  module Openai
18
21
  module Client
19
22
  extend Configurable
20
23
 
21
- ATTRS = ['models', 'edits', 'completions', 'images', 'embeddings'].freeze
24
+ ATTRS = [
25
+ 'models',
26
+ 'edits',
27
+ 'completions',
28
+ 'images',
29
+ 'embeddings',
30
+ 'moderations',
31
+ 'files',
32
+ 'fine_tunes'
33
+ ].freeze
22
34
 
23
35
  class << self
24
36
  ATTRS.each do |attr|
25
37
  define_method(attr) do
26
- instance_variable_get("@#{attr}") || instance_variable_set("@#{attr}",
27
- const_get(attr.capitalize, self).new)
38
+ klass = const_get(attr.split('_').map(&:capitalize).join, self)
39
+ instance_variable_get("@#{attr}") || instance_variable_set("@#{attr}", klass.new)
28
40
  end
29
41
  end
30
42
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openai-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ihor Tykhonenko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-10 00:00:00.000000000 Z
11
+ date: 2023-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -59,9 +59,13 @@ files:
59
59
  - lib/openai/client/configuration.rb
60
60
  - lib/openai/client/edits.rb
61
61
  - lib/openai/client/embeddings.rb
62
+ - lib/openai/client/files.rb
63
+ - lib/openai/client/fine_tunes.rb
62
64
  - lib/openai/client/http.rb
63
65
  - lib/openai/client/images.rb
64
66
  - lib/openai/client/models.rb
67
+ - lib/openai/client/moderations.rb
68
+ - lib/openai/client/utils.rb
65
69
  - lib/openai/client/version.rb
66
70
  - sig/openai/client.rbs
67
71
  homepage: https://github.com/itikhonenko/openai-client