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_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.4.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, timeout: nil, max_output_tokens: nil) ⇒ AiLite

Returns a new instance of AiLite.

Raises:

  • (ArgumentError)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ai_lite.rb', line 81

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



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

def api_key
  @api_key
end

#embedding_modelObject (readonly)

Returns the value of attribute embedding_model.



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

def embedding_model
  @embedding_model
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#image_modelObject (readonly)

Returns the value of attribute image_model.



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

def image_model
  @image_model
end

#max_output_tokensObject (readonly)

Returns the value of attribute max_output_tokens.



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

def max_output_tokens
  @max_output_tokens
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#moderation_modelObject (readonly)

Returns the value of attribute moderation_model.



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

def moderation_model
  @moderation_model
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

Class Method Details

.chat(message, **kwargs) ⇒ Object



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

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

.clientObject



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

def client
  @client ||= new
end

.configurationObject



38
39
40
# File 'lib/ai_lite.rb', line 38

def configuration
  @configuration ||= Configuration.new
end

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

Yields:



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

def configure
  yield(configuration)
  reset_client!
  configuration
end

.embed(input, **kwargs) ⇒ Object



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

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

.image(prompt, **kwargs) ⇒ Object



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

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

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



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

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

.reset_client!Object



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

def reset_client!
  @client = nil
end

.reset_configuration!Object



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

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

Instance Method Details

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



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ai_lite.rb', line 97

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



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

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ai_lite.rb', line 134

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



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

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