bdd_openai 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -4
- data/README.md +2 -2
- data/lib/bdd_openai/clients/http.rb +9 -9
- data/lib/bdd_openai/file_client.rb +43 -0
- data/lib/bdd_openai/{files → mappers}/file.rb +1 -1
- data/lib/bdd_openai/services/files/base.rb +23 -0
- data/lib/bdd_openai/services/files/delete.rb +21 -0
- data/lib/bdd_openai/services/files/list.rb +22 -0
- data/lib/bdd_openai/services/files/retrieve.rb +21 -0
- data/lib/bdd_openai/services/files/upload.rb +27 -0
- data/lib/bdd_openai/version.rb +1 -1
- data/lib/bdd_openai.rb +2 -2
- metadata +9 -4
- data/lib/bdd_openai/files/client.rb +0 -71
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 56cdede8702f4004ca214db560794e5d37773889ebf0b3c09a8cd6b87fc63940
|
4
|
+
data.tar.gz: 1fbffb02950082c214b189e6505465bde18eb07b020b9ead9f47e3a95bdb44a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eec6abbce93c90bad22bf7fe8c6b8d8878f4cffa2588e92d0ca635ca78ba17ccbb506e45e4e37c599022d9ec3b921c5ab9bb065695923053f18ec3d79ec2e485
|
7
|
+
data.tar.gz: 29502804936673dfda3d9a708df7ad65c6741363e7f79d98381a6b3419ed7e6846efb2646fc00864b0e68eb8d64a3b6b37bf81dc8d54568e906a023557c9f195
|
data/CHANGELOG.md
CHANGED
@@ -2,13 +2,15 @@
|
|
2
2
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
4
4
|
|
5
|
+
## [1.1.0] - 2023-12-13
|
6
|
+
|
7
|
+
### Changed
|
8
|
+
|
9
|
+
- Move BddOpenai::Files::Client to BddOpenai::FileClient
|
10
|
+
|
5
11
|
## [1.0.0] - 2023-12-10
|
6
12
|
|
7
13
|
### Added
|
8
14
|
|
9
15
|
- Init the Gem project using RubyMine template
|
10
16
|
- Create module BddOpenai::Files
|
11
|
-
|
12
|
-
### Changed
|
13
|
-
|
14
|
-
- N/A
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# BddOpenai
|
2
2
|
|
3
|
-
This gem is a personal exercise to get familiar with Ruby and Gem creating. gem Gem include only one top level namespace of TestOpenai which you can find all you need inside. Starting by creating an instance of BddOpenai::
|
3
|
+
This gem is a personal exercise to get familiar with Ruby and Gem creating. gem Gem include only one top level namespace of TestOpenai which you can find all you need inside. Starting by creating an instance of BddOpenai::FileClient.
|
4
4
|
|
5
5
|
For this exercise, the client will provide wrapper to some of OpenAI File APIs. API doc can bbe found here:
|
6
6
|
- https://platform.openai.com/docs/api-reference/files
|
@@ -31,7 +31,7 @@ You can direct interact with gem by:
|
|
31
31
|
```ruby
|
32
32
|
require "bdd_openai"
|
33
33
|
|
34
|
-
client = BddOpenai::
|
34
|
+
client = BddOpenai::FileClient.new(ENV["OPENAI_API_KEY"])
|
35
35
|
|
36
36
|
client.list_files
|
37
37
|
|
@@ -5,26 +5,26 @@ module BddOpenai
|
|
5
5
|
module Client
|
6
6
|
# An HTTP client
|
7
7
|
class HttpClient
|
8
|
-
# @param uri [
|
8
|
+
# @param uri [URI::Generic]
|
9
9
|
# @param headers [Hash]
|
10
10
|
# @param disable_ssl [Boolean]
|
11
11
|
# @return [Net::HTTPResponse]
|
12
12
|
def call_get(uri, headers, disable_ssl: false)
|
13
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
13
|
+
http = Net::HTTP.new(uri.host || '', uri.port)
|
14
14
|
http.use_ssl = disable_ssl ? false : true
|
15
|
-
request = Net::HTTP::Get.new(uri.path, headers)
|
15
|
+
request = Net::HTTP::Get.new(uri.path || '', headers)
|
16
16
|
http.request(request)
|
17
17
|
end
|
18
18
|
|
19
|
-
# @param uri [
|
19
|
+
# @param uri [URI::Generic]
|
20
20
|
# @param req_body [String] Request body in the form of JSON string.
|
21
21
|
# @param headers [Hash]
|
22
22
|
# @param disable_ssl [Boolean]
|
23
23
|
# @return [Net::HTTPResponse]
|
24
24
|
def call_post(uri, req_body, headers, disable_ssl: false)
|
25
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
25
|
+
http = Net::HTTP.new(uri.host || '', uri.port)
|
26
26
|
http.use_ssl = disable_ssl ? false : true
|
27
|
-
request = Net::HTTP::Post.new(uri.path, headers)
|
27
|
+
request = Net::HTTP::Post.new(uri.path || '', headers)
|
28
28
|
request.body = req_body unless req_body.nil?
|
29
29
|
http.request(request)
|
30
30
|
end
|
@@ -50,14 +50,14 @@ module BddOpenai
|
|
50
50
|
[body, boundary]
|
51
51
|
end
|
52
52
|
|
53
|
-
# @param uri [
|
53
|
+
# @param uri [URI::Generic]
|
54
54
|
# @param headers [Hash]
|
55
55
|
# @param disable_ssl [Boolean]
|
56
56
|
# @return [Net::HTTPResponse]
|
57
57
|
def call_delete(uri, headers, disable_ssl: false)
|
58
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
58
|
+
http = Net::HTTP.new(uri.host || '', uri.port)
|
59
59
|
http.use_ssl = disable_ssl ? false : true
|
60
|
-
request = Net::HTTP::Delete.new(uri.path, headers)
|
60
|
+
request = Net::HTTP::Delete.new(uri.path || '', headers)
|
61
61
|
http.request(request)
|
62
62
|
end
|
63
63
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'services/files/delete'
|
4
|
+
require_relative 'services/files/list'
|
5
|
+
require_relative 'services/files/retrieve'
|
6
|
+
require_relative 'services/files/upload'
|
7
|
+
|
8
|
+
module BddOpenai
|
9
|
+
# Client for OpenAI Files API
|
10
|
+
# Ref: https://platform.openai.com/docs/api-reference/files
|
11
|
+
class FileClient
|
12
|
+
# @param api_key [String] The key of the OpenAI API
|
13
|
+
attr_accessor :api_key
|
14
|
+
|
15
|
+
def initialize(api_key = '')
|
16
|
+
@api_key = api_key
|
17
|
+
end
|
18
|
+
|
19
|
+
# @return [Array<BddOpenai::Mapper::File>, BddOpenai::ErrorResponse]
|
20
|
+
def list_files
|
21
|
+
BddOpenai::Services::Files::List.new(@api_key).list_files
|
22
|
+
end
|
23
|
+
|
24
|
+
# @param purpose [String] The intended purpose of the file. One of: "fine-tune", "assistants".
|
25
|
+
# @param file_path [String] The path of the file to upload.rb
|
26
|
+
# @return [BddOpenai::Mapper::File, BddOpenai::ErrorResponse]
|
27
|
+
def upload_file(purpose, file_path)
|
28
|
+
BddOpenai::Services::Files::Upload.new(@api_key).upload_files(purpose, file_path)
|
29
|
+
end
|
30
|
+
|
31
|
+
# @param file_id [String] The id of the file to delete
|
32
|
+
# @return [true, BddOpenai::ErrorResponse]
|
33
|
+
def delete_file(file_id)
|
34
|
+
BddOpenai::Services::Files::Delete.new(@api_key).delete_file(file_id)
|
35
|
+
end
|
36
|
+
|
37
|
+
# @param file_id [String] The id of the file to retrieve
|
38
|
+
# @return [BddOpenai::Mapper::File, BddOpenai::ErrorResponse]
|
39
|
+
def retrieve_file(file_id)
|
40
|
+
BddOpenai::Services::Files::Retrieve.new(@api_key).retrieve_file(file_id)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BddOpenai
|
4
|
+
module Services
|
5
|
+
module Files
|
6
|
+
class Base
|
7
|
+
# @param api_key [String] The key of the OpenAI API
|
8
|
+
def initialize(api_key = '')
|
9
|
+
@http_client = BddOpenai::Client::HttpClient.new
|
10
|
+
@openai_api_domain = 'https://api.openai.com/v1'
|
11
|
+
@openai_api_key = api_key
|
12
|
+
end
|
13
|
+
|
14
|
+
def default_headers
|
15
|
+
{
|
16
|
+
"Authorization": "Bearer #{@openai_api_key}",
|
17
|
+
"Content-Type": 'application/json'
|
18
|
+
}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'base'
|
4
|
+
|
5
|
+
module BddOpenai
|
6
|
+
module Services
|
7
|
+
module Files
|
8
|
+
class Delete < Base
|
9
|
+
# @param file_id [String] The id of the file to delete
|
10
|
+
# @return [true, BddOpenai::ErrorResponse]
|
11
|
+
def delete_file(file_id)
|
12
|
+
uri = URI.parse("#{@openai_api_domain}/files/#{file_id}")
|
13
|
+
response = @http_client.call_delete(uri, default_headers)
|
14
|
+
return BddOpenai::ErrorResponse.from_json(response.body) unless response.code == '200'
|
15
|
+
|
16
|
+
true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'base'
|
4
|
+
|
5
|
+
module BddOpenai
|
6
|
+
module Services
|
7
|
+
module Files
|
8
|
+
class List < Base
|
9
|
+
# @return [Array<BddOpenai::Mapper::File>, BddOpenai::ErrorResponse]
|
10
|
+
def list_files
|
11
|
+
uri = URI.parse("#{@openai_api_domain}/files")
|
12
|
+
response = @http_client.call_get(uri, default_headers)
|
13
|
+
return BddOpenai::ErrorResponse.from_json(response.body) unless response.code == '200'
|
14
|
+
|
15
|
+
JSON.parse(response.body)['data'].map do |file|
|
16
|
+
BddOpenai::Mapper::File.from_json(file.to_json)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'base'
|
4
|
+
|
5
|
+
module BddOpenai
|
6
|
+
module Services
|
7
|
+
module Files
|
8
|
+
class Retrieve < Base
|
9
|
+
# @param file_id [String] The id of the file to retrieve
|
10
|
+
# @return [BddOpenai::Mapper::File, BddOpenai::ErrorResponse]
|
11
|
+
def retrieve_file(file_id)
|
12
|
+
uri = URI.parse("#{@openai_api_domain}/files/#{file_id}")
|
13
|
+
response = @http_client.call_get(uri, default_headers)
|
14
|
+
return BddOpenai::ErrorResponse.from_json(response.body) unless response.code == '200'
|
15
|
+
|
16
|
+
BddOpenai::Mapper::File.from_json(response.body)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'base'
|
4
|
+
|
5
|
+
module BddOpenai
|
6
|
+
module Services
|
7
|
+
module Files
|
8
|
+
class Upload < Base
|
9
|
+
# @param purpose [String] The intended purpose of the file. One of: "fine-tune", "assistants".
|
10
|
+
# @param file_path [String] The path of the file to upload.rb
|
11
|
+
# @return [BddOpenai::Mapper::File, BddOpenai::ErrorResponse]
|
12
|
+
def upload_files(purpose, file_path)
|
13
|
+
uri = URI.parse("#{@openai_api_domain}/files")
|
14
|
+
body, boundary = @http_client.create_multipart_body({ purpose: purpose }, { file: file_path })
|
15
|
+
headers = default_headers
|
16
|
+
.merge({
|
17
|
+
"Content-Type": "multipart/form-data; boundary=#{boundary}"
|
18
|
+
})
|
19
|
+
response = @http_client.call_post(uri, body, headers)
|
20
|
+
return BddOpenai::ErrorResponse.from_json(response.body) unless response.code == '200'
|
21
|
+
|
22
|
+
BddOpenai::Mapper::File.from_json(response.body)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/bdd_openai/version.rb
CHANGED
data/lib/bdd_openai.rb
CHANGED
@@ -5,6 +5,6 @@ require 'net/http'
|
|
5
5
|
require 'securerandom'
|
6
6
|
require_relative 'bdd_openai/clients/http'
|
7
7
|
require_relative 'bdd_openai/error_response'
|
8
|
-
require_relative 'bdd_openai/
|
9
|
-
require_relative 'bdd_openai/
|
8
|
+
require_relative 'bdd_openai/file_client'
|
9
|
+
require_relative 'bdd_openai/mappers/file'
|
10
10
|
require_relative 'bdd_openai/version'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bdd_openai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brendon Dao
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-12-
|
11
|
+
date: 2023-12-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Write a longer description or delete this line.
|
14
14
|
email:
|
@@ -32,8 +32,13 @@ files:
|
|
32
32
|
- lib/bdd_openai.rb
|
33
33
|
- lib/bdd_openai/clients/http.rb
|
34
34
|
- lib/bdd_openai/error_response.rb
|
35
|
-
- lib/bdd_openai/
|
36
|
-
- lib/bdd_openai/
|
35
|
+
- lib/bdd_openai/file_client.rb
|
36
|
+
- lib/bdd_openai/mappers/file.rb
|
37
|
+
- lib/bdd_openai/services/files/base.rb
|
38
|
+
- lib/bdd_openai/services/files/delete.rb
|
39
|
+
- lib/bdd_openai/services/files/list.rb
|
40
|
+
- lib/bdd_openai/services/files/retrieve.rb
|
41
|
+
- lib/bdd_openai/services/files/upload.rb
|
37
42
|
- lib/bdd_openai/version.rb
|
38
43
|
homepage: https://github.com/brendondaoateh/bdd_openai
|
39
44
|
licenses:
|
@@ -1,71 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module BddOpenai
|
4
|
-
# Module for OpenAI Files API
|
5
|
-
# Ref: https://platform.openai.com/docs/api-reference/files
|
6
|
-
module Files
|
7
|
-
# Client for OpenAI Files API
|
8
|
-
class Client
|
9
|
-
# @param api_key [String] The key of the OpenAI API
|
10
|
-
def initialize(api_key = '')
|
11
|
-
@http_client = BddOpenai::Client::HttpClient.new
|
12
|
-
@openai_api_domain = 'https://api.openai.com/v1'
|
13
|
-
@openai_api_key = api_key
|
14
|
-
end
|
15
|
-
|
16
|
-
def default_headers
|
17
|
-
{
|
18
|
-
"Authorization": "Bearer #{@openai_api_key}",
|
19
|
-
"Content-Type": 'application/json'
|
20
|
-
}
|
21
|
-
end
|
22
|
-
|
23
|
-
# @return [Array<BddOpenai::Files::File>, BddOpenai::ErrorResponse]
|
24
|
-
def list_files
|
25
|
-
uri = URI.parse("#{@openai_api_domain}/files")
|
26
|
-
response = @http_client.call_get(uri, default_headers)
|
27
|
-
return BddOpenai::ErrorResponse.from_json(response.body) unless response.code == '200'
|
28
|
-
|
29
|
-
JSON.parse(response.body)['data'].map do |file|
|
30
|
-
BddOpenai::Files::File.from_json(file.to_json)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
# @param purpose [String] The intended purpose of the file. One of: "fine-tune", "assistants".
|
35
|
-
# @param file_path [String] The path of the file to upload
|
36
|
-
# @return [BddOpenai::Files::File, BddOpenai::ErrorResponse]
|
37
|
-
def upload_file(purpose, file_path)
|
38
|
-
uri = URI.parse("#{@openai_api_domain}/files")
|
39
|
-
body, boundary = @http_client.create_multipart_body({ purpose: purpose }, { file: file_path })
|
40
|
-
headers = default_headers
|
41
|
-
.merge({
|
42
|
-
"Content-Type": "multipart/form-data; boundary=#{boundary}"
|
43
|
-
})
|
44
|
-
response = @http_client.call_post(uri, body, headers)
|
45
|
-
return BddOpenai::ErrorResponse.from_json(response.body) unless response.code == '200'
|
46
|
-
|
47
|
-
BddOpenai::Files::File.from_json(response.body)
|
48
|
-
end
|
49
|
-
|
50
|
-
# @param file_id [String] The id of the file to delete
|
51
|
-
# @return [true, BddOpenai::ErrorResponse]
|
52
|
-
def delete_file(file_id)
|
53
|
-
uri = URI.parse("#{@openai_api_domain}/files/#{file_id}")
|
54
|
-
response = @http_client.call_delete(uri, default_headers)
|
55
|
-
return BddOpenai::ErrorResponse.from_json(response.body) unless response.code == '200'
|
56
|
-
|
57
|
-
true
|
58
|
-
end
|
59
|
-
|
60
|
-
# @param file_id [String] The id of the file to retrieve
|
61
|
-
# @return [BddOpenai::Files::File, BddOpenai::ErrorResponse]
|
62
|
-
def retrieve_file(file_id)
|
63
|
-
uri = URI.parse("#{@openai_api_domain}/files/#{file_id}")
|
64
|
-
response = @http_client.call_get(uri, default_headers)
|
65
|
-
return BddOpenai::ErrorResponse.from_json(response.body) unless response.code == '200'
|
66
|
-
|
67
|
-
BddOpenai::Files::File.from_json(response.body)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|