Class: TestOpenai::Client
- Inherits:
-
Object
- Object
- TestOpenai::Client
- Defined in:
- lib/client.rb
Instance Attribute Summary collapse
-
#openai_host ⇒ Object
Returns the value of attribute openai_host.
-
#openai_key ⇒ Object
Returns the value of attribute openai_key.
Instance Method Summary collapse
-
#delete_file(file_id) ⇒ TestOpenai::DeleteFileResp
The response of the deletion.
-
#get_file(file_id) ⇒ TestOpenai::File
The file gotten.
- #get_file_content(file_id) ⇒ Object
-
#initialize(openai_host = 'https://api.openai.com', openai_key = '') ⇒ TestOpenai::Client
constructor
A new instance of TestOpenai::OpenAIClient.
-
#list_files ⇒ TestOpenai::ListFilesResp
The list of files.
- #parse_as_file(data) ⇒ Object
-
#upload_file(file_path) ⇒ TestOpenai::File
The file uploaded.
Constructor Details
#initialize(openai_host = 'https://api.openai.com', openai_key = '') ⇒ TestOpenai::Client
Returns A new instance of TestOpenai::OpenAIClient.
15 16 17 18 |
# File 'lib/client.rb', line 15 def initialize(openai_host = 'https://api.openai.com', openai_key = '') @openai_host = openai_host @openai_key = openai_key end |
Instance Attribute Details
#openai_host ⇒ Object
Returns the value of attribute openai_host.
10 11 12 |
# File 'lib/client.rb', line 10 def openai_host @openai_host end |
#openai_key ⇒ Object
Returns the value of attribute openai_key.
10 11 12 |
# File 'lib/client.rb', line 10 def openai_key @openai_key end |
Instance Method Details
#delete_file(file_id) ⇒ TestOpenai::DeleteFileResp
Returns The response of the deletion.
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/client.rb', line 56 def delete_file(file_id) url = "#{openai_host}/files/#{file_id}" headers = { Authorization: "Bearer #{openai_key}" } res = RestClient.delete(url, headers) response_data = JSON.parse(res.body) TestOpenai::DeleteFileResp.new( object: response_data['object'], id: response_data['id'], deleted: response_data['deleted'] ) end |
#get_file(file_id) ⇒ TestOpenai::File
Returns The file gotten.
72 73 74 75 76 77 78 79 |
# File 'lib/client.rb', line 72 def get_file(file_id) url = "#{openai_host}/files/#{file_id}" headers = { Authorization: "Bearer #{openai_key}" } res = RestClient.get(url, headers) parse_as_file(JSON.parse(res.body)) end |
#get_file_content(file_id) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/client.rb', line 81 def get_file_content(file_id) url = "#{openai_host}/files/#{file_id}/content" headers = { Authorization: "Bearer #{openai_key}" } res = RestClient.get(url, headers) res.body end |
#list_files ⇒ TestOpenai::ListFilesResp
Returns The list of files.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/client.rb', line 21 def list_files url = "#{openai_host}/files" headers = { Authorization: "Bearer #{openai_key}" } res = RestClient.get(url, headers) response_data = JSON.parse(res.body) file_data = response_data['data'] TestOpenai::ListFilesResp.new( object: response_data['object'], has_more: response_data['has_more'], data: file_data.map do |file| parse_as_file(file) end ) end |
#parse_as_file(data) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/client.rb', line 91 def parse_as_file(data) TestOpenai::File.new( object: data['object'], id: data['id'], purpose: data['purpose'], filename: data['filename'], bytes: data['bytes'], created_at: data['created_at'] ) end |
#upload_file(file_path) ⇒ TestOpenai::File
Returns The file uploaded.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/client.rb', line 41 def upload_file(file_path) url = "#{openai_host}/files" headers = { Authorization: "Bearer #{openai_key}" } params = { purpose: 'assistants', file: ::File.new(file_path) } res = RestClient.post(url, params, headers) parse_as_file(JSON.parse(res.body)) end |