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_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
VERSION =
"0.5.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, timeout: nil, max_output_tokens: nil) ⇒ AiLite

Returns a new instance of AiLite.

Raises:

  • (ArgumentError)


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

def initialize(api_key: nil, model: nil, moderation_model: nil, embedding_model: nil, image_model: nil, speech_model: nil, speech_voice: 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
  @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.



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

def api_key
  @api_key
end

#embedding_modelObject (readonly)

Returns the value of attribute embedding_model.



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

def embedding_model
  @embedding_model
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#image_modelObject (readonly)

Returns the value of attribute image_model.



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

def image_model
  @image_model
end

#max_output_tokensObject (readonly)

Returns the value of attribute max_output_tokens.



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

def max_output_tokens
  @max_output_tokens
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#moderation_modelObject (readonly)

Returns the value of attribute moderation_model.



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

def moderation_model
  @moderation_model
end

#speech_modelObject (readonly)

Returns the value of attribute speech_model.



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

def speech_model
  @speech_model
end

#speech_voiceObject (readonly)

Returns the value of attribute speech_voice.



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

def speech_voice
  @speech_voice
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

Class Method Details

.chat(message, **kwargs) ⇒ Object



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

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

.clientObject



59
60
61
# File 'lib/ai_lite.rb', line 59

def client
  @client ||= new
end

.configurationObject



43
44
45
# File 'lib/ai_lite.rb', line 43

def configuration
  @configuration ||= Configuration.new
end

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

Yields:



47
48
49
50
51
# File 'lib/ai_lite.rb', line 47

def configure
  yield(configuration)
  reset_client!
  configuration
end

.embed(input, **kwargs) ⇒ Object



71
72
73
# File 'lib/ai_lite.rb', line 71

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

.image(prompt, **kwargs) ⇒ Object



75
76
77
# File 'lib/ai_lite.rb', line 75

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

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



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

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

.reset_client!Object



83
84
85
# File 'lib/ai_lite.rb', line 83

def reset_client!
  @client = nil
end

.reset_configuration!Object



53
54
55
56
57
# File 'lib/ai_lite.rb', line 53

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

.speak(text, **kwargs) ⇒ Object



79
80
81
# File 'lib/ai_lite.rb', line 79

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

Instance Method Details

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



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ai_lite.rb', line 108

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



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ai_lite.rb', line 132

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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ai_lite.rb', line 145

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



121
122
123
124
125
126
127
128
129
130
# File 'lib/ai_lite.rb', line 121

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



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

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