ruby-openai 1.2.0 → 1.3.1
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 +9 -3
- data/.github/dependabot.yml +15 -0
- data/.rubocop.yml +1 -1
- data/CHANGELOG.md +30 -0
- data/Gemfile +2 -2
- data/Gemfile.lock +13 -13
- data/README.md +31 -4
- data/lib/ruby/openai/client.rb +39 -25
- data/lib/ruby/openai/files.rb +10 -0
- data/lib/ruby/openai/version.rb +1 -1
- 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: 2b8fe14b8e08b05f4743569e5c94f92552032802bf0eb4fd39fd8168ea6e1105
|
4
|
+
data.tar.gz: 70933cec4140ffe73c52e0bc25e5afc2bb65d4348b1c8138f95acc8b7ccdb500
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 58572f5ea0262e16d9b6a916e5a1ae04e786804cdb04771e443ce8c6010b4d6ba2956e1cb5c82a03d743d83e8511f91e9510b6ac5b36ab257435c5cd02dc41f2
|
7
|
+
data.tar.gz: b655c9054c7d89c8dfb8b1f364acc7e70a8aece46bdaf4c365a693d81dc97db83f8e6fe325e8e7eabdec12c079aca9c12e402ac909fe485e721cf8613d46f403
|
data/.circleci/config.yml
CHANGED
@@ -8,7 +8,7 @@ jobs:
|
|
8
8
|
rubocop:
|
9
9
|
parallelism: 1
|
10
10
|
docker:
|
11
|
-
- image: cimg/ruby:
|
11
|
+
- image: cimg/ruby:3.0-node
|
12
12
|
steps:
|
13
13
|
- checkout
|
14
14
|
- ruby/install-deps
|
@@ -16,9 +16,12 @@ jobs:
|
|
16
16
|
name: Run Rubocop
|
17
17
|
command: bundle exec rubocop
|
18
18
|
test:
|
19
|
+
parameters:
|
20
|
+
ruby-image:
|
21
|
+
type: string
|
19
22
|
parallelism: 1
|
20
23
|
docker:
|
21
|
-
- image:
|
24
|
+
- image: << parameters.ruby-image >>
|
22
25
|
steps:
|
23
26
|
- checkout
|
24
27
|
- ruby/install-deps
|
@@ -31,4 +34,7 @@ workflows:
|
|
31
34
|
checks:
|
32
35
|
jobs:
|
33
36
|
- rubocop
|
34
|
-
- test
|
37
|
+
- test:
|
38
|
+
matrix:
|
39
|
+
parameters:
|
40
|
+
ruby-image: ['cimg/ruby:2.5-node', 'cimg/ruby:2.6-node', 'cimg/ruby:2.7-node', 'cimg/ruby:3.0-node']
|
@@ -0,0 +1,15 @@
|
|
1
|
+
version: 2
|
2
|
+
updates:
|
3
|
+
- package-ecosystem: bundler
|
4
|
+
directory: "/"
|
5
|
+
schedule:
|
6
|
+
interval: daily
|
7
|
+
open-pull-requests-limit: 10
|
8
|
+
ignore:
|
9
|
+
- dependency-name: webmock
|
10
|
+
versions:
|
11
|
+
- 3.11.1
|
12
|
+
- 3.11.3
|
13
|
+
- dependency-name: rspec
|
14
|
+
versions:
|
15
|
+
- 3.10.0
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -5,6 +5,36 @@ 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
|
+
## [1.3.1] - 2021-07-14
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
|
12
|
+
- Add backwards compatibility from Ruby 2.5+.
|
13
|
+
|
14
|
+
## [1.3.0] - 2021-04-18
|
15
|
+
|
16
|
+
### Added
|
17
|
+
|
18
|
+
- Add Client#classifications to predict the most likely labels based on examples or a file.
|
19
|
+
|
20
|
+
### Fixed
|
21
|
+
|
22
|
+
- Fixed Files#upload which was previously broken by the validation code!
|
23
|
+
|
24
|
+
## [1.2.2] - 2021-04-18
|
25
|
+
|
26
|
+
### Changed
|
27
|
+
|
28
|
+
- Add Client#search(parameters:) to allow passing `max_rerank` or `return_metadata`.
|
29
|
+
- Deprecate Client#search with query, file or document parameters at the top level.
|
30
|
+
- Thanks [@stevegeek](https://github.com/stevegeek) for pointing this issue out!
|
31
|
+
|
32
|
+
## [1.2.1] - 2021-04-11
|
33
|
+
|
34
|
+
### Added
|
35
|
+
|
36
|
+
- Add validation of JSONL files to make it easier to debug during upload.
|
37
|
+
|
8
38
|
## [1.2.0] - 2021-04-08
|
9
39
|
|
10
40
|
### Added
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ruby-openai (1.
|
4
|
+
ruby-openai (1.3.1)
|
5
5
|
dotenv (~> 2.7.6)
|
6
6
|
httparty (~> 0.18.1)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
addressable (2.
|
11
|
+
addressable (2.8.0)
|
12
12
|
public_suffix (>= 2.0.2, < 5.0)
|
13
13
|
ast (2.4.2)
|
14
14
|
byebug (11.1.3)
|
@@ -22,16 +22,16 @@ GEM
|
|
22
22
|
multi_xml (>= 0.5.2)
|
23
23
|
mime-types (3.3.1)
|
24
24
|
mime-types-data (~> 3.2015)
|
25
|
-
mime-types-data (3.
|
25
|
+
mime-types-data (3.2021.0704)
|
26
26
|
multi_xml (0.6.0)
|
27
27
|
parallel (1.20.1)
|
28
|
-
parser (3.0.
|
28
|
+
parser (3.0.1.1)
|
29
29
|
ast (~> 2.4.1)
|
30
30
|
public_suffix (4.0.6)
|
31
31
|
rainbow (3.0.0)
|
32
|
-
rake (13.0.
|
32
|
+
rake (13.0.6)
|
33
33
|
regexp_parser (2.1.1)
|
34
|
-
rexml (3.2.
|
34
|
+
rexml (3.2.5)
|
35
35
|
rspec (3.10.0)
|
36
36
|
rspec-core (~> 3.10.0)
|
37
37
|
rspec-expectations (~> 3.10.0)
|
@@ -45,21 +45,21 @@ GEM
|
|
45
45
|
diff-lcs (>= 1.2.0, < 2.0)
|
46
46
|
rspec-support (~> 3.10.0)
|
47
47
|
rspec-support (3.10.1)
|
48
|
-
rubocop (1.
|
48
|
+
rubocop (1.18.3)
|
49
49
|
parallel (~> 1.10)
|
50
50
|
parser (>= 3.0.0.0)
|
51
51
|
rainbow (>= 2.2.2, < 4.0)
|
52
52
|
regexp_parser (>= 1.8, < 3.0)
|
53
53
|
rexml
|
54
|
-
rubocop-ast (>= 1.
|
54
|
+
rubocop-ast (>= 1.7.0, < 2.0)
|
55
55
|
ruby-progressbar (~> 1.7)
|
56
56
|
unicode-display_width (>= 1.4.0, < 3.0)
|
57
|
-
rubocop-ast (1.
|
58
|
-
parser (>=
|
57
|
+
rubocop-ast (1.7.0)
|
58
|
+
parser (>= 3.0.1.1)
|
59
59
|
ruby-progressbar (1.11.0)
|
60
60
|
unicode-display_width (2.0.0)
|
61
61
|
vcr (6.0.0)
|
62
|
-
webmock (3.
|
62
|
+
webmock (3.13.0)
|
63
63
|
addressable (>= 2.3.6)
|
64
64
|
crack (>= 0.3.2)
|
65
65
|
hashdiff (>= 0.4.0, < 2.0.0)
|
@@ -71,10 +71,10 @@ DEPENDENCIES
|
|
71
71
|
byebug (~> 11.1.3)
|
72
72
|
rake (~> 13.0)
|
73
73
|
rspec (~> 3.10)
|
74
|
-
rubocop (~> 1.
|
74
|
+
rubocop (~> 1.18.3)
|
75
75
|
ruby-openai!
|
76
76
|
vcr (~> 6.0.0)
|
77
|
-
webmock (~> 3.
|
77
|
+
webmock (~> 3.13.0)
|
78
78
|
|
79
79
|
BUNDLED WITH
|
80
80
|
2.2.3
|
data/README.md
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/ruby-openai.svg)](https://badge.fury.io/rb/ruby-openai)
|
4
4
|
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/alexrudall/ruby-openai/blob/main/LICENSE.txt)
|
5
5
|
[![CircleCI Build Status](https://circleci.com/gh/alexrudall/ruby-openai.svg?style=shield)](https://circleci.com/gh/alexrudall/ruby-openai)
|
6
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/a99a88d28ad37a79dbf6/maintainability)](https://codeclimate.com/github/codeclimate/codeclimate/maintainability)
|
6
7
|
|
7
8
|
A simple Ruby wrapper for the [OpenAI GPT-3 API](https://openai.com/blog/openai-api/).
|
8
9
|
|
@@ -81,7 +82,7 @@ and pass the path to `client.files.upload` to upload it to OpenAI, and then inte
|
|
81
82
|
Pass documents and a query string to get semantic search scores against each document:
|
82
83
|
|
83
84
|
```
|
84
|
-
response = client.search(engine: "ada", documents: %w[washington hospital school], query: "president")
|
85
|
+
response = client.search(engine: "ada", parameters: { documents: %w[washington hospital school], query: "president" })
|
85
86
|
puts response["data"].map { |d| d["score"] }
|
86
87
|
=> [202.0, 48.052, 19.247]
|
87
88
|
```
|
@@ -89,7 +90,7 @@ Pass documents and a query string to get semantic search scores against each doc
|
|
89
90
|
You can alternatively search using the ID of a file you've uploaded:
|
90
91
|
|
91
92
|
```
|
92
|
-
client.search(engine: "ada", file: "abc123", query: "happy")
|
93
|
+
client.search(engine: "ada", parameters: { file: "abc123", query: "happy" })
|
93
94
|
```
|
94
95
|
|
95
96
|
### Answers
|
@@ -106,7 +107,7 @@ Pass documents, a question string, and an example question/response to get an an
|
|
106
107
|
})
|
107
108
|
```
|
108
109
|
|
109
|
-
|
110
|
+
Or use the ID of a file you've uploaded:
|
110
111
|
|
111
112
|
```
|
112
113
|
response = client.answers(parameters: {
|
@@ -118,11 +119,37 @@ You can alternatively search using the ID of a file you've uploaded:
|
|
118
119
|
})
|
119
120
|
```
|
120
121
|
|
122
|
+
### Classifications
|
123
|
+
|
124
|
+
Pass examples and a query to predict the most likely labels:
|
125
|
+
|
126
|
+
```
|
127
|
+
response = client.classifications(parameters: {
|
128
|
+
examples: [
|
129
|
+
["A happy moment", "Positive"],
|
130
|
+
["I am sad.", "Negative"],
|
131
|
+
["I am feeling awesome", "Positive"]
|
132
|
+
],
|
133
|
+
query: "It is a raining day :(",
|
134
|
+
model: "ada"
|
135
|
+
})
|
136
|
+
```
|
137
|
+
|
138
|
+
Or use the ID of a file you've uploaded:
|
139
|
+
|
140
|
+
```
|
141
|
+
response = client.classifications(parameters: {
|
142
|
+
file: "123abc,
|
143
|
+
query: "It is a raining day :(",
|
144
|
+
model: "ada"
|
145
|
+
})
|
146
|
+
```
|
147
|
+
|
121
148
|
## Development
|
122
149
|
|
123
150
|
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.
|
124
151
|
|
125
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
152
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, update `CHANGELOG.md`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
126
153
|
|
127
154
|
## Contributing
|
128
155
|
|
data/lib/ruby/openai/client.rb
CHANGED
@@ -8,44 +8,47 @@ module OpenAI
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def answers(version: default_version, parameters: {})
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
},
|
17
|
-
body: parameters.to_json
|
18
|
-
)
|
11
|
+
post(url: "/#{version}/answers", parameters: parameters)
|
12
|
+
end
|
13
|
+
|
14
|
+
def classifications(version: default_version, parameters: {})
|
15
|
+
post(url: "/#{version}/classifications", parameters: parameters)
|
19
16
|
end
|
20
17
|
|
21
18
|
def completions(engine:, version: default_version, parameters: {})
|
22
|
-
|
23
|
-
"/#{version}/engines/#{engine}/completions",
|
24
|
-
headers: {
|
25
|
-
"Content-Type" => "application/json",
|
26
|
-
"Authorization" => "Bearer #{@access_token}"
|
27
|
-
},
|
28
|
-
body: parameters.to_json
|
29
|
-
)
|
19
|
+
post(url: "/#{version}/engines/#{engine}/completions", parameters: parameters)
|
30
20
|
end
|
31
21
|
|
32
22
|
def files
|
33
23
|
@files ||= OpenAI::Files.new(access_token: @access_token)
|
34
24
|
end
|
35
25
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
},
|
43
|
-
body: { query: query }.merge(documents_or_file(documents: documents, file: file)).to_json
|
44
|
-
)
|
26
|
+
# rubocop:disable Layout/LineLength
|
27
|
+
# rubocop:disable Metrics/ParameterLists
|
28
|
+
def search(engine:, query: nil, documents: nil, file: nil, version: default_version, parameters: {})
|
29
|
+
return legacy_search(engine: engine, query: query, documents: documents, file: file, version: version) if query || documents || file
|
30
|
+
|
31
|
+
post(url: "/#{version}/engines/#{engine}/search", parameters: parameters)
|
45
32
|
end
|
33
|
+
# rubocop:enable Layout/LineLength
|
34
|
+
# rubocop:enable Metrics/ParameterLists
|
46
35
|
|
47
36
|
private
|
48
37
|
|
38
|
+
# rubocop:disable Layout/LineLength
|
39
|
+
def legacy_search(engine:, query:, documents: nil, file: nil, version: default_version)
|
40
|
+
warn "[DEPRECATION] Passing `query`, `documents` or `file` directly to `Client#search` is deprecated and will be removed in a future version of ruby-openai.
|
41
|
+
Please nest these terms within `parameters` instead, like this:
|
42
|
+
client.search(engine: 'davinci', parameters: { query: 'president', documents: %w[washington hospital school] })
|
43
|
+
"
|
44
|
+
|
45
|
+
post(
|
46
|
+
url: "/#{version}/engines/#{engine}/search",
|
47
|
+
parameters: { query: query }.merge(documents_or_file(documents: documents, file: file))
|
48
|
+
)
|
49
|
+
end
|
50
|
+
# rubocop:enable Layout/LineLength
|
51
|
+
|
49
52
|
def default_version
|
50
53
|
"v1".freeze
|
51
54
|
end
|
@@ -53,5 +56,16 @@ module OpenAI
|
|
53
56
|
def documents_or_file(documents: nil, file: nil)
|
54
57
|
documents ? { documents: documents } : { file: file }
|
55
58
|
end
|
59
|
+
|
60
|
+
def post(url:, parameters:)
|
61
|
+
self.class.post(
|
62
|
+
url,
|
63
|
+
headers: {
|
64
|
+
"Content-Type" => "application/json",
|
65
|
+
"Authorization" => "Bearer #{@access_token}"
|
66
|
+
},
|
67
|
+
body: parameters.to_json
|
68
|
+
)
|
69
|
+
end
|
56
70
|
end
|
57
71
|
end
|
data/lib/ruby/openai/files.rb
CHANGED
@@ -18,6 +18,8 @@ module OpenAI
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def upload(version: default_version, parameters: {})
|
21
|
+
validate(file: parameters[:file])
|
22
|
+
|
21
23
|
self.class.post(
|
22
24
|
"/#{version}/files",
|
23
25
|
headers: {
|
@@ -53,5 +55,13 @@ module OpenAI
|
|
53
55
|
def default_version
|
54
56
|
"v1".freeze
|
55
57
|
end
|
58
|
+
|
59
|
+
def validate(file:)
|
60
|
+
File.open(file).each_line.with_index do |line, index|
|
61
|
+
JSON.parse(line)
|
62
|
+
rescue JSON::ParserError => e
|
63
|
+
raise JSON::ParserError, "#{e.message} - found on line #{index + 1} of #{file}"
|
64
|
+
end
|
65
|
+
end
|
56
66
|
end
|
57
67
|
end
|
data/lib/ruby/openai/version.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.5.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: 1.
|
4
|
+
version: 1.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-07-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dotenv
|
@@ -48,6 +48,7 @@ files:
|
|
48
48
|
- ".circleci/config.yml"
|
49
49
|
- ".github/ISSUE_TEMPLATE/bug_report.md"
|
50
50
|
- ".github/ISSUE_TEMPLATE/feature_request.md"
|
51
|
+
- ".github/dependabot.yml"
|
51
52
|
- ".gitignore"
|
52
53
|
- ".rspec"
|
53
54
|
- ".rubocop.yml"
|
@@ -83,7 +84,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
83
84
|
requirements:
|
84
85
|
- - ">="
|
85
86
|
- !ruby/object:Gem::Version
|
86
|
-
version: 2.
|
87
|
+
version: 2.5.0
|
87
88
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
89
|
requirements:
|
89
90
|
- - ">="
|