Skip to content

Commit fa4c744

Browse files
authored
Merge pull request #35 from alexrudall/add_new_endpoints
Add file upload
2 parents e60bf4d + b1b380a commit fa4c744

20 files changed

+579
-19
lines changed

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
AllCops:
22
TargetRubyVersion: 2.7
3-
43
NewCops: enable
4+
SuggestExtensions: false
55

66
Style/Documentation:
77
# Skips checking to make sure top level modules / classes have a comment.

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ 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.1.0] - 2021-04-07
9+
10+
### Added
11+
12+
- Add Client#files to allow file upload.
13+
- Add Client#search(file:) so you can search a file.
14+
815
## [1.0.0] - 2021-02-01
916

1017
### Removed

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.0.0)
4+
ruby-openai (1.1.0)
55
dotenv (~> 2.7.6)
66
httparty (~> 0.18.1)
77

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ The engine options are currently "ada", "babbage", "curie" and "davinci". Hit th
5858
=> [", there lived a great"]
5959
```
6060

61+
### Files
62+
63+
Put your data in a `.jsonl` file like this:
64+
65+
```
66+
{"text": "puppy A is happy", "metadata": "emotional state of puppy A"}
67+
{"text": "puppy B is sad", "metadata": "emotional state of puppy B"}
68+
```
69+
70+
and pass the path to `client.files.upload` to upload it to OpenAI, and then interact with it:
71+
72+
```
73+
client.files.upload(parameters: { file: 'path/to/puppy.jsonl', purpose: 'search' })
74+
client.files.list
75+
client.files.retrieve(id: 123)
76+
client.files.delete(id: 123)
77+
```
78+
6179
### Search
6280

6381
Pass documents and a query string to get semantic search scores against each document:
@@ -68,6 +86,12 @@ Pass documents and a query string to get semantic search scores against each doc
6886
=> [202.0, 48.052, 19.247]
6987
```
7088

89+
You can alternatively search using the ID of a file you've uploaded:
90+
91+
```
92+
client.search(engine: "ada", file: "abc123", query: "happy")
93+
```
94+
7195
## Development
7296

7397
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.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "httparty"
2+
require "ruby/openai/files"
23
require "ruby/openai/client"
34
require "ruby/openai/version"
45
require "dotenv/load"

lib/ruby/openai/client.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@ def completions(engine:, version: default_version, parameters: {})
1818
)
1919
end
2020

21-
def search(engine:, documents:, query:, version: default_version)
21+
def files
22+
@files ||= OpenAI::Files.new(access_token: @access_token)
23+
end
24+
25+
def search(engine:, query:, documents: nil, file: nil, version: default_version)
2226
self.class.post(
2327
"/#{version}/engines/#{engine}/search",
2428
headers: {
2529
"Content-Type" => "application/json",
2630
"Authorization" => "Bearer #{@access_token}"
2731
},
28-
body: {
29-
documents: documents, query: query
30-
}.to_json
32+
body: { query: query }.merge(documents_or_file(documents: documents, file: file)).to_json
3133
)
3234
end
3335

@@ -36,5 +38,9 @@ def search(engine:, documents:, query:, version: default_version)
3638
def default_version
3739
"v1".freeze
3840
end
41+
42+
def documents_or_file(documents: nil, file: nil)
43+
documents ? { documents: documents } : { file: file }
44+
end
3945
end
4046
end

lib/ruby/openai/files.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
self.class.post(
22+
"/#{version}/files",
23+
headers: {
24+
"Content-Type" => "application/json",
25+
"Authorization" => "Bearer #{@access_token}"
26+
},
27+
body: parameters.merge(file: File.open(parameters[:file]))
28+
)
29+
end
30+
31+
def retrieve(id:, version: default_version)
32+
self.class.get(
33+
"/#{version}/files/#{id}",
34+
headers: {
35+
"Content-Type" => "application/json",
36+
"Authorization" => "Bearer #{@access_token}"
37+
}
38+
)
39+
end
40+
41+
def delete(id:, version: default_version)
42+
self.class.delete(
43+
"/#{version}/files/#{id}",
44+
headers: {
45+
"Content-Type" => "application/json",
46+
"Authorization" => "Bearer #{@access_token}"
47+
}
48+
)
49+
end
50+
51+
private
52+
53+
def default_version
54+
"v1".freeze
55+
end
56+
end
57+
end

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.0.0".freeze
3+
VERSION = "1.1.0".freeze
44
end
55
end

pull_request_template.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
## All Submissions:
22

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-
* [ ] Do the tests pass? (`rake spec`)
6-
* [ ] Does Rubocop pass? (`rubocop`)
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?
75
* [ ] Have you added an explanation of what your changes do and why you'd like us to include them?

spec/fixtures/cassettes/davinci_search_happy.yml

Lines changed: 63 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)