Class: AiLite
- Inherits:
-
Object
- Object
- AiLite
- 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
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#max_output_tokens ⇒ Object
readonly
Returns the value of attribute max_output_tokens.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Class Method Summary collapse
- .chat(message, **kwargs) ⇒ Object
- .client ⇒ Object
- .configuration ⇒ Object
- .configure {|configuration| ... } ⇒ Object
- .reset_client! ⇒ Object
- .reset_configuration! ⇒ Object
Instance Method Summary collapse
- #chat(message, model: nil, instructions: nil, max_output_tokens: nil, debug: false, options: {}) ⇒ Object
-
#initialize(api_key: nil, model: nil, timeout: nil, max_output_tokens: nil) ⇒ AiLite
constructor
A new instance of AiLite.
Constructor Details
#initialize(api_key: nil, model: nil, timeout: nil, max_output_tokens: nil) ⇒ AiLite
Returns a new instance of AiLite.
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_key ⇒ Object (readonly)
Returns the value of attribute api_key.
53 54 55 |
# File 'lib/ai_lite.rb', line 53 def api_key @api_key end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
53 54 55 |
# File 'lib/ai_lite.rb', line 53 def headers @headers end |
#max_output_tokens ⇒ Object (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 |
#model ⇒ Object (readonly)
Returns the value of attribute model.
53 54 55 |
# File 'lib/ai_lite.rb', line 53 def model @model end |
#timeout ⇒ Object (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(, **kwargs) client.chat(, **kwargs) end |
.client ⇒ Object
40 41 42 |
# File 'lib/ai_lite.rb', line 40 def client @client ||= new end |
.configuration ⇒ Object
24 25 26 |
# File 'lib/ai_lite.rb', line 24 def configuration @configuration ||= Configuration.new end |
.configure {|configuration| ... } ⇒ Object
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(, model: nil, instructions: nil, max_output_tokens: nil, debug: false, options: {}) payload = .merge( model: model || self.model, input: , 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., raw: nil, debug: debug) end |