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
- PROVIDER_PREFERENCE =
Ordered provider preference for disambiguation.
%w[ openai anthropic gemini vertexai bedrock openrouter deepseek mistral perplexity xai azure ollama gpustack github ].freeze
- DELEGATES =
Methods delegated to the singleton instance.
%i[all each find 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 model by ID, optionally scoped to a provider.
-
#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.
Constructor Details
#initialize(models = nil) ⇒ ModelCatalog
Returns a new instance of ModelCatalog.
303 304 305 |
# File 'lib/ask/models.rb', line 303 def initialize(models = nil) @models = models || [] end |
Class Method Details
.instance ⇒ Ask::ModelCatalog
Returns the process-wide singleton instance.
292 293 294 |
# File 'lib/ask/models.rb', line 292 def instance @instance ||= new end |
.reset_instance! ⇒ Object
Reset the singleton instance (useful for testing).
297 298 299 |
# File 'lib/ask/models.rb', line 297 def reset_instance! @instance = nil end |
Instance Method Details
#all ⇒ Array<Ask::ModelInfo>
Returns all models in the catalog.
310 311 312 |
# File 'lib/ask/models.rb', line 310 def all @models end |
#audio_models ⇒ Ask::ModelCatalog
Returns new catalog containing only audio models.
344 345 346 |
# File 'lib/ask/models.rb', line 344 def audio_models self.class.new(@models.select(&:audio?)) end |
#by_family(family) ⇒ Ask::ModelCatalog
Returns new catalog filtered by family.
355 356 357 |
# File 'lib/ask/models.rb', line 355 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.
361 362 363 |
# File 'lib/ask/models.rb', line 361 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.
334 335 336 |
# File 'lib/ask/models.rb', line 334 def chat_models self.class.new(@models.select(&:chat?)) end |
#each {|Ask::ModelInfo| ... } ⇒ Enumerator
316 317 318 |
# File 'lib/ask/models.rb', line 316 def each(&block) @models.each(&block) end |
#embedding_models ⇒ Ask::ModelCatalog
Returns new catalog containing only embedding models.
339 340 341 |
# File 'lib/ask/models.rb', line 339 def self.class.new(@models.select(&:embedding?)) end |
#find(model_id, provider = nil) ⇒ Ask::ModelInfo
Find a model by ID, optionally scoped to a provider.
325 326 327 328 329 330 331 |
# File 'lib/ask/models.rb', line 325 def find(model_id, provider = nil) if provider find_with_provider(model_id, provider.to_s) else find_without_provider(model_id) end end |
#image_models ⇒ Ask::ModelCatalog
Returns new catalog containing only image models.
349 350 351 |
# File 'lib/ask/models.rb', line 349 def image_models self.class.new(@models.select(&:image?)) end |
#length ⇒ Integer Also known as: size
Returns number of models.
366 |
# File 'lib/ask/models.rb', line 366 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.
375 376 377 378 |
# File 'lib/ask/models.rb', line 375 def refresh!(timeout: 10) @models = fetch_from_models_dev(timeout: timeout) self end |
#register(model) ⇒ self
Register a single model, skipping duplicates.
385 386 387 388 |
# File 'lib/ask/models.rb', line 385 def register(model) @models << model unless @models.any? { |m| m.id == model.id && m.provider == model.provider } self end |