ruby-openai 0.3.0 → 1.2.2

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: df318e71dd94c35f0779b393494833aba28be2d0849e76593cf1caa023694e28
4
- data.tar.gz: 294916dd3edb50a70bed4bc2c6b96c0b385e19b25e7ded70f696b143be7e776b
3
+ metadata.gz: 9a10874422fb2b2250857d43afe2baa501152ba0358dadfe12d4a2c21f106325
4
+ data.tar.gz: 5bfbedd2a2347ee7479ebcdf48dc4c7fe8e4dc74fcd721c9c3beeb1386b3199e
5
5
  SHA512:
6
- metadata.gz: e9762da7271d38a2f1ea744923799321df71e1d9025e2f7d8a4ca72a621a406760106e7d718ba6e493a1fc63e45d3c44032c874fc5b9ae3b553602066cfa789a
7
- data.tar.gz: 8709f60b06f8b7713cb82f106d4c9c4698b04d327760bd57c6d466344f55abf61dd20feb2886f7f1f506d40941d04dbd75bb3b0725af02ac79d13cffc4f1fb0b
6
+ metadata.gz: 67f860d7243072419bc21c91bce26e71b8def68115f91347822770e087c2cc898b42cf2b8fd2a97155d7b44cf79d096e54e6dd16b0842d680ddcef4effb4ca9c
7
+ data.tar.gz: 6283aaf2aba88b61f47c6218a62bf70c837c9d9a8893bea5f1ee27801f2168f5868329fe33b3de827e056f278e6c70f61b7d70b18582ffc87586bce579374d47
data/.rubocop.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  AllCops:
2
2
  TargetRubyVersion: 2.7
3
-
4
3
  NewCops: enable
4
+ SuggestExtensions: false
5
5
 
6
6
  Style/Documentation:
7
7
  # Skips checking to make sure top level modules / classes have a comment.
data/CHANGELOG.md CHANGED
@@ -5,6 +5,44 @@ 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.2.2] - 2021-04-18
9
+
10
+ ### Changed
11
+
12
+ - Add Client#search(parameters:) to allow passing `max_rerank` or `return_metadata`.
13
+ - Deprecate Client#search with query, file or document parameters at the top level.
14
+ - Thanks [@stevegeek](https://github.com/stevegeek) for pointing this issue out!
15
+
16
+ ## [1.2.1] - 2021-04-11
17
+
18
+ ### Added
19
+
20
+ - Add validation of JSONL files to make it easier to debug during upload.
21
+
22
+ ## [1.2.0] - 2021-04-08
23
+
24
+ ### Added
25
+
26
+ - Add Client#answers endpoint for question/answer response on documents or a file.
27
+
28
+ ## [1.1.0] - 2021-04-07
29
+
30
+ ### Added
31
+
32
+ - Add Client#files to allow file upload.
33
+ - Add Client#search(file:) so you can search a file.
34
+
35
+ ## [1.0.0] - 2021-02-01
36
+
37
+ ### Removed
38
+
39
+ - Remove deprecated method Client#call - use Client#completions instead.
40
+
41
+ ### Changed
42
+
43
+ - Rename 'master' branch to 'main' branch.
44
+ - Bump dependencies.
45
+
8
46
  ## [0.3.0] - 2020-11-22
9
47
 
10
48
  ### Added
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,3 @@
1
+ ## Contributing
2
+
3
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alexrudall/ruby-openai. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/alexrudall/ruby-openai/blob/main/CODE_OF_CONDUCT.md).
data/Gemfile CHANGED
@@ -5,7 +5,7 @@ gemspec
5
5
 
6
6
  gem "byebug", "~> 11.1.3"
7
7
  gem "rake", "~> 13.0"
8
- gem "rspec", "~> 3.0"
9
- gem "rubocop", "~> 0.93.1"
8
+ gem "rspec", "~> 3.10"
9
+ gem "rubocop", "~> 1.12.1"
10
10
  gem "vcr", "~> 6.0.0"
11
- gem "webmock", "~> 3.9.2"
11
+ gem "webmock", "~> 3.12.2"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-openai (0.3.0)
4
+ ruby-openai (1.2.2)
5
5
  dotenv (~> 2.7.6)
6
6
  httparty (~> 0.18.1)
7
7
 
@@ -10,9 +10,10 @@ GEM
10
10
  specs:
11
11
  addressable (2.7.0)
12
12
  public_suffix (>= 2.0.2, < 5.0)
13
- ast (2.4.1)
13
+ ast (2.4.2)
14
14
  byebug (11.1.3)
15
- crack (0.4.4)
15
+ crack (0.4.5)
16
+ rexml
16
17
  diff-lcs (1.4.4)
17
18
  dotenv (2.7.6)
18
19
  hashdiff (1.0.1)
@@ -21,44 +22,44 @@ GEM
21
22
  multi_xml (>= 0.5.2)
22
23
  mime-types (3.3.1)
23
24
  mime-types-data (~> 3.2015)
24
- mime-types-data (3.2020.0512)
25
+ mime-types-data (3.2021.0225)
25
26
  multi_xml (0.6.0)
26
- parallel (1.19.2)
27
- parser (2.7.2.0)
27
+ parallel (1.20.1)
28
+ parser (3.0.0.0)
28
29
  ast (~> 2.4.1)
29
30
  public_suffix (4.0.6)
30
31
  rainbow (3.0.0)
31
- rake (13.0.1)
32
- regexp_parser (1.8.2)
32
+ rake (13.0.3)
33
+ regexp_parser (2.1.1)
33
34
  rexml (3.2.4)
34
- rspec (3.9.0)
35
- rspec-core (~> 3.9.0)
36
- rspec-expectations (~> 3.9.0)
37
- rspec-mocks (~> 3.9.0)
38
- rspec-core (3.9.2)
39
- rspec-support (~> 3.9.3)
40
- rspec-expectations (3.9.2)
35
+ rspec (3.10.0)
36
+ rspec-core (~> 3.10.0)
37
+ rspec-expectations (~> 3.10.0)
38
+ rspec-mocks (~> 3.10.0)
39
+ rspec-core (3.10.1)
40
+ rspec-support (~> 3.10.0)
41
+ rspec-expectations (3.10.1)
41
42
  diff-lcs (>= 1.2.0, < 2.0)
42
- rspec-support (~> 3.9.0)
43
- rspec-mocks (3.9.1)
43
+ rspec-support (~> 3.10.0)
44
+ rspec-mocks (3.10.1)
44
45
  diff-lcs (>= 1.2.0, < 2.0)
45
- rspec-support (~> 3.9.0)
46
- rspec-support (3.9.3)
47
- rubocop (0.93.1)
46
+ rspec-support (~> 3.10.0)
47
+ rspec-support (3.10.1)
48
+ rubocop (1.12.1)
48
49
  parallel (~> 1.10)
49
- parser (>= 2.7.1.5)
50
+ parser (>= 3.0.0.0)
50
51
  rainbow (>= 2.2.2, < 4.0)
51
- regexp_parser (>= 1.8)
52
+ regexp_parser (>= 1.8, < 3.0)
52
53
  rexml
53
- rubocop-ast (>= 0.6.0)
54
+ rubocop-ast (>= 1.2.0, < 2.0)
54
55
  ruby-progressbar (~> 1.7)
55
- unicode-display_width (>= 1.4.0, < 2.0)
56
- rubocop-ast (0.7.1)
56
+ unicode-display_width (>= 1.4.0, < 3.0)
57
+ rubocop-ast (1.4.1)
57
58
  parser (>= 2.7.1.5)
58
- ruby-progressbar (1.10.1)
59
- unicode-display_width (1.7.0)
59
+ ruby-progressbar (1.11.0)
60
+ unicode-display_width (2.0.0)
60
61
  vcr (6.0.0)
61
- webmock (3.9.2)
62
+ webmock (3.12.2)
62
63
  addressable (>= 2.3.6)
63
64
  crack (>= 0.3.2)
64
65
  hashdiff (>= 0.4.0, < 2.0.0)
@@ -69,11 +70,11 @@ PLATFORMS
69
70
  DEPENDENCIES
70
71
  byebug (~> 11.1.3)
71
72
  rake (~> 13.0)
72
- rspec (~> 3.0)
73
- rubocop (~> 0.93.1)
73
+ rspec (~> 3.10)
74
+ rubocop (~> 1.12.1)
74
75
  ruby-openai!
75
76
  vcr (~> 6.0.0)
76
- webmock (~> 3.9.2)
77
+ webmock (~> 3.12.2)
77
78
 
78
79
  BUNDLED WITH
79
- 2.1.4
80
+ 2.2.3
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # Ruby::OpenAI
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/ruby-openai.svg)](https://badge.fury.io/rb/ruby-openai)
4
- [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/alexrudall/ruby-openai/blob/master/LICENSE.txt)
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
 
@@ -58,16 +59,66 @@ The engine options are currently "ada", "babbage", "curie" and "davinci". Hit th
58
59
  => [", there lived a great"]
59
60
  ```
60
61
 
62
+ ### Files
63
+
64
+ Put your data in a `.jsonl` file like this:
65
+
66
+ ```
67
+ {"text": "puppy A is happy", "metadata": "emotional state of puppy A"}
68
+ {"text": "puppy B is sad", "metadata": "emotional state of puppy B"}
69
+ ```
70
+
71
+ and pass the path to `client.files.upload` to upload it to OpenAI, and then interact with it:
72
+
73
+ ```
74
+ client.files.upload(parameters: { file: 'path/to/puppy.jsonl', purpose: 'search' })
75
+ client.files.list
76
+ client.files.retrieve(id: 123)
77
+ client.files.delete(id: 123)
78
+ ```
79
+
61
80
  ### Search
62
81
 
63
82
  Pass documents and a query string to get semantic search scores against each document:
64
83
 
65
84
  ```
66
- 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" })
67
86
  puts response["data"].map { |d| d["score"] }
68
87
  => [202.0, 48.052, 19.247]
69
88
  ```
70
89
 
90
+ You can alternatively search using the ID of a file you've uploaded:
91
+
92
+ ```
93
+ client.search(engine: "ada", parameters: { file: "abc123", query: "happy" })
94
+ ```
95
+
96
+ ### Answers
97
+
98
+ Pass documents, a question string, and an example question/response to get an answer to a question:
99
+
100
+ ```
101
+ response = client.answers(parameters: {
102
+ documents: ["Puppy A is happy.", "Puppy B is sad."],
103
+ question: "which puppy is happy?",
104
+ model: "curie",
105
+ examples_context: "In 2017, U.S. life expectancy was 78.6 years.",
106
+ examples: [["What is human life expectancy in the United States?","78 years."]],
107
+ })
108
+ ```
109
+
110
+ You can alternatively search using the ID of a file you've uploaded:
111
+
112
+ ```
113
+ response = client.answers(parameters: {
114
+ file: "123abc",
115
+ question: "which puppy is happy?",
116
+ model: "curie",
117
+ examples_context: "In 2017, U.S. life expectancy was 78.6 years.",
118
+ examples: [["What is human life expectancy in the United States?","78 years."]],
119
+ })
120
+ ```
121
+
71
122
  ## Development
72
123
 
73
124
  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.
@@ -76,7 +127,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
76
127
 
77
128
  ## Contributing
78
129
 
79
- Bug reports and pull requests are welcome on GitHub at https://github.com/alexrudall/ruby-openai. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/alexrudall/ruby-openai/blob/master/CODE_OF_CONDUCT.md).
130
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alexrudall/ruby-openai. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/alexrudall/ruby-openai/blob/main/CODE_OF_CONDUCT.md).
80
131
 
81
132
  ## License
82
133
 
@@ -84,4 +135,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
84
135
 
85
136
  ## Code of Conduct
86
137
 
87
- Everyone interacting in the Ruby::OpenAI project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/alexrudall/ruby-openai/blob/master/CODE_OF_CONDUCT.md).
138
+ Everyone interacting in the Ruby::OpenAI project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/alexrudall/ruby-openai/blob/main/CODE_OF_CONDUCT.md).
data/lib/ruby/openai.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "httparty"
2
+ require "ruby/openai/files"
2
3
  require "ruby/openai/client"
3
4
  require "ruby/openai/version"
4
5
  require "dotenv/load"
@@ -7,6 +7,17 @@ module OpenAI
7
7
  @access_token = access_token || ENV["OPENAI_ACCESS_TOKEN"]
8
8
  end
9
9
 
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
+ )
19
+ end
20
+
10
21
  def completions(engine:, version: default_version, parameters: {})
11
22
  self.class.post(
12
23
  "/#{version}/engines/#{engine}/completions",
@@ -18,44 +29,54 @@ module OpenAI
18
29
  )
19
30
  end
20
31
 
21
- def search(engine:, documents:, query:, version: default_version)
32
+ def files
33
+ @files ||= OpenAI::Files.new(access_token: @access_token)
34
+ end
35
+
36
+ # rubocop:disable Layout/LineLength
37
+ # rubocop:disable Metrics/ParameterLists
38
+ def search(engine:, query: nil, documents: nil, file: nil, version: default_version, parameters: {})
39
+ return legacy_search(engine: engine, query: query, documents: documents, file: file, version: version) if query || documents || file
40
+
22
41
  self.class.post(
23
42
  "/#{version}/engines/#{engine}/search",
24
43
  headers: {
25
44
  "Content-Type" => "application/json",
26
45
  "Authorization" => "Bearer #{@access_token}"
27
46
  },
28
- body: {
29
- documents: documents, query: query
30
- }.to_json
47
+ body: parameters.to_json
31
48
  )
32
49
  end
50
+ # rubocop:enable Layout/LineLength
51
+ # rubocop:enable Metrics/ParameterLists
52
+
53
+ private
33
54
 
34
55
  # rubocop:disable Metrics/MethodLength
35
56
  # rubocop:disable Layout/LineLength
36
- def call(engine:, prompt:, max_tokens:, version: default_version)
37
- warn "[DEPRECATION] `Client#call` is deprecated and will be removed in a future version of ruby-openai.
38
- Please use `Client#completions` instead, like this:
39
- client.completions(engine: 'davinci', parameters: { prompt: 'Once upon a time', max_tokens: 5 })
57
+ def legacy_search(engine:, query:, documents: nil, file: nil, version: default_version)
58
+ warn "[DEPRECATION] Passing `query`, `documents` or `file` directly to `Client#search` is deprecated and will be removed in a future version of ruby-openai.
59
+ Please nest these terms within `parameters` instead, like this:
60
+ client.search(engine: 'davinci', parameters: { query: 'president', documents: %w[washington hospital school] })
40
61
  "
41
62
  self.class.post(
42
- "/#{version}/engines/#{engine}/completions",
63
+ "/#{version}/engines/#{engine}/search",
43
64
  headers: {
44
65
  "Content-Type" => "application/json",
45
66
  "Authorization" => "Bearer #{@access_token}"
46
67
  },
47
- body: {
48
- prompt: prompt, max_tokens: max_tokens
49
- }.to_json
68
+ body: { query: query }.merge(documents_or_file(documents: documents, file: file)).to_json
50
69
  )
51
70
  end
52
71
  # rubocop:enable Metrics/MethodLength
53
72
  # rubocop:enable Layout/LineLength
54
73
 
55
- private
56
-
57
74
  def default_version
58
75
  "v1".freeze
59
76
  end
77
+
78
+ def documents_or_file(documents: nil, file: nil)
79
+ documents ? { documents: documents } : { file: file }
80
+ end
60
81
  end
61
82
  end
@@ -0,0 +1,67 @@
1
+ module OpenAI
2
+ class Files
3
+ include HTTParty
4
+ base_uri "https://api.openai.com"
5
+
6
+ def initialize(access_token: nil)
7
+ @access_token = access_token || ENV["OPENAI_ACCESS_TOKEN"]
8
+ end
9
+
10
+ def list(version: default_version)
11
+ self.class.get(
12
+ "/#{version}/files",
13
+ headers: {
14
+ "Content-Type" => "application/json",
15
+ "Authorization" => "Bearer #{@access_token}"
16
+ }
17
+ )
18
+ end
19
+
20
+ def upload(version: default_version, parameters: {})
21
+ file = validate(file: parameters[:file])
22
+
23
+ self.class.post(
24
+ "/#{version}/files",
25
+ headers: {
26
+ "Content-Type" => "application/json",
27
+ "Authorization" => "Bearer #{@access_token}"
28
+ },
29
+ body: parameters.merge(file: file)
30
+ )
31
+ end
32
+
33
+ def retrieve(id:, version: default_version)
34
+ self.class.get(
35
+ "/#{version}/files/#{id}",
36
+ headers: {
37
+ "Content-Type" => "application/json",
38
+ "Authorization" => "Bearer #{@access_token}"
39
+ }
40
+ )
41
+ end
42
+
43
+ def delete(id:, version: default_version)
44
+ self.class.delete(
45
+ "/#{version}/files/#{id}",
46
+ headers: {
47
+ "Content-Type" => "application/json",
48
+ "Authorization" => "Bearer #{@access_token}"
49
+ }
50
+ )
51
+ end
52
+
53
+ private
54
+
55
+ def default_version
56
+ "v1".freeze
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
66
+ end
67
+ end
@@ -1,5 +1,5 @@
1
1
  module Ruby
2
2
  module OpenAI
3
- VERSION = "0.3.0".freeze
3
+ VERSION = "1.2.2".freeze
4
4
  end
5
5
  end
@@ -0,0 +1,5 @@
1
+ ## All Submissions:
2
+
3
+ * [ ] Have you followed the guidelines in our [Contributing document](../blob/main/CONTRIBUTING.md)?
4
+ * [ ] Have you checked to ensure there aren't other open [Pull Requests](../pulls) for the same update/change?
5
+ * [ ] Have you added an explanation of what your changes do and why you'd like us to include them?
data/ruby-openai.gemspec CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
13
13
 
14
14
  spec.metadata["homepage_uri"] = spec.homepage
15
15
  spec.metadata["source_code_uri"] = "https://github.com/alexrudall/ruby-openai"
16
- spec.metadata["changelog_uri"] = "https://github.com/alexrudall/ruby-openai/blob/master/CHANGELOG.md"
16
+ spec.metadata["changelog_uri"] = "https://github.com/alexrudall/ruby-openai/blob/main/CHANGELOG.md"
17
17
 
18
18
  # Specify which files should be added to the gem when it is released.
19
19
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
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: 0.3.0
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-22 00:00:00.000000000 Z
11
+ date: 2021-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dotenv
@@ -54,6 +54,7 @@ files:
54
54
  - ".travis.yml"
55
55
  - CHANGELOG.md
56
56
  - CODE_OF_CONDUCT.md
57
+ - CONTRIBUTING.md
57
58
  - Gemfile
58
59
  - Gemfile.lock
59
60
  - LICENSE.txt
@@ -63,7 +64,9 @@ files:
63
64
  - bin/setup
64
65
  - lib/ruby/openai.rb
65
66
  - lib/ruby/openai/client.rb
67
+ - lib/ruby/openai/files.rb
66
68
  - lib/ruby/openai/version.rb
69
+ - pull_request_template.md
67
70
  - ruby-openai.gemspec
68
71
  homepage: https://github.com/alexrudall/ruby-openai
69
72
  licenses:
@@ -71,7 +74,7 @@ licenses:
71
74
  metadata:
72
75
  homepage_uri: https://github.com/alexrudall/ruby-openai
73
76
  source_code_uri: https://github.com/alexrudall/ruby-openai
74
- changelog_uri: https://github.com/alexrudall/ruby-openai/blob/master/CHANGELOG.md
77
+ changelog_uri: https://github.com/alexrudall/ruby-openai/blob/main/CHANGELOG.md
75
78
  post_install_message:
76
79
  rdoc_options: []
77
80
  require_paths:
@@ -87,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
90
  - !ruby/object:Gem::Version
88
91
  version: '0'
89
92
  requirements: []
90
- rubygems_version: 3.1.4
93
+ rubygems_version: 3.2.3
91
94
  signing_key:
92
95
  specification_version: 4
93
96
  summary: A Ruby gem for the OpenAI GPT-3 API