Class: OpenAIHelperGem::Client

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



16
17
18
# File 'lib/openai_helper_gem/client.rb', line 16

def api_key
  @api_key
end

.default_audio_pathObject

Returns the value of attribute default_audio_path.



16
17
18
# File 'lib/openai_helper_gem/client.rb', line 16

def default_audio_path
  @default_audio_path
end

.notification_methodObject

Returns the value of attribute notification_method.



16
17
18
# File 'lib/openai_helper_gem/client.rb', line 16

def notification_method
  @notification_method
end

Class Method Details

.extract_structured_output(response) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/openai_helper_gem/client.rb', line 89

def extract_structured_output(response)
  if response['error']
    notify_error("API Error", response['error']['message'])
    exit 1
  end

  function_call = response.dig('choices', 0, 'message', 'function_call')
  unless function_call && function_call['arguments']
    notify_error("Invalid Response", "No structured output found in the response.")
    exit 1
  end

  begin
    JSON.parse(function_call['arguments'])
  rescue JSON::ParserError => e
    notify_error("JSON Parsing Error", e.message)
    exit 1
  end
end

.generate_speech(text, output_file_name = "output.mp3", autoplay: true) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/openai_helper_gem/client.rb', line 60

def generate_speech(text, output_file_name = "output.mp3", autoplay: true)
  request_data = { model: 'tts-1-hd', voice: 'alloy', input: text }

  begin
    http = Net::HTTP.new(@tts_api_endpoint.host, @tts_api_endpoint.port)
    http.use_ssl = true
    request = Net::HTTP::Post.new(@tts_api_endpoint.path, {
      'Content-Type': 'application/json',
      'Authorization': "Bearer #{@api_key}"
    })
    request.body = request_data.to_json
    response = http.request(request)

    output_path = File.join(@default_audio_path, output_file_name)
    FileUtils.mkdir_p(File.dirname(output_path))
    File.open(output_path, 'wb') { |file| file.write(response.body) }

    puts "Audio saved to #{output_path}"
    play_audio(output_path) if autoplay
  rescue StandardError => e
    notify_error("Text-to-Speech Error", e.message)
    exit 1
  end
end

.notify_error(title, message) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/openai_helper_gem/client.rb', line 115

def notify_error(title, message)
  case @notification_method
  when :mac_os
    system("osascript -e 'display notification \"#{message}\" with title \"#{title}\"'")
  when :puts
    puts "[#{title}] #{message}"
  when :none
    # No output
  else
    puts "[Unknown Notification Method] #{title}: #{message}"
  end
end

.perform_request(messages, schema, file_path = nil) ⇒ Object



109
110
111
112
113
# File 'lib/openai_helper_gem/client.rb', line 109

def perform_request(messages, schema, file_path = nil)
  file_id = upload_file(file_path) if file_path
  response = send_message_with_file(messages, file_id) if file_id
  extract_structured_output(response)
end

.play_audio(file_path) ⇒ Object



85
86
87
# File 'lib/openai_helper_gem/client.rb', line 85

def play_audio(file_path)
  system("afplay '#{file_path}'")
end

.send_message_with_file(messages, file_id) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/openai_helper_gem/client.rb', line 41

def send_message_with_file(messages, file_id)
  request_data = { model: ENV['GPT_MODEL'] || 'gpt-3.5-turbo', messages: messages, file_ids: [file_id] }

  begin
    http = Net::HTTP.new(@chat_completion_endpoint.host, @chat_completion_endpoint.port)
    http.use_ssl = true
    request = Net::HTTP::Post.new(@chat_completion_endpoint.path, {
      'Content-Type': 'application/json',
      'Authorization': "Bearer #{@api_key}"
    })
    request.body = request_data.to_json
    response = http.request(request)
    JSON.parse(response.body)
  rescue StandardError => e
    notify_error("API Request Failed", e.message)
    exit 1
  end
end

.upload_file(file_path) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/openai_helper_gem/client.rb', line 18

def upload_file(file_path)
  file = File.open(file_path, 'rb')
  request_data = { purpose: 'assistant', file: file }

  begin
    http = Net::HTTP.new(@file_upload_endpoint.host, @file_upload_endpoint.port)
    http.use_ssl = true
    request = Net::HTTP::Post.new(@file_upload_endpoint.path, { 'Authorization': "Bearer #{@api_key}" })
    request.set_form([['file', file]], 'multipart/form-data')
    response = http.request(request)
    file.close
    result = JSON.parse(response.body)
    if result['error']
      notify_error("File Upload Failed", result['error']['message'])
      exit 1
    end
    result['id']
  rescue StandardError => e
    notify_error("File Upload Error", e.message)
    exit 1
  end
end