openai-client 0.5.0 → 0.7.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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +80 -6
- data/lib/openai/client/files.rb +69 -0
- data/lib/openai/client/http.rb +14 -0
- data/lib/openai/client/images.rb +4 -18
- data/lib/openai/client/moderations.rb +21 -0
- data/lib/openai/client/utils.rb +24 -0
- data/lib/openai/client/version.rb +1 -1
- data/lib/openai/client.rb +3 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa13b10a22961929472de3fc7cb1ebedc43c5d46d2e1b221d3e7ac39293af99e
|
4
|
+
data.tar.gz: d848b51aa150fe3b33f755cd774f8e959550bfeef82daf475976756fe0b3a12a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30b8d9843d40343ba72e06450c7262238a2d2d67bfffe6d59c55c61e4d20535985c513bc8b692bab193b5df3e2427b82ede2f441e50834d3fb15bf5f61d2d272
|
7
|
+
data.tar.gz: '0817ca7dce3899b5e43a5a87fad228c370051475ccd41c475d5c5e32560cfa3cfca318006f59a8fb611be722cf08c76a82cb8184407c53d2e92999e6d38bed47'
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -12,6 +12,13 @@ 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)
|
15
22
|
|
16
23
|
## Installation
|
17
24
|
|
@@ -74,7 +81,7 @@ request_body = {
|
|
74
81
|
Openai::Client.completions.create(request_body)
|
75
82
|
```
|
76
83
|
|
77
|
-
[
|
84
|
+
[API documentation](https://platform.openai.com/docs/api-reference/completions/create)
|
78
85
|
|
79
86
|
## OpenAI Edits API
|
80
87
|
|
@@ -87,7 +94,7 @@ request_body = {
|
|
87
94
|
Openai::Client.edits.create(request_body)
|
88
95
|
```
|
89
96
|
|
90
|
-
[
|
97
|
+
[API documentation](https://platform.openai.com/docs/api-reference/edits/create)
|
91
98
|
|
92
99
|
## OpenAI Image API
|
93
100
|
|
@@ -103,7 +110,7 @@ request_body = {
|
|
103
110
|
response = Openai::Client.images.create(request_body)
|
104
111
|
```
|
105
112
|
|
106
|
-
[
|
113
|
+
[API documentation](https://platform.openai.com/docs/api-reference/images/create)
|
107
114
|
|
108
115
|
### Create an Image Edit
|
109
116
|
|
@@ -122,7 +129,7 @@ response = Openai::Client.images.edit(request_body)
|
|
122
129
|
- `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
130
|
- `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
131
|
|
125
|
-
[
|
132
|
+
[API documentation](https://platform.openai.com/docs/api-reference/images/create-edit)
|
126
133
|
|
127
134
|
### Create an Image Variation
|
128
135
|
|
@@ -138,7 +145,7 @@ response = Openai::Client.images.variations(request_body)
|
|
138
145
|
|
139
146
|
- `image` - must be a valid PNG file, less than 4MB, and square.
|
140
147
|
|
141
|
-
[
|
148
|
+
[API documentation](https://platform.openai.com/docs/api-reference/images/create-variation)
|
142
149
|
|
143
150
|
## OpenAI Embeddings API
|
144
151
|
|
@@ -150,7 +157,74 @@ request_body = {
|
|
150
157
|
Openai::Client.embeddings.create(request_body)
|
151
158
|
```
|
152
159
|
|
153
|
-
[
|
160
|
+
[API documentation](https://platform.openai.com/docs/api-reference/embeddings/create)
|
161
|
+
|
162
|
+
## OpenAI Moderations API
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
request_body = {
|
166
|
+
model: 'text-moderation-latest', # text-moderation-stable or text-moderation-latest
|
167
|
+
input: 'I want to kill them.'
|
168
|
+
}
|
169
|
+
Openai::Client.moderations.create(request_body)
|
170
|
+
```
|
171
|
+
|
172
|
+
[API documentation](https://platform.openai.com/docs/api-reference/moderations/create)
|
173
|
+
|
174
|
+
## OpenAI Files API
|
175
|
+
|
176
|
+
### List Files
|
177
|
+
|
178
|
+
```ruby
|
179
|
+
Openai::Client.files.list
|
180
|
+
```
|
181
|
+
|
182
|
+
[API documentation](https://platform.openai.com/docs/api-reference/files/list)
|
183
|
+
|
184
|
+
### Find File
|
185
|
+
|
186
|
+
```ruby
|
187
|
+
Openai::Client.files.find(file_id)
|
188
|
+
```
|
189
|
+
|
190
|
+
[API documentation](https://platform.openai.com/docs/api-reference/files/retrieve)
|
191
|
+
|
192
|
+
### Find File Content
|
193
|
+
|
194
|
+
```ruby
|
195
|
+
Openai::Client.files.find_content(file_id)
|
196
|
+
```
|
197
|
+
|
198
|
+
[API documentation](https://platform.openai.com/docs/api-reference/files/retrieve-content)
|
199
|
+
|
200
|
+
### Upload File
|
201
|
+
|
202
|
+
```ruby
|
203
|
+
request_body = {
|
204
|
+
file: '/absolute/path/to/file.jsonl',
|
205
|
+
purpose: 'fine-tune'
|
206
|
+
}
|
207
|
+
Openai::Client.files.upload(request_body)
|
208
|
+
```
|
209
|
+
> The file format must be jsonl, where each line contains the prompt and completion properties.
|
210
|
+
|
211
|
+
Example (file.jsonl):
|
212
|
+
|
213
|
+
```json
|
214
|
+
{"prompt": "<prompt text>", "completion": "<ideal generated text>"}
|
215
|
+
{"prompt": "<prompt text>", "completion": "<ideal generated text>"}
|
216
|
+
...
|
217
|
+
```
|
218
|
+
|
219
|
+
[API documentation](https://platform.openai.com/docs/api-reference/files/upload)
|
220
|
+
|
221
|
+
### Delete File
|
222
|
+
|
223
|
+
```ruby
|
224
|
+
Openai::Client.files.delete(file_id)
|
225
|
+
```
|
226
|
+
|
227
|
+
[API documentation](https://platform.openai.com/docs/api-reference/files/delete)
|
154
228
|
|
155
229
|
## Contributing
|
156
230
|
|
@@ -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 or nil
|
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 or nil
|
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
|
data/lib/openai/client/http.rb
CHANGED
@@ -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
|
data/lib/openai/client/images.rb
CHANGED
@@ -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
|
@@ -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
|
data/lib/openai/client.rb
CHANGED
@@ -13,12 +13,14 @@ 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'
|
16
18
|
|
17
19
|
module Openai
|
18
20
|
module Client
|
19
21
|
extend Configurable
|
20
22
|
|
21
|
-
ATTRS = ['models', 'edits', 'completions', 'images', 'embeddings'].freeze
|
23
|
+
ATTRS = ['models', 'edits', 'completions', 'images', 'embeddings', 'moderations', 'files'].freeze
|
22
24
|
|
23
25
|
class << self
|
24
26
|
ATTRS.each do |attr|
|
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.
|
4
|
+
version: 0.7.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-
|
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,12 @@ 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
|
62
63
|
- lib/openai/client/http.rb
|
63
64
|
- lib/openai/client/images.rb
|
64
65
|
- lib/openai/client/models.rb
|
66
|
+
- lib/openai/client/moderations.rb
|
67
|
+
- lib/openai/client/utils.rb
|
65
68
|
- lib/openai/client/version.rb
|
66
69
|
- sig/openai/client.rbs
|
67
70
|
homepage: https://github.com/itikhonenko/openai-client
|