Class: AiLite

Inherits:
Object
  • Object
show all
Defined in:
lib/ai_lite.rb,
lib/ai_lite/version.rb

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

API_BASE_URL =
"https://api.openai.com/v1".freeze
DEFAULT_MODEL =
"gpt-5.5".freeze
DEFAULT_MODERATION_MODEL =
"omni-moderation-latest".freeze
DEFAULT_EMBEDDING_MODEL =
"text-embedding-3-small".freeze
DEFAULT_IMAGE_MODEL =
"gpt-image-2".freeze
DEFAULT_SPEECH_MODEL =
"gpt-4o-mini-tts".freeze
DEFAULT_SPEECH_VOICE =
"alloy".freeze
DEFAULT_SPEECH_FORMAT =
"mp3".freeze
DEFAULT_TRANSCRIPTION_MODEL =
"gpt-transcribe".freeze
DEFAULT_TIMEOUT =
120
DEFAULT_MAX_OUTPUT_TOKENS =
2000
IMAGE_MIME_TYPES =
{
  ".gif" => "image/gif",
  ".jpeg" => "image/jpeg",
  ".jpg" => "image/jpeg",
  ".png" => "image/png",
  ".webp" => "image/webp"
}.freeze
AUDIO_MIME_TYPES =
{
  ".flac" => "audio/flac",
  ".m4a" => "audio/mp4",
  ".mp3" => "audio/mpeg",
  ".mp4" => "audio/mp4",
  ".mpeg" => "audio/mpeg",
  ".mpga" => "audio/mpeg",
  ".ogg" => "audio/ogg",
  ".wav" => "audio/wav",
  ".webm" => "audio/webm"
}.freeze
VERSION =
"0.6.0".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, model: nil, moderation_model: nil, embedding_model: nil, image_model: nil, speech_model: nil, speech_voice: nil, transcription_model: nil, timeout: nil, max_output_tokens: nil) ⇒ AiLite

Returns a new instance of AiLite.

Raises:

  • (ArgumentError)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ai_lite.rb', line 108

def initialize(api_key: nil, model: nil, moderation_model: nil, embedding_model: nil, image_model: nil, speech_model: nil, speech_voice: nil, transcription_model: nil, timeout: nil, max_output_tokens: nil)
  @api_key = api_key || self.class.configuration.api_key || ENV["OPENAI_API_KEY"] || ENV["OPEN_AI_TOKEN"]
  raise ArgumentError, "Missing OpenAI API key" if @api_key.to_s.strip.empty?

  @model = model || self.class.configuration.model
  @moderation_model = moderation_model || self.class.configuration.moderation_model
  @embedding_model = embedding_model || self.class.configuration.embedding_model
  @image_model = image_model || self.class.configuration.image_model
  @speech_model = speech_model || self.class.configuration.speech_model
  @speech_voice = speech_voice || self.class.configuration.speech_voice
  @transcription_model = transcription_model || self.class.configuration.transcription_model
  @timeout = timeout || self.class.configuration.timeout
  @max_output_tokens = max_output_tokens || self.class.configuration.max_output_tokens
  @headers = {
    "Authorization" => "Bearer #{@api_key}",
    "Content-Type" => "application/json"
  }
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



106
107
108
# File 'lib/ai_lite.rb', line 106

def api_key
  @api_key
end

#embedding_modelObject (readonly)

Returns the value of attribute embedding_model.



106
107
108
# File 'lib/ai_lite.rb', line 106

def embedding_model
  @embedding_model
end

#headersObject (readonly)

Returns the value of attribute headers.



106
107
108
# File 'lib/ai_lite.rb', line 106

def headers
  @headers
end

#image_modelObject (readonly)

Returns the value of attribute image_model.



106
107
108
# File 'lib/ai_lite.rb', line 106

def image_model
  @image_model
end

#max_output_tokensObject (readonly)

Returns the value of attribute max_output_tokens.



106
107
108
# File 'lib/ai_lite.rb', line 106

def max_output_tokens
  @max_output_tokens
end

#modelObject (readonly)

Returns the value of attribute model.



106
107
108
# File 'lib/ai_lite.rb', line 106

def model
  @model
end

#moderation_modelObject (readonly)

Returns the value of attribute moderation_model.



106
107
108
# File 'lib/ai_lite.rb', line 106

def moderation_model
  @moderation_model
end

#speech_modelObject (readonly)

Returns the value of attribute speech_model.



106
107
108
# File 'lib/ai_lite.rb', line 106

def speech_model
  @speech_model
end

#speech_voiceObject (readonly)

Returns the value of attribute speech_voice.



106
107
108
# File 'lib/ai_lite.rb', line 106

def speech_voice
  @speech_voice
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



106
107
108
# File 'lib/ai_lite.rb', line 106

def timeout
  @timeout
end

#transcription_modelObject (readonly)

Returns the value of attribute transcription_model.



106
107
108
# File 'lib/ai_lite.rb', line 106

def transcription_model
  @transcription_model
end

Class Method Details

.chat(message, **kwargs) ⇒ Object



77
78
79
# File 'lib/ai_lite.rb', line 77

def chat(message, **kwargs)
  client.chat(message, **kwargs)
end

.clientObject



73
74
75
# File 'lib/ai_lite.rb', line 73

def client
  @client ||= new
end

.configurationObject



57
58
59
# File 'lib/ai_lite.rb', line 57

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



61
62
63
64
65
# File 'lib/ai_lite.rb', line 61

def configure
  yield(configuration)
  reset_client!
  configuration
end

.embed(input, **kwargs) ⇒ Object



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

def embed(input, **kwargs)
  client.embed(input, **kwargs)
end

.image(prompt, **kwargs) ⇒ Object



89
90
91
# File 'lib/ai_lite.rb', line 89

def image(prompt, **kwargs)
  client.image(prompt, **kwargs)
end

.moderate(input = nil, **kwargs) ⇒ Object



81
82
83
# File 'lib/ai_lite.rb', line 81

def moderate(input = nil, **kwargs)
  client.moderate(input, **kwargs)
end

.reset_client!Object



101
102
103
# File 'lib/ai_lite.rb', line 101

def reset_client!
  @client = nil
end

.reset_configuration!Object



67
68
69
70
71
# File 'lib/ai_lite.rb', line 67

def reset_configuration!
  @configuration = Configuration.new
  reset_client!
  configuration
end

.speak(text, **kwargs) ⇒ Object



93
94
95
# File 'lib/ai_lite.rb', line 93

def speak(text, **kwargs)
  client.speak(text, **kwargs)
end

.transcribe(file_path, **kwargs) ⇒ Object



97
98
99
# File 'lib/ai_lite.rb', line 97

def transcribe(file_path, **kwargs)
  client.transcribe(file_path, **kwargs)
end

Instance Method Details

#chat(message, model: nil, instructions: nil, max_output_tokens: nil, debug: false, options: {}) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ai_lite.rb', line 127

def chat(message, model: nil, instructions: nil, max_output_tokens: nil, debug: false, options: {})
  payload = options.merge(
    model: model || self.model,
    input: message,
    max_output_tokens: max_output_tokens || self.max_output_tokens
  )
  payload[:instructions] = instructions if instructions

  extract_content(post(payload), debug: debug)
rescue => e
  prettify_data(status: "unknown", error: e.message, raw: nil, debug: debug)
end

#embed(input, model: nil, dimensions: nil, encoding_format: nil, debug: false, options: {}) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/ai_lite.rb', line 151

def embed(input, model: nil, dimensions: nil, encoding_format: nil, debug: false, options: {})
  payload = options.merge(
    model: model || embedding_model,
    input: input
  )
  payload[:dimensions] = dimensions if dimensions
  payload[:encoding_format] = encoding_format if encoding_format

  extract_embedding(post(payload, endpoint: embedding_endpoint), multiple: input.is_a?(Array), debug: debug)
rescue => e
  prettify_data(status: "unknown", error: e.message, raw: nil, debug: debug)
end

#image(prompt, model: nil, size: nil, quality: nil, background: nil, output_format: nil, output_path: nil, debug: false, options: {}) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/ai_lite.rb', line 164

def image(prompt, model: nil, size: nil, quality: nil, background: nil, output_format: nil, output_path: nil, debug: false, options: {})
  payload = options.merge(
    model: model || image_model,
    prompt: prompt
  )
  payload[:size] = size if size
  payload[:quality] = quality if quality
  payload[:background] = background if background
  payload[:output_format] = output_format if output_format

  extract_image(post(payload, endpoint: image_endpoint), output_path: output_path, debug: debug)
rescue => e
  prettify_data(status: "unknown", error: e.message, raw: nil, debug: debug)
end

#moderate(input = nil, model: nil, text: nil, image_url: nil, image_path: nil, debug: false, options: {}) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'lib/ai_lite.rb', line 140

def moderate(input = nil, model: nil, text: nil, image_url: nil, image_path: nil, debug: false, options: {})
  payload = options.merge(
    model: model || moderation_model,
    input: moderation_input(input, text: text, image_url: image_url, image_path: image_path)
  )

  extract_moderation(post(payload, endpoint: moderation_endpoint), debug: debug)
rescue => e
  prettify_data(status: "unknown", error: e.message, raw: nil, debug: debug)
end

#speak(text, model: nil, voice: nil, response_format: nil, speed: nil, instructions: nil, output_path: nil, base64: false, debug: false, options: {}) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ai_lite.rb', line 179

def speak(text, model: nil, voice: nil, response_format: nil, speed: nil, instructions: nil, output_path: nil, base64: false, debug: false, options: {})
  payload = options.merge(
    model: model || speech_model,
    input: text,
    voice: voice || speech_voice
  )
  payload[:response_format] = response_format if response_format
  payload[:speed] = speed if speed
  payload[:instructions] = instructions if instructions

  extract_speech(
    post(payload, endpoint: speech_endpoint),
    output_path: output_path,
    base64: base64,
    response_format: payload[:response_format] || payload["response_format"] || DEFAULT_SPEECH_FORMAT,
    debug: debug
  )
rescue => e
  prettify_data(status: "unknown", error: e.message, raw: nil, debug: debug)
end

#transcribe(file_path, model: nil, language: nil, prompt: nil, response_format: nil, temperature: nil, timestamp_granularities: nil, debug: false, options: {}) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/ai_lite.rb', line 200

def transcribe(file_path, model: nil, language: nil, prompt: nil, response_format: nil, temperature: nil, timestamp_granularities: nil, debug: false, options: {})
  fields = options.merge(
    model: model || transcription_model
  )
  fields[:language] = language if language
  fields[:prompt] = prompt if prompt
  fields[:response_format] = response_format if response_format
  fields[:temperature] = temperature unless temperature.nil?
  fields[:timestamp_granularities] = timestamp_granularities if timestamp_granularities

  extract_transcription(
    post_multipart(fields, file_field: audio_file_field(file_path), endpoint: transcription_endpoint),
    debug: debug
  )
rescue => e
  prettify_data(status: "unknown", error: e.message, raw: nil, debug: debug)
end