Class: Ask::ModelCatalog
- Inherits:
-
Object
- Object
- Ask::ModelCatalog
- Includes:
- Enumerable
- Defined in:
- lib/ask/models.rb
Overview
Constant Summary collapse
- MODELS_DEV_URL =
Returns URL for the models.dev API.
"https://models.dev/api.json".freeze
- DELEGATES =
Methods delegated to the singleton instance.
%i[all each find where chat_models embedding_models audio_models image_models by_family by_provider refresh!].freeze
Class Method Summary collapse
-
.instance ⇒ Ask::ModelCatalog
The process-wide singleton instance.
-
.reset_instance! ⇒ Object
Reset the singleton instance (useful for testing).
Instance Method Summary collapse
-
#all ⇒ Array<Ask::ModelInfo>
All models in the catalog.
-
#audio_models ⇒ Ask::ModelCatalog
New catalog containing only audio models.
-
#by_family(family) ⇒ Ask::ModelCatalog
New catalog filtered by family.
-
#by_provider(provider) ⇒ Ask::ModelCatalog
New catalog filtered by provider.
-
#chat_models ⇒ Ask::ModelCatalog
New catalog containing only chat models.
- #each {|Ask::ModelInfo| ... } ⇒ Enumerator
-
#embedding_models ⇒ Ask::ModelCatalog
New catalog containing only embedding models.
-
#find(model_id, provider: nil) ⇒ Ask::ModelInfo
Find a single model by ID.
-
#image_models ⇒ Ask::ModelCatalog
New catalog containing only image models.
-
#initialize(models = nil) ⇒ ModelCatalog
constructor
A new instance of ModelCatalog.
-
#length ⇒ Integer
(also: #size)
Number of models.
-
#refresh!(timeout: 10) ⇒ self
Fetch the latest model data from the models.dev API.
-
#register(model) ⇒ self
Register a single model, skipping duplicates.
-
#where(model_id) ⇒ Array<Ask::ModelInfo>
Find all models matching an ID.
Constructor Details
#initialize(models = nil) ⇒ ModelCatalog
Returns a new instance of ModelCatalog.
295 296 297 |
# File 'lib/ask/models.rb', line 295 def initialize(models = nil) @models = models || [] end |
Class Method Details
.instance ⇒ Ask::ModelCatalog
Returns the process-wide singleton instance.
284 285 286 |
# File 'lib/ask/models.rb', line 284 def instance @instance ||= new end |
.reset_instance! ⇒ Object
Reset the singleton instance (useful for testing).
289 290 291 |
# File 'lib/ask/models.rb', line 289 def reset_instance! @instance = nil end |
Instance Method Details
#all ⇒ Array<Ask::ModelInfo>
Returns all models in the catalog.
302 303 304 |
# File 'lib/ask/models.rb', line 302 def all @models end |
#audio_models ⇒ Ask::ModelCatalog
Returns new catalog containing only audio models.
345 346 347 |
# File 'lib/ask/models.rb', line 345 def audio_models self.class.new(@models.select(&:audio?)) end |
#by_family(family) ⇒ Ask::ModelCatalog
Returns new catalog filtered by family.
356 357 358 |
# File 'lib/ask/models.rb', line 356 def by_family(family) self.class.new(@models.select { |m| m.family.to_s == family.to_s }) end |
#by_provider(provider) ⇒ Ask::ModelCatalog
Returns new catalog filtered by provider.
362 363 364 |
# File 'lib/ask/models.rb', line 362 def by_provider(provider) self.class.new(@models.select { |m| m.provider == provider.to_s }) end |
#chat_models ⇒ Ask::ModelCatalog
Returns new catalog containing only chat models.
335 336 337 |
# File 'lib/ask/models.rb', line 335 def chat_models self.class.new(@models.select(&:chat?)) end |
#each {|Ask::ModelInfo| ... } ⇒ Enumerator
308 309 310 |
# File 'lib/ask/models.rb', line 308 def each(&block) @models.each(&block) end |
#embedding_models ⇒ Ask::ModelCatalog
Returns new catalog containing only embedding models.
340 341 342 |
# File 'lib/ask/models.rb', line 340 def self.class.new(@models.select(&:embedding?)) end |
#find(model_id, provider: nil) ⇒ Ask::ModelInfo
Find a single model by ID. Scopes to provider when given.
317 318 319 320 321 322 323 324 325 |
# File 'lib/ask/models.rb', line 317 def find(model_id, provider: nil) if provider @models.find { |m| m.id == model_id && m.provider == provider.to_s } || raise(ModelNotFound, "Model #{model_id.inspect} not found for provider #{provider.inspect}.") else @models.find { |m| m.id == model_id } || raise(ModelNotFound, "Unknown model: #{model_id.inspect}.") end end |
#image_models ⇒ Ask::ModelCatalog
Returns new catalog containing only image models.
350 351 352 |
# File 'lib/ask/models.rb', line 350 def image_models self.class.new(@models.select(&:image?)) end |
#length ⇒ Integer Also known as: size
Returns number of models.
367 |
# File 'lib/ask/models.rb', line 367 def length = @models.length |
#refresh!(timeout: 10) ⇒ self
Fetch the latest model data from the models.dev API. Falls back to current models if the API is unreachable.
376 377 378 379 |
# File 'lib/ask/models.rb', line 376 def refresh!(timeout: 10) @models = fetch_from_models_dev(timeout: timeout) self end |
#register(model) ⇒ self
Register a single model, skipping duplicates.
386 387 388 389 |
# File 'lib/ask/models.rb', line 386 def register(model) @models << model unless @models.any? { |m| m.id == model.id && m.provider == model.provider } self end |
#where(model_id) ⇒ Array<Ask::ModelInfo>
Find all models matching an ID. Never raises.
330 331 332 |
# File 'lib/ask/models.rb', line 330 def where(model_id) @models.select { |m| m.id == model_id } end |