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_MODERATION_MODEL =
"omni-moderation-latest".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.2.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.
-
#moderation_model ⇒ Object
readonly
Returns the value of attribute moderation_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
- .moderate(input = nil, **kwargs) ⇒ 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, moderation_model: nil, timeout: nil, max_output_tokens: nil) ⇒ AiLite
constructor
A new instance of AiLite.
- #moderate(input = nil, model: nil, text: nil, image_url: nil, image_path: nil, debug: false, options: {}) ⇒ Object
Constructor Details
#initialize(api_key: nil, model: nil, moderation_model: nil, timeout: nil, max_output_tokens: nil) ⇒ AiLite
Returns a new instance of AiLite.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/ai_lite.rb', line 69 def initialize(api_key: nil, model: nil, moderation_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 @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.
67 68 69 |
# File 'lib/ai_lite.rb', line 67 def api_key @api_key end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
67 68 69 |
# File 'lib/ai_lite.rb', line 67 def headers @headers end |
#max_output_tokens ⇒ Object (readonly)
Returns the value of attribute max_output_tokens.
67 68 69 |
# File 'lib/ai_lite.rb', line 67 def max_output_tokens @max_output_tokens end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
67 68 69 |
# File 'lib/ai_lite.rb', line 67 def model @model end |
#moderation_model ⇒ Object (readonly)
Returns the value of attribute moderation_model.
67 68 69 |
# File 'lib/ai_lite.rb', line 67 def moderation_model @moderation_model end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
67 68 69 |
# File 'lib/ai_lite.rb', line 67 def timeout @timeout end |
Class Method Details
.chat(message, **kwargs) ⇒ Object
54 55 56 |
# File 'lib/ai_lite.rb', line 54 def chat(, **kwargs) client.chat(, **kwargs) end |
.client ⇒ Object
50 51 52 |
# File 'lib/ai_lite.rb', line 50 def client @client ||= new end |
.configuration ⇒ Object
34 35 36 |
# File 'lib/ai_lite.rb', line 34 def configuration @configuration ||= Configuration.new end |
.configure {|configuration| ... } ⇒ Object
38 39 40 41 42 |
# File 'lib/ai_lite.rb', line 38 def configure yield(configuration) reset_client! configuration end |
.moderate(input = nil, **kwargs) ⇒ Object
58 59 60 |
# File 'lib/ai_lite.rb', line 58 def moderate(input = nil, **kwargs) client.moderate(input, **kwargs) end |
.reset_client! ⇒ Object
62 63 64 |
# File 'lib/ai_lite.rb', line 62 def reset_client! @client = nil end |
.reset_configuration! ⇒ Object
44 45 46 47 48 |
# File 'lib/ai_lite.rb', line 44 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
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ai_lite.rb', line 83 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 |
#moderate(input = nil, model: nil, text: nil, image_url: nil, image_path: nil, debug: false, options: {}) ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ai_lite.rb', line 96 def moderate(input = nil, model: nil, text: nil, image_url: nil, image_path: nil, debug: false, options: {}) payload = .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., raw: nil, debug: debug) end |