openai-client 0.2.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +113 -4
- data/lib/openai/client/completions.rb +21 -0
- data/lib/openai/client/edits.rb +21 -0
- data/lib/openai/client/http.rb +19 -0
- data/lib/openai/client/images.rb +63 -0
- data/lib/openai/client/models.rb +2 -2
- data/lib/openai/client/version.rb +1 -1
- data/lib/openai/client.rb +4 -1
- metadata +5 -3
- data/openai-client-0.1.0.gem +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc51a3de3eacf9128058627f6775f3258f6b400dc34064a4194bde9f7da0f477
|
4
|
+
data.tar.gz: f78be5f020154fe2d316b1438eb14e2b11fab21b6001df78bd231ecc58d32269
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c8ac94f2fadb74327367411a8998117a794a2f5aa2cb99e87806403224bd98b35486f5244a3e9d23c8290746d141f4232079372cf667abfd92e66e86651cd2d3
|
7
|
+
data.tar.gz: cd40f73c1a77570f13243213a8b67fcb05ca2f4c196c9501fc9fc498b105f15abe61629a854dfe6a8302c9eeceaab75e32d9e877a632e91580a9f0d4c12b266e
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,23 +1,43 @@
|
|
1
1
|
# Openai::Client
|
2
|
+
|
2
3
|
This gem is a wrapper for calling the OpenAI and GPT-3 APIs.
|
3
4
|
|
5
|
+
* [Installation](#installation)
|
6
|
+
* [Usage](#usage)
|
7
|
+
* [OpenAI Models API](#openai-models-api)
|
8
|
+
* [OpenAI Completions API](#openai-completions-api)
|
9
|
+
* [OpenAI Edits API](#openai-edits-api)
|
10
|
+
* [OpenAI Image API](#openai-image-api)
|
11
|
+
* [Create an Image](#create-an-image)
|
12
|
+
* [Create an Image Edit](#create-an-image-edit)
|
13
|
+
* [Create an Image Variation](#create-an-image-variation)
|
14
|
+
|
15
|
+
|
16
|
+
|
4
17
|
## Installation
|
18
|
+
|
5
19
|
Add this line to your application's Gemfile:
|
20
|
+
|
6
21
|
```ruby
|
7
22
|
gem 'openai-client'
|
8
23
|
```
|
24
|
+
|
9
25
|
And then execute:
|
26
|
+
|
10
27
|
```bash
|
11
28
|
bundle
|
12
29
|
```
|
30
|
+
|
13
31
|
Or install it yourself as:
|
32
|
+
|
14
33
|
```bash
|
15
34
|
gem install openai-client
|
16
35
|
```
|
17
36
|
|
18
37
|
## Usage
|
19
|
-
|
20
|
-
-
|
38
|
+
|
39
|
+
- API key (`access_token`) <https://beta.openai.com/account/api-keys>.
|
40
|
+
- Organization ID (if needed) <https://beta.openai.com/account/org-settings>.
|
21
41
|
|
22
42
|
```ruby
|
23
43
|
require 'openai-client'
|
@@ -26,7 +46,11 @@ Openai::Client.configure do |c|
|
|
26
46
|
c.access_token = 'access_token'
|
27
47
|
c.organization_id = 'organization_id' # optional
|
28
48
|
end
|
49
|
+
```
|
50
|
+
|
51
|
+
## OpenAI Models API
|
29
52
|
|
53
|
+
```ruby
|
30
54
|
# Models
|
31
55
|
Openai::Client.models.list
|
32
56
|
|
@@ -34,8 +58,93 @@ Openai::Client.models.list
|
|
34
58
|
Openai::Client.models.find(model_id)
|
35
59
|
```
|
36
60
|
|
61
|
+
## OpenAI Completions API
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
request_body = {
|
65
|
+
model: 'text-davinci-003',
|
66
|
+
prompt: 'Say this is a test',
|
67
|
+
max_tokens: 7,
|
68
|
+
temperature: 0,
|
69
|
+
top_p: 1,
|
70
|
+
n: 1,
|
71
|
+
stream: false,
|
72
|
+
logprobs: nil,
|
73
|
+
stop: "\n"
|
74
|
+
}
|
75
|
+
Openai::Client.completions.create(request_body)
|
76
|
+
```
|
77
|
+
|
78
|
+
[Request body documentation](https://platform.openai.com/docs/api-reference/completions/create)
|
79
|
+
|
80
|
+
## OpenAI Edits API
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
request_body = {
|
84
|
+
model: 'text-davinci-edit-001',
|
85
|
+
input: 'What day of the wek is it?',
|
86
|
+
instruction: 'Fix the spelling mistakes'
|
87
|
+
}
|
88
|
+
Openai::Client.edits.create(request_body)
|
89
|
+
```
|
90
|
+
|
91
|
+
[Request body documentation](https://platform.openai.com/docs/api-reference/edits/create)
|
92
|
+
|
93
|
+
## OpenAI Image API
|
94
|
+
|
95
|
+
### Create an Image
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
request_body = {
|
99
|
+
prompt: 'A cute baby sea otter',
|
100
|
+
n: 1, # between 1 and 10
|
101
|
+
size: '1024x1024', # 256x256, 512x512, or 1024x1024
|
102
|
+
response_format: 'url' # url or b64_json
|
103
|
+
}
|
104
|
+
response = Openai::Client.images.create(request_body)
|
105
|
+
```
|
106
|
+
|
107
|
+
[Request body documentation](https://platform.openai.com/docs/api-reference/images/create)
|
108
|
+
|
109
|
+
### Create an Image Edit
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
request_body = {
|
113
|
+
image: '/absolute/path/to/image/you/want/to/change/img.png'
|
114
|
+
mask: '/absolute/path/to/mask.png'
|
115
|
+
prompt: 'A cute baby sea otter wearing a beret',
|
116
|
+
n: 1, # between 1 and 10
|
117
|
+
size: '1024x1024', # 256x256, 512x512, or 1024x1024
|
118
|
+
response_format: 'url' # url or b64_json
|
119
|
+
}
|
120
|
+
response = Openai::Client.images.edit(request_body)
|
121
|
+
```
|
122
|
+
|
123
|
+
- `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.
|
124
|
+
- `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.
|
125
|
+
|
126
|
+
[Request body documentation](https://platform.openai.com/docs/api-reference/images/create-edit)
|
127
|
+
|
128
|
+
### Create an Image Variation
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
request_body = {
|
132
|
+
image: '/absolute/path/to/image.png'
|
133
|
+
n: 1, # between 1 and 10
|
134
|
+
size: '1024x1024', # 256x256, 512x512, or 1024x1024
|
135
|
+
response_format: 'url' # url or b64_json
|
136
|
+
}
|
137
|
+
response = Openai::Client.images.variations(request_body)
|
138
|
+
```
|
139
|
+
|
140
|
+
- `image` - must be a valid PNG file, less than 4MB, and square.
|
141
|
+
|
142
|
+
[Request body documentation](https://platform.openai.com/docs/api-reference/images/create-variation)
|
143
|
+
|
37
144
|
## Contributing
|
38
|
-
|
145
|
+
|
146
|
+
Bug reports and pull requests are welcome on GitHub at <https://github.com/itikhonenko/openai-client>.
|
39
147
|
|
40
148
|
## License
|
41
|
-
|
149
|
+
|
150
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Openai
|
4
|
+
module Client
|
5
|
+
class Completions
|
6
|
+
PATH = 'completions'
|
7
|
+
|
8
|
+
# @api public
|
9
|
+
# Public: Makes an API call to create a completion.
|
10
|
+
#
|
11
|
+
# @param [Hash] body request body
|
12
|
+
#
|
13
|
+
# @return [Hash] a hash with a completion
|
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,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Openai
|
4
|
+
module Client
|
5
|
+
class Edits
|
6
|
+
PATH = 'edits'
|
7
|
+
|
8
|
+
# @api public
|
9
|
+
# Public: Makes an API call to create edit.
|
10
|
+
#
|
11
|
+
# @param [Hash] body request body
|
12
|
+
#
|
13
|
+
# @return [Hash] a hash with edit
|
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
|
data/lib/openai/client/http.rb
CHANGED
@@ -8,6 +8,7 @@ module Openai
|
|
8
8
|
conn.response :json
|
9
9
|
conn.response :raise_error
|
10
10
|
conn.request :json
|
11
|
+
conn.request :multipart
|
11
12
|
end
|
12
13
|
@logger = Openai::Client.configuration.logger
|
13
14
|
end
|
@@ -42,6 +43,24 @@ module Openai
|
|
42
43
|
log_error(e) && raise
|
43
44
|
end
|
44
45
|
|
46
|
+
# @api public
|
47
|
+
# Public: Makes a multipart request using the Faraday HTTP Client.
|
48
|
+
#
|
49
|
+
# @param [String] path API path
|
50
|
+
# @param [Hash] body request body
|
51
|
+
#
|
52
|
+
# @raise [Faraday::Error] on failure API call
|
53
|
+
#
|
54
|
+
# @return [Faraday::Response] instance of Faraday::Response class
|
55
|
+
def multipart_post(path, body = {})
|
56
|
+
connection.post(path, body) do |request|
|
57
|
+
request.headers['Content-Type'] = 'multipart/form-data'
|
58
|
+
request.options.timeout = 300
|
59
|
+
end
|
60
|
+
rescue Faraday::Error => e
|
61
|
+
log_error(e) && raise
|
62
|
+
end
|
63
|
+
|
45
64
|
private
|
46
65
|
|
47
66
|
attr_reader :connection, :logger
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Openai
|
4
|
+
module Client
|
5
|
+
class Images
|
6
|
+
CREATE_PATH = 'images/generations'
|
7
|
+
EDIT_PATH = 'images/edits'
|
8
|
+
VARIATION_PATH = 'images/variations'
|
9
|
+
|
10
|
+
# @api public
|
11
|
+
# Public: Makes an API call to create an image given a prompt.
|
12
|
+
#
|
13
|
+
# @param [Hash] body request body
|
14
|
+
#
|
15
|
+
# @return [Hash] an image
|
16
|
+
def create(body)
|
17
|
+
Http.new.post(CREATE_PATH, body).body
|
18
|
+
rescue Faraday::Error
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
|
22
|
+
# @api public
|
23
|
+
# Public: Makes an API call to create an edited or extended image given an original image and a prompt.
|
24
|
+
#
|
25
|
+
# @param [Hash] body request body
|
26
|
+
#
|
27
|
+
# @return [Hash] an edited or extended image
|
28
|
+
def edit(body)
|
29
|
+
Http.new.multipart_post(EDIT_PATH, upload_io(body, %i[image mask])).body
|
30
|
+
rescue Faraday::Error
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
|
34
|
+
# @api public
|
35
|
+
# Public: Makes an API call to creates a variation of a given image.
|
36
|
+
#
|
37
|
+
# @param [Hash] body request body
|
38
|
+
#
|
39
|
+
# @return [Hash] a variation of a given image
|
40
|
+
def variations(body)
|
41
|
+
Http.new.multipart_post(VARIATION_PATH, upload_io(body, %i[image])).body
|
42
|
+
rescue Faraday::Error
|
43
|
+
nil
|
44
|
+
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
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/lib/openai/client/models.rb
CHANGED
@@ -11,7 +11,7 @@ module Openai
|
|
11
11
|
# @return [Hash] a hash with models
|
12
12
|
def list
|
13
13
|
Http.new.get(PATH).body
|
14
|
-
rescue
|
14
|
+
rescue Faraday::Error
|
15
15
|
nil
|
16
16
|
end
|
17
17
|
|
@@ -23,7 +23,7 @@ module Openai
|
|
23
23
|
# @return [Hash] found model or nil
|
24
24
|
def find(id)
|
25
25
|
Http.new.get("#{PATH}/#{id}").body
|
26
|
-
rescue
|
26
|
+
rescue Faraday::Error
|
27
27
|
nil
|
28
28
|
end
|
29
29
|
end
|
data/lib/openai/client.rb
CHANGED
@@ -9,12 +9,15 @@ require 'openai/client/configuration'
|
|
9
9
|
require 'openai/client/configurable'
|
10
10
|
require 'openai/client/http'
|
11
11
|
require 'openai/client/models'
|
12
|
+
require 'openai/client/edits'
|
13
|
+
require 'openai/client/completions'
|
14
|
+
require 'openai/client/images'
|
12
15
|
|
13
16
|
module Openai
|
14
17
|
module Client
|
15
18
|
extend Configurable
|
16
19
|
|
17
|
-
ATTRS = ['models'].freeze
|
20
|
+
ATTRS = ['models', 'edits', 'completions', 'images'].freeze
|
18
21
|
|
19
22
|
class << self
|
20
23
|
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.4.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-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -54,12 +54,14 @@ files:
|
|
54
54
|
- README.md
|
55
55
|
- Rakefile
|
56
56
|
- lib/openai/client.rb
|
57
|
+
- lib/openai/client/completions.rb
|
57
58
|
- lib/openai/client/configurable.rb
|
58
59
|
- lib/openai/client/configuration.rb
|
60
|
+
- lib/openai/client/edits.rb
|
59
61
|
- lib/openai/client/http.rb
|
62
|
+
- lib/openai/client/images.rb
|
60
63
|
- lib/openai/client/models.rb
|
61
64
|
- lib/openai/client/version.rb
|
62
|
-
- openai-client-0.1.0.gem
|
63
65
|
- sig/openai/client.rbs
|
64
66
|
homepage: https://github.com/itikhonenko/openai-client
|
65
67
|
licenses:
|
data/openai-client-0.1.0.gem
DELETED
Binary file
|