Skip to content

Commit 5d56dd3

Browse files
authored
Merge pull request alexrudall#40 from alexrudall/classifications
Add Classifications
2 parents 3a84895 + 7924a04 commit 5d56dd3

File tree

11 files changed

+363
-38
lines changed

11 files changed

+363
-38
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

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+
818
## [1.2.2] - 2021-04-18
919

1020
### Changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
ruby-openai (1.2.2)
4+
ruby-openai (1.3.0)
55
dotenv (~> 2.7.6)
66
httparty (~> 0.18.1)
77

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ Pass documents, a question string, and an example question/response to get an an
107107
})
108108
```
109109

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:
111111

112112
```
113113
response = client.answers(parameters: {
@@ -119,6 +119,32 @@ You can alternatively search using the ID of a file you've uploaded:
119119
})
120120
```
121121

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+
122148
## Development
123149

124150
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.

lib/ruby/openai/client.rb

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,15 @@ def initialize(access_token: nil)
88
end
99

1010
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)
1916
end
2017

2118
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)
3020
end
3121

3222
def files
@@ -38,37 +28,25 @@ def files
3828
def search(engine:, query: nil, documents: nil, file: nil, version: default_version, parameters: {})
3929
return legacy_search(engine: engine, query: query, documents: documents, file: file, version: version) if query || documents || file
4030

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)
4932
end
5033
# rubocop:enable Layout/LineLength
5134
# rubocop:enable Metrics/ParameterLists
5235

5336
private
5437

55-
# rubocop:disable Metrics/MethodLength
5638
# rubocop:disable Layout/LineLength
5739
def legacy_search(engine:, query:, documents: nil, file: nil, version: default_version)
5840
warn "[DEPRECATION] Passing `query`, `documents` or `file` directly to `Client#search` is deprecated and will be removed in a future version of ruby-openai.
5941
Please nest these terms within `parameters` instead, like this:
6042
client.search(engine: 'davinci', parameters: { query: 'president', documents: %w[washington hospital school] })
6143
"
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))
6948
)
7049
end
71-
# rubocop:enable Metrics/MethodLength
7250
# rubocop:enable Layout/LineLength
7351

7452
def default_version
@@ -78,5 +56,16 @@ def default_version
7856
def documents_or_file(documents: nil, file: nil)
7957
documents ? { documents: documents } : { file: file }
8058
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
8170
end
8271
end

lib/ruby/openai/files.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ def list(version: default_version)
1818
end
1919

2020
def upload(version: default_version, parameters: {})
21-
file = validate(file: parameters[:file])
21+
validate(file: parameters[:file])
2222

2323
self.class.post(
2424
"/#{version}/files",
2525
headers: {
2626
"Content-Type" => "application/json",
2727
"Authorization" => "Bearer #{@access_token}"
2828
},
29-
body: parameters.merge(file: file)
29+
body: parameters.merge(file: File.open(parameters[:file]))
3030
)
3131
end
3232

lib/ruby/openai/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Ruby
22
module OpenAI
3-
VERSION = "1.2.2".freeze
3+
VERSION = "1.3.0".freeze
44
end
55
end

spec/fixtures/cassettes/ada_classifications_examples_it_is_a_raining_day_.yml

Lines changed: 78 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/cassettes/curie_classifications_file_movie_is_very_good.yml

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)