Class: TestOpenai::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(openai_host = 'https://api.openai.com', openai_key = '') ⇒ TestOpenai::Client

Returns A new instance of TestOpenai::OpenAIClient.

Parameters:

  • openai_host (String) (defaults to: 'https://api.openai.com')

    The host of the OpenAI API

  • openai_key (String) (defaults to: '')

    The key of the OpenAI API



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_hostObject

Returns the value of attribute openai_host.



10
11
12
# File 'lib/client.rb', line 10

def openai_host
  @openai_host
end

#openai_keyObject

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.

Parameters:

  • file_id (String)

    The id of the file to delete

Returns:



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.

Parameters:

  • file_id (String)

    The id of the file to get

Returns:



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_filesTestOpenai::ListFilesResp

Returns The list of files.

Returns:



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.

Parameters:

  • file_path (String)

    The path of the file to upload

Returns:



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