ruby-openai 1.2.2 → 1.3.0

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: 9a10874422fb2b2250857d43afe2baa501152ba0358dadfe12d4a2c21f106325
4
- data.tar.gz: 5bfbedd2a2347ee7479ebcdf48dc4c7fe8e4dc74fcd721c9c3beeb1386b3199e
3
+ metadata.gz: 6a7d27bf0f52788ef3941a6b77e0fba8896fe93cebedf04a034a10a2612bf82d
4
+ data.tar.gz: 0f2428ce0a634438124e9c9e3155b58e323aa79ba1fc5b423d5ef0b975b09b43
5
5
  SHA512:
6
- metadata.gz: 67f860d7243072419bc21c91bce26e71b8def68115f91347822770e087c2cc898b42cf2b8fd2a97155d7b44cf79d096e54e6dd16b0842d680ddcef4effb4ca9c
7
- data.tar.gz: 6283aaf2aba88b61f47c6218a62bf70c837c9d9a8893bea5f1ee27801f2168f5868329fe33b3de827e056f278e6c70f61b7d70b18582ffc87586bce579374d47
6
+ metadata.gz: 4a516cc0f45ec8d7e4085dc67b0db380c34be6712eb822de199b0c8b83f9623c681828c76259d5d17478cca279ae6227c372cbb755151159a0f868146772e579
7
+ data.tar.gz: bf7f2bd1ffd7064d4f553f87a3eda924871d8a6d60140290af1b4edd01b689af3792b651b7084904fc711a4e27209ed257c20b9076759a213f30f2b98c1207cb
data/CHANGELOG.md CHANGED
@@ -5,6 +5,16 @@ 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.0] - 2021-04-18
9
+
10
+ ### Added
11
+
12
+ - Add Client#classifications to predict the most likely labels based on examples or a file.
13
+
14
+ ### Fixed
15
+
16
+ - Fixed Files#upload which was previously broken by the validation code!
17
+
8
18
  ## [1.2.2] - 2021-04-18
9
19
 
10
20
  ### Changed
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-openai (1.2.2)
4
+ ruby-openai (1.3.0)
5
5
  dotenv (~> 2.7.6)
6
6
  httparty (~> 0.18.1)
7
7
 
data/README.md CHANGED
@@ -107,7 +107,7 @@ Pass documents, a question string, and an example question/response to get an an
107
107
  })
108
108
  ```
109
109
 
110
- You can alternatively search using the ID of a file you've uploaded:
110
+ Or use the ID of a file you've uploaded:
111
111
 
112
112
  ```
113
113
  response = client.answers(parameters: {
@@ -119,6 +119,32 @@ You can alternatively search using the ID of a file you've uploaded:
119
119
  })
120
120
  ```
121
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
+
122
148
  ## Development
123
149
 
124
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.
@@ -8,25 +8,15 @@ 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
@@ -38,37 +28,25 @@ module OpenAI
38
28
  def search(engine:, query: nil, documents: nil, file: nil, version: default_version, parameters: {})
39
29
  return legacy_search(engine: engine, query: query, documents: documents, file: file, version: version) if query || documents || file
40
30
 
41
- self.class.post(
42
- "/#{version}/engines/#{engine}/search",
43
- headers: {
44
- "Content-Type" => "application/json",
45
- "Authorization" => "Bearer #{@access_token}"
46
- },
47
- body: parameters.to_json
48
- )
31
+ post(url: "/#{version}/engines/#{engine}/search", parameters: parameters)
49
32
  end
50
33
  # rubocop:enable Layout/LineLength
51
34
  # rubocop:enable Metrics/ParameterLists
52
35
 
53
36
  private
54
37
 
55
- # rubocop:disable Metrics/MethodLength
56
38
  # rubocop:disable Layout/LineLength
57
39
  def legacy_search(engine:, query:, documents: nil, file: nil, version: default_version)
58
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.
59
41
  Please nest these terms within `parameters` instead, like this:
60
42
  client.search(engine: 'davinci', parameters: { query: 'president', documents: %w[washington hospital school] })
61
43
  "
62
- self.class.post(
63
- "/#{version}/engines/#{engine}/search",
64
- headers: {
65
- "Content-Type" => "application/json",
66
- "Authorization" => "Bearer #{@access_token}"
67
- },
68
- body: { query: query }.merge(documents_or_file(documents: documents, file: file)).to_json
44
+
45
+ post(
46
+ url: "/#{version}/engines/#{engine}/search",
47
+ parameters: { query: query }.merge(documents_or_file(documents: documents, file: file))
69
48
  )
70
49
  end
71
- # rubocop:enable Metrics/MethodLength
72
50
  # rubocop:enable Layout/LineLength
73
51
 
74
52
  def default_version
@@ -78,5 +56,16 @@ module OpenAI
78
56
  def documents_or_file(documents: nil, file: nil)
79
57
  documents ? { documents: documents } : { file: file }
80
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
81
70
  end
82
71
  end
@@ -18,7 +18,7 @@ module OpenAI
18
18
  end
19
19
 
20
20
  def upload(version: default_version, parameters: {})
21
- file = validate(file: parameters[:file])
21
+ validate(file: parameters[:file])
22
22
 
23
23
  self.class.post(
24
24
  "/#{version}/files",
@@ -26,7 +26,7 @@ module OpenAI
26
26
  "Content-Type" => "application/json",
27
27
  "Authorization" => "Bearer #{@access_token}"
28
28
  },
29
- body: parameters.merge(file: file)
29
+ body: parameters.merge(file: File.open(parameters[:file]))
30
30
  )
31
31
  end
32
32
 
@@ -1,5 +1,5 @@
1
1
  module Ruby
2
2
  module OpenAI
3
- VERSION = "1.2.2".freeze
3
+ VERSION = "1.3.0".freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-openai
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex