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_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
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#embedding_model ⇒ Object
readonly
Returns the value of attribute embedding_model.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#image_model ⇒ Object
readonly
Returns the value of attribute image_model.
-
#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
- .embed(input, **kwargs) ⇒ Object
- .image(prompt, **kwargs) ⇒ 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
- #embed(input, model: nil, dimensions: nil, encoding_format: nil, debug: false, options: {}) ⇒ Object
- #image(prompt, model: nil, size: nil, quality: nil, background: nil, output_format: nil, output_path: nil, debug: false, options: {}) ⇒ Object
-
#initialize(api_key: nil, model: nil, moderation_model: nil, embedding_model: nil, image_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, embedding_model: nil, image_model: nil, timeout: nil, max_output_tokens: nil) ⇒ AiLite
Returns a new instance of AiLite.
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 = || self.class.configuration. @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_key ⇒ Object (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_model ⇒ Object (readonly)
Returns the value of attribute embedding_model.
79 80 81 |
# File 'lib/ai_lite.rb', line 79 def @embedding_model end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
79 80 81 |
# File 'lib/ai_lite.rb', line 79 def headers @headers end |
#image_model ⇒ Object (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_tokens ⇒ Object (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 |
#model ⇒ Object (readonly)
Returns the value of attribute model.
79 80 81 |
# File 'lib/ai_lite.rb', line 79 def model @model end |
#moderation_model ⇒ Object (readonly)
Returns the value of attribute moderation_model.
79 80 81 |
# File 'lib/ai_lite.rb', line 79 def moderation_model @moderation_model end |
#timeout ⇒ Object (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(, **kwargs) client.chat(, **kwargs) end |
.client ⇒ Object
54 55 56 |
# File 'lib/ai_lite.rb', line 54 def client @client ||= new end |
.configuration ⇒ Object
38 39 40 |
# File 'lib/ai_lite.rb', line 38 def configuration @configuration ||= Configuration.new end |
.configure {|configuration| ... } ⇒ Object
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 (input, **kwargs) client.(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(, 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 |
#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 (input, model: nil, dimensions: nil, encoding_format: nil, debug: false, options: {}) payload = .merge( model: model || , input: input ) payload[:dimensions] = dimensions if dimensions payload[:encoding_format] = encoding_format if encoding_format (post(payload, endpoint: ), multiple: input.is_a?(Array), debug: debug) rescue => e prettify_data(status: "unknown", error: e., 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 = .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., 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 = .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 |