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_TIMEOUT =
120
DEFAULT_MAX_OUTPUT_TOKENS =
2000
VERSION =
"0.1.0".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, model: nil, timeout: nil, max_output_tokens: nil) ⇒ AiLite

Returns a new instance of AiLite.

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ai_lite.rb', line 55

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



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

def api_key
  @api_key
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#max_output_tokensObject (readonly)

Returns the value of attribute max_output_tokens.



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

def max_output_tokens
  @max_output_tokens
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

Class Method Details

.chat(message, **kwargs) ⇒ Object



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

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

.clientObject



40
41
42
# File 'lib/ai_lite.rb', line 40

def client
  @client ||= new
end

.configurationObject



24
25
26
# File 'lib/ai_lite.rb', line 24

def configuration
  @configuration ||= Configuration.new
end

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

Yields:



28
29
30
31
32
# File 'lib/ai_lite.rb', line 28

def configure
  yield(configuration)
  reset_client!
  configuration
end

.reset_client!Object



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

def reset_client!
  @client = nil
end

.reset_configuration!Object



34
35
36
37
38
# File 'lib/ai_lite.rb', line 34

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



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ai_lite.rb', line 68

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