openai-client 0.3.0 → 0.4.1

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: 374775f26834baae35b6799f23ebc4c1f82379292c7b67fb0529a85a3dbee23b
4
- data.tar.gz: 00a67ee196ac3f0b6e52f377605c76273c968d3aec03a47820f75ed6955907eb
3
+ metadata.gz: 3fdb9832b6b6a5b14a1b14537ac74595fa1a85a689f84fc7f2411651812b6131
4
+ data.tar.gz: 57d4465e713d0ff02e7c5b97dcc32b3db7e96085b3850d00a4076eb2de8509ae
5
5
  SHA512:
6
- metadata.gz: ddd4d92869f64c240c45924ea43b6b9acda29ec97f57c5e3f54d5b5602ec4b43e754247b53407758c20fa10abb54a8399324a59e85437d0869bd4f4e6c448a36
7
- data.tar.gz: 24ec9056e95a781ed5d0f6c18c205738a375b3fb990168f41f6a1be6fec34769772403fff93d531b900d67be217a0acc6c6ff84737ef54a47dd4f23956c84354
6
+ metadata.gz: 78fb79a3c56a1ad02ac6574f619dc8c5492b30d167478b5678730768e40b1c91adccebebceb238440a51915337d331472de8de9b711133d4276fd49066a33123
7
+ data.tar.gz: bd23759e786d446ed0e14ad63b7db1e4b438f5b7291cb68ec97b38928e00e3be38e73664909f3c6837c0b7391154f317ef45b23e95628cef5fe35fc97c955742
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- openai-client (0.3.0)
4
+ openai-client (0.4.1)
5
5
  faraday (~> 1.8)
6
6
  faraday_middleware (~> 1.2)
7
7
 
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
- - API key (`access_token`) https://beta.openai.com/account/api-keys.
20
- - Organization ID (if needed) https://beta.openai.com/account/org-settings.
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'
@@ -29,6 +49,7 @@ end
29
49
  ```
30
50
 
31
51
  ## OpenAI Models API
52
+
32
53
  ```ruby
33
54
  # Models
34
55
  Openai::Client.models.list
@@ -36,7 +57,9 @@ Openai::Client.models.list
36
57
  # Find a Model
37
58
  Openai::Client.models.find(model_id)
38
59
  ```
60
+
39
61
  ## OpenAI Completions API
62
+
40
63
  ```ruby
41
64
  request_body = {
42
65
  model: 'text-davinci-003',
@@ -51,9 +74,11 @@ request_body = {
51
74
  }
52
75
  Openai::Client.completions.create(request_body)
53
76
  ```
54
- [Completions request body documentation](https://platform.openai.com/docs/api-reference/completions/create)
77
+
78
+ [Request body documentation](https://platform.openai.com/docs/api-reference/completions/create)
55
79
 
56
80
  ## OpenAI Edits API
81
+
57
82
  ```ruby
58
83
  request_body = {
59
84
  model: 'text-davinci-edit-001',
@@ -62,10 +87,64 @@ request_body = {
62
87
  }
63
88
  Openai::Client.edits.create(request_body)
64
89
  ```
65
- [Edits request body documentation](https://platform.openai.com/docs/api-reference/edits/create)
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)
66
143
 
67
144
  ## Contributing
68
- Bug reports and pull requests are welcome on GitHub at https://github.com/itikhonenko/openai-client.
145
+
146
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/itikhonenko/openai-client>.
69
147
 
70
148
  ## License
149
+
71
150
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -13,7 +13,7 @@ module Openai
13
13
  # @return [Hash] a hash with a completion
14
14
  def create(body)
15
15
  Http.new.post(PATH, body).body
16
- rescue StandardError
16
+ rescue Faraday::Error
17
17
  nil
18
18
  end
19
19
  end
@@ -13,7 +13,7 @@ module Openai
13
13
  # @return [Hash] a hash with edit
14
14
  def create(body)
15
15
  Http.new.post(PATH, body).body
16
- rescue StandardError
16
+ rescue Faraday::Error
17
17
  nil
18
18
  end
19
19
  end
@@ -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
@@ -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 StandardError
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 StandardError
26
+ rescue Faraday::Error
27
27
  nil
28
28
  end
29
29
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Openai
4
4
  module Client
5
- VERSION = '0.3.0'
5
+ VERSION = '0.4.1'
6
6
  end
7
7
  end
data/lib/openai/client.rb CHANGED
@@ -11,12 +11,13 @@ require 'openai/client/http'
11
11
  require 'openai/client/models'
12
12
  require 'openai/client/edits'
13
13
  require 'openai/client/completions'
14
+ require 'openai/client/images'
14
15
 
15
16
  module Openai
16
17
  module Client
17
18
  extend Configurable
18
19
 
19
- ATTRS = ['models', 'edits', 'completions'].freeze
20
+ ATTRS = ['models', 'edits', 'completions', 'images'].freeze
20
21
 
21
22
  class << self
22
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.3.0
4
+ version: 0.4.1
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-07 00:00:00.000000000 Z
11
+ date: 2023-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -59,6 +59,7 @@ files:
59
59
  - lib/openai/client/configuration.rb
60
60
  - lib/openai/client/edits.rb
61
61
  - lib/openai/client/http.rb
62
+ - lib/openai/client/images.rb
62
63
  - lib/openai/client/models.rb
63
64
  - lib/openai/client/version.rb
64
65
  - sig/openai/client.rbs