ruby-openai 1.2.0 → 1.3.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a958db13dbbf94dbbeeadb2dba89e9c00eb17c40def662e85074410becbca3c5
4
- data.tar.gz: 2ec73b390ef82c2e8f5498d6abf195cf49c40a03e6b446e42f7715df0bfc27fd
3
+ metadata.gz: 2b8fe14b8e08b05f4743569e5c94f92552032802bf0eb4fd39fd8168ea6e1105
4
+ data.tar.gz: 70933cec4140ffe73c52e0bc25e5afc2bb65d4348b1c8138f95acc8b7ccdb500
5
5
  SHA512:
6
- metadata.gz: ed8d2bcedcc30defd2acb28d29be00c2c07bf80b38dd7748323c0661b90f1b09da5c21c7dd7b6014161d7fd966e9da6854d996236c032744d4f0fe70f0a4abb2
7
- data.tar.gz: a846cf9bb2888dd30a89b5796ead927615ccb46647ef803a88cfc281c08e7064857a6ee025f4611d9b7db4b0760198c7504b65257ec1584b88196ca4d27a8423
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:2.7-node
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: cimg/ruby:2.7-node
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
@@ -1,5 +1,5 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.7
2
+ TargetRubyVersion: 2.5
3
3
  NewCops: enable
4
4
  SuggestExtensions: false
5
5
 
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
@@ -6,6 +6,6 @@ gemspec
6
6
  gem "byebug", "~> 11.1.3"
7
7
  gem "rake", "~> 13.0"
8
8
  gem "rspec", "~> 3.10"
9
- gem "rubocop", "~> 1.12.1"
9
+ gem "rubocop", "~> 1.18.3"
10
10
  gem "vcr", "~> 6.0.0"
11
- gem "webmock", "~> 3.12.2"
11
+ gem "webmock", "~> 3.13.0"
data/Gemfile.lock CHANGED
@@ -1,14 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-openai (1.2.0)
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.7.0)
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.2020.0512)
25
+ mime-types-data (3.2021.0704)
26
26
  multi_xml (0.6.0)
27
27
  parallel (1.20.1)
28
- parser (3.0.0.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.3)
32
+ rake (13.0.6)
33
33
  regexp_parser (2.1.1)
34
- rexml (3.2.4)
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.12.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.2.0, < 2.0)
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.4.1)
58
- parser (>= 2.7.1.5)
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.12.2)
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.12.1)
74
+ rubocop (~> 1.18.3)
75
75
  ruby-openai!
76
76
  vcr (~> 6.0.0)
77
- webmock (~> 3.12.2)
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
- You can alternatively search using the ID of a file you've uploaded:
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
 
@@ -8,44 +8,47 @@ module OpenAI
8
8
  end
9
9
 
10
10
  def answers(version: default_version, parameters: {})
11
- self.class.post(
12
- "/#{version}/answers",
13
- headers: {
14
- "Content-Type" => "application/json",
15
- "Authorization" => "Bearer #{@access_token}"
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
- self.class.post(
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
- def search(engine:, query:, documents: nil, file: nil, version: default_version)
37
- self.class.post(
38
- "/#{version}/engines/#{engine}/search",
39
- headers: {
40
- "Content-Type" => "application/json",
41
- "Authorization" => "Bearer #{@access_token}"
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
@@ -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
@@ -1,5 +1,5 @@
1
1
  module Ruby
2
2
  module OpenAI
3
- VERSION = "1.2.0".freeze
3
+ VERSION = "1.3.1".freeze
4
4
  end
5
5
  end
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.7.0")
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.2.0
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-04-08 00:00:00.000000000 Z
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.7.0
87
+ version: 2.5.0
87
88
  required_rubygems_version: !ruby/object:Gem::Requirement
88
89
  requirements:
89
90
  - - ">="