ruby-openai 1.5.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +6 -7
- data/.rubocop.yml +1 -1
- data/CHANGELOG.md +13 -0
- data/Gemfile +2 -2
- data/Gemfile.lock +14 -12
- data/README.md +23 -38
- data/lib/ruby/openai/client.rb +27 -24
- data/lib/ruby/openai/models.rb +36 -0
- data/lib/ruby/openai/version.rb +1 -1
- data/lib/ruby/openai.rb +1 -0
- data/ruby-openai.gemspec +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f5927989b832799b3620dd5e1d93796d44fd3fe353d2edc2d64a4be2162bc6ff
|
4
|
+
data.tar.gz: 3313d8fa4ce08438fda80850736c96d73397f8f9d7baf72a365dea839c0cd876
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4604610b91ea93c5a8b08886700c656ca3651129542a6c257d5d000fc414f2a47e9ca8e8cadba622fb426a4e969fd0d058a83d857455893d172f3125852efd19
|
7
|
+
data.tar.gz: d8b959447a050615b4cdffd15cb7ac782b43b4d73953e6b0aa687bacf1f5872d4e9546547d4961fd0e1f5b07f40f460ae2cb136f717034e0a9199f4e5822946c
|
data/.circleci/config.yml
CHANGED
@@ -36,10 +36,9 @@ workflows:
|
|
36
36
|
- rubocop
|
37
37
|
- test:
|
38
38
|
matrix:
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
- cimg/ruby:3.1-node
|
39
|
+
parameters:
|
40
|
+
ruby-image:
|
41
|
+
- cimg/ruby:2.6-node
|
42
|
+
- cimg/ruby:2.7-node
|
43
|
+
- cimg/ruby:3.0-node
|
44
|
+
- cimg/ruby:3.1-node
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file.
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
7
|
|
8
|
+
## [2.0.0] - 2022-09-19
|
9
|
+
|
10
|
+
### Removed
|
11
|
+
|
12
|
+
- [BREAKING] Remove support for Ruby 2.5.
|
13
|
+
- [BREAKING] Remove support for passing `query`, `documents` or `file` as top-level parameters to `Client#search`.
|
14
|
+
- Deprecate Client#search endpoint.
|
15
|
+
- Deprecate Client#engines endpoints.
|
16
|
+
|
17
|
+
### Added
|
18
|
+
|
19
|
+
- Add Client#models endpoints to list and query available models.
|
20
|
+
|
8
21
|
## [1.5.0] - 2022-09-18
|
9
22
|
|
10
23
|
### Added
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ruby-openai (
|
4
|
+
ruby-openai (2.0.0)
|
5
5
|
dotenv (>= 2.7.6, < 2.9.0)
|
6
6
|
httparty (>= 0.18.1, < 0.21.0)
|
7
7
|
|
@@ -20,17 +20,18 @@ GEM
|
|
20
20
|
httparty (0.20.0)
|
21
21
|
mime-types (~> 3.0)
|
22
22
|
multi_xml (>= 0.5.2)
|
23
|
+
json (2.6.2)
|
23
24
|
mime-types (3.4.1)
|
24
25
|
mime-types-data (~> 3.2015)
|
25
26
|
mime-types-data (3.2022.0105)
|
26
27
|
multi_xml (0.6.0)
|
27
28
|
parallel (1.22.1)
|
28
|
-
parser (3.1.2.
|
29
|
+
parser (3.1.2.1)
|
29
30
|
ast (~> 2.4.1)
|
30
31
|
public_suffix (4.0.7)
|
31
32
|
rainbow (3.1.1)
|
32
33
|
rake (13.0.6)
|
33
|
-
regexp_parser (2.
|
34
|
+
regexp_parser (2.5.0)
|
34
35
|
rexml (3.2.5)
|
35
36
|
rspec (3.11.0)
|
36
37
|
rspec-core (~> 3.11.0)
|
@@ -45,20 +46,21 @@ GEM
|
|
45
46
|
diff-lcs (>= 1.2.0, < 2.0)
|
46
47
|
rspec-support (~> 3.11.0)
|
47
48
|
rspec-support (3.11.0)
|
48
|
-
rubocop (1.
|
49
|
+
rubocop (1.36.0)
|
50
|
+
json (~> 2.3)
|
49
51
|
parallel (~> 1.10)
|
50
|
-
parser (>= 3.1.
|
52
|
+
parser (>= 3.1.2.1)
|
51
53
|
rainbow (>= 2.2.2, < 4.0)
|
52
54
|
regexp_parser (>= 1.8, < 3.0)
|
53
|
-
rexml
|
54
|
-
rubocop-ast (>= 1.
|
55
|
+
rexml (>= 3.2.5, < 4.0)
|
56
|
+
rubocop-ast (>= 1.20.1, < 2.0)
|
55
57
|
ruby-progressbar (~> 1.7)
|
56
58
|
unicode-display_width (>= 1.4.0, < 3.0)
|
57
|
-
rubocop-ast (1.
|
59
|
+
rubocop-ast (1.21.0)
|
58
60
|
parser (>= 3.1.1.0)
|
59
61
|
ruby-progressbar (1.11.0)
|
60
|
-
unicode-display_width (2.
|
61
|
-
vcr (6.
|
62
|
+
unicode-display_width (2.3.0)
|
63
|
+
vcr (6.1.0)
|
62
64
|
webmock (3.18.1)
|
63
65
|
addressable (>= 2.8.0)
|
64
66
|
crack (>= 0.3.2)
|
@@ -71,9 +73,9 @@ DEPENDENCIES
|
|
71
73
|
byebug (~> 11.1.3)
|
72
74
|
rake (~> 13.0)
|
73
75
|
rspec (~> 3.11)
|
74
|
-
rubocop (~> 1.
|
76
|
+
rubocop (~> 1.36.0)
|
75
77
|
ruby-openai!
|
76
|
-
vcr (~> 6.
|
78
|
+
vcr (~> 6.1.0)
|
77
79
|
webmock (~> 3.18.1)
|
78
80
|
|
79
81
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -14,7 +14,7 @@ Use the [OpenAI GPT-3 API](https://openai.com/blog/openai-api/) with Ruby! 🤖
|
|
14
14
|
Add this line to your application's Gemfile:
|
15
15
|
|
16
16
|
```ruby
|
17
|
-
gem
|
17
|
+
gem "ruby-openai"
|
18
18
|
```
|
19
19
|
|
20
20
|
And then execute:
|
@@ -61,24 +61,36 @@ Alternatively you can pass your key directly to a new client:
|
|
61
61
|
|
62
62
|
#### Examples
|
63
63
|
|
64
|
-
- [GPT-3](https://beta.openai.com/docs/
|
64
|
+
- [GPT-3](https://beta.openai.com/docs/models/gpt-3)
|
65
65
|
- text-ada-001
|
66
66
|
- text-babbage-001
|
67
67
|
- text-curie-001
|
68
68
|
- text-davinci-001
|
69
|
-
- [Codex (private beta)](https://beta.openai.com/docs/
|
69
|
+
- [Codex (private beta)](https://beta.openai.com/docs/models/codex-series-private-beta)
|
70
70
|
- code-davinci-002
|
71
71
|
- code-cushman-001
|
72
|
-
|
73
|
-
|
72
|
+
|
73
|
+
### Models
|
74
|
+
|
75
|
+
There are different models that can be used to generate text. For a full list and to retrieve information about a single models:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
client.models.list
|
79
|
+
client.models.retrieve(id: "text-ada-001")
|
80
|
+
```
|
74
81
|
|
75
82
|
### Completions
|
76
83
|
|
77
84
|
Hit the OpenAI API for a completion:
|
78
85
|
|
79
86
|
```ruby
|
80
|
-
response = client.completions(
|
81
|
-
|
87
|
+
response = client.completions(
|
88
|
+
parameters: {
|
89
|
+
model: "text-davinci-001",
|
90
|
+
prompt: "Once upon a time",
|
91
|
+
max_tokens: 5
|
92
|
+
})
|
93
|
+
puts response["choices"].map { |c| c["text"] }
|
82
94
|
=> [", there lived a great"]
|
83
95
|
```
|
84
96
|
|
@@ -104,9 +116,9 @@ You can use the embeddings endpoint to get a vector of numbers representing an i
|
|
104
116
|
|
105
117
|
```ruby
|
106
118
|
client.embeddings(
|
107
|
-
engine: "babbage-similarity",
|
108
119
|
parameters: {
|
109
|
-
|
120
|
+
model: "babbage-similarity",
|
121
|
+
input: "The food was delicious and the waiter..."
|
110
122
|
}
|
111
123
|
)
|
112
124
|
```
|
@@ -123,7 +135,7 @@ Put your data in a `.jsonl` file like this:
|
|
123
135
|
and pass the path to `client.files.upload` to upload it to OpenAI, and then interact with it:
|
124
136
|
|
125
137
|
```ruby
|
126
|
-
client.files.upload(parameters: { file:
|
138
|
+
client.files.upload(parameters: { file: "path/to/puppy.jsonl", purpose: "search" })
|
127
139
|
client.files.list
|
128
140
|
client.files.retrieve(id: 123)
|
129
141
|
client.files.delete(id: 123)
|
@@ -141,7 +153,7 @@ Put your fine-tuning data in a `.jsonl` file like this:
|
|
141
153
|
and pass the path to `client.files.upload` to upload it to OpenAI and get its ID:
|
142
154
|
|
143
155
|
```ruby
|
144
|
-
response = client.files.upload(parameters: { file:
|
156
|
+
response = client.files.upload(parameters: { file: "path/to/sentiment.jsonl", purpose: "fine-tune" })
|
145
157
|
file_id = JSON.parse(response.body)["id"]
|
146
158
|
```
|
147
159
|
|
@@ -182,8 +194,6 @@ This fine-tuned model name can then be used in classifications:
|
|
182
194
|
JSON.parse(response.body)["choices"].map { |c| c["text"] }
|
183
195
|
```
|
184
196
|
|
185
|
-
Do not pass the engine parameter when using a fine-tuned model.
|
186
|
-
|
187
197
|
### Moderations
|
188
198
|
|
189
199
|
Pass a string to check if it violates OpenAI's Content Policy:
|
@@ -194,22 +204,6 @@ Pass a string to check if it violates OpenAI's Content Policy:
|
|
194
204
|
=> 5.505014632944949e-05
|
195
205
|
```
|
196
206
|
|
197
|
-
### Searches
|
198
|
-
|
199
|
-
Pass documents and a query string to get semantic search scores against each document:
|
200
|
-
|
201
|
-
```ruby
|
202
|
-
response = client.search(engine: "text-ada-001", parameters: { documents: %w[washington hospital school], query: "president" })
|
203
|
-
puts response["data"].map { |d| d["score"] }
|
204
|
-
=> [202.0, 48.052, 19.247]
|
205
|
-
```
|
206
|
-
|
207
|
-
You can alternatively search using the ID of a file you've uploaded:
|
208
|
-
|
209
|
-
```ruby
|
210
|
-
client.search(engine: "text-ada-001", parameters: { file: "abc123", query: "happy" })
|
211
|
-
```
|
212
|
-
|
213
207
|
### Classifications
|
214
208
|
|
215
209
|
Pass examples and a query to predict the most likely labels:
|
@@ -262,15 +256,6 @@ Or use the ID of a file you've uploaded:
|
|
262
256
|
})
|
263
257
|
```
|
264
258
|
|
265
|
-
### Engines
|
266
|
-
|
267
|
-
There are different engines that can be used to generate text. For a full list and to retrieve information about a single engine:
|
268
|
-
|
269
|
-
```ruby
|
270
|
-
client.engines.list
|
271
|
-
client.engines.retrieve(id: 'text-ada-001')
|
272
|
-
```
|
273
|
-
|
274
259
|
## Development
|
275
260
|
|
276
261
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/ruby/openai/client.rb
CHANGED
@@ -16,22 +16,25 @@ module OpenAI
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def completions(engine: nil, version: default_version, parameters: {})
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
post(url: "/#{version}/completions", parameters: parameters)
|
23
|
-
end
|
19
|
+
parameters = deprecate_engine(engine: engine, method: "completions", parameters: parameters)
|
20
|
+
|
21
|
+
post(url: "/#{version}/completions", parameters: parameters)
|
24
22
|
end
|
25
23
|
|
26
24
|
def edits(version: default_version, parameters: {})
|
27
25
|
post(url: "/#{version}/edits", parameters: parameters)
|
28
26
|
end
|
29
27
|
|
30
|
-
def embeddings(engine
|
31
|
-
|
28
|
+
def embeddings(engine: nil, version: default_version, parameters: {})
|
29
|
+
parameters = deprecate_engine(engine: engine, method: "embeddings", parameters: parameters)
|
30
|
+
|
31
|
+
post(url: "/#{version}/embeddings", parameters: parameters)
|
32
32
|
end
|
33
33
|
|
34
34
|
def engines
|
35
|
+
warn "[DEPRECATION WARNING] [ruby-openai] `Client#engines` is deprecated and will
|
36
|
+
be removed from ruby-openai v3.0. Use `Client#models` instead."
|
37
|
+
|
35
38
|
@engines ||= OpenAI::Engines.new(access_token: @access_token)
|
36
39
|
end
|
37
40
|
|
@@ -43,35 +46,35 @@ module OpenAI
|
|
43
46
|
@finetunes ||= OpenAI::Finetunes.new(access_token: @access_token)
|
44
47
|
end
|
45
48
|
|
49
|
+
def models
|
50
|
+
@models ||= OpenAI::Models.new(access_token: @access_token)
|
51
|
+
end
|
52
|
+
|
46
53
|
def moderations(version: default_version, parameters: {})
|
47
54
|
post(url: "/#{version}/moderations", parameters: parameters)
|
48
55
|
end
|
49
56
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
57
|
+
def search(engine:, version: default_version, parameters: {})
|
58
|
+
warn "[DEPRECATION WARNING] [ruby-openai] `Client#search` is deprecated and will
|
59
|
+
be removed from the OpenAI API on 3 December 2022 and from ruby-openai v3.0.
|
60
|
+
More information: https://help.openai.com/en/articles/6272952-search-transition-guide"
|
54
61
|
|
55
62
|
post(url: "/#{version}/engines/#{engine}/search", parameters: parameters)
|
56
63
|
end
|
57
|
-
# rubocop:enable Layout/LineLength
|
58
|
-
# rubocop:enable Metrics/ParameterLists
|
59
64
|
|
60
65
|
private
|
61
66
|
|
62
|
-
|
63
|
-
|
64
|
-
warn "[DEPRECATION] Passing `query`, `documents` or `file` directly to `Client#search` is deprecated and will be removed in a future version of ruby-openai.
|
65
|
-
Please nest these terms within `parameters` instead, like this:
|
66
|
-
client.search(engine: 'davinci', parameters: { query: 'president', documents: %w[washington hospital school] })
|
67
|
-
"
|
67
|
+
def deprecate_engine(engine:, method:, parameters:)
|
68
|
+
return parameters unless engine
|
68
69
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
70
|
+
parameters = { model: engine }.merge(parameters)
|
71
|
+
|
72
|
+
warn "[DEPRECATION WARNING] [ruby-openai] Passing `engine` directly to `Client##{method}` is
|
73
|
+
deprecated and will be removed in ruby-openai 3.0. Pass `model` within `parameters` instead:
|
74
|
+
client.completions(parameters: { #{parameters.map { |k, v| "#{k}: \"#{v}\"" }.join(', ')} })"
|
75
|
+
|
76
|
+
parameters
|
73
77
|
end
|
74
|
-
# rubocop:enable Layout/LineLength
|
75
78
|
|
76
79
|
def default_version
|
77
80
|
"v1".freeze
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module OpenAI
|
2
|
+
class Models
|
3
|
+
include HTTParty
|
4
|
+
base_uri "https://api.openai.com"
|
5
|
+
|
6
|
+
def initialize(access_token: nil)
|
7
|
+
@access_token = access_token || ENV.fetch("OPENAI_ACCESS_TOKEN")
|
8
|
+
end
|
9
|
+
|
10
|
+
def list(version: default_version)
|
11
|
+
self.class.get(
|
12
|
+
"/#{version}/models",
|
13
|
+
headers: {
|
14
|
+
"Content-Type" => "application/json",
|
15
|
+
"Authorization" => "Bearer #{@access_token}"
|
16
|
+
}
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
def retrieve(id:, version: default_version)
|
21
|
+
self.class.get(
|
22
|
+
"/#{version}/models/#{id}",
|
23
|
+
headers: {
|
24
|
+
"Content-Type" => "application/json",
|
25
|
+
"Authorization" => "Bearer #{@access_token}"
|
26
|
+
}
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def default_version
|
33
|
+
"v1".freeze
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/ruby/openai/version.rb
CHANGED
data/lib/ruby/openai.rb
CHANGED
data/ruby-openai.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.summary = "A Ruby gem for the OpenAI GPT-3 API"
|
10
10
|
spec.homepage = "https://github.com/alexrudall/ruby-openai"
|
11
11
|
spec.license = "MIT"
|
12
|
-
spec.required_ruby_version = Gem::Requirement.new(">= 2.
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
|
13
13
|
|
14
14
|
spec.metadata["homepage_uri"] = spec.homepage
|
15
15
|
spec.metadata["source_code_uri"] = "https://github.com/alexrudall/ruby-openai"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-openai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-09-
|
11
|
+
date: 2022-09-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dotenv
|
@@ -80,6 +80,7 @@ files:
|
|
80
80
|
- lib/ruby/openai/engines.rb
|
81
81
|
- lib/ruby/openai/files.rb
|
82
82
|
- lib/ruby/openai/finetunes.rb
|
83
|
+
- lib/ruby/openai/models.rb
|
83
84
|
- lib/ruby/openai/version.rb
|
84
85
|
- pull_request_template.md
|
85
86
|
- ruby-openai.gemspec
|
@@ -99,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
100
|
requirements:
|
100
101
|
- - ">="
|
101
102
|
- !ruby/object:Gem::Version
|
102
|
-
version: 2.
|
103
|
+
version: 2.6.0
|
103
104
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
105
|
requirements:
|
105
106
|
- - ">="
|