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.
302 303 304 |
# File 'lib/ask/models.rb', line 302 def initialize(models = nil) @models = models || [] end |
Class Method Details
.instance ⇒ Ask::ModelCatalog
Returns the process-wide singleton instance.
291 292 293 |
# File 'lib/ask/models.rb', line 291 def instance @instance ||= new end |
.reset_instance! ⇒ Object
Reset the singleton instance (useful for testing).
296 297 298 |
# File 'lib/ask/models.rb', line 296 def reset_instance! @instance = nil end |
Instance Method Details
#all ⇒ Array<Ask::ModelInfo>
Returns all models in the catalog.
309 310 311 |
# File 'lib/ask/models.rb', line 309 def all @models end |
#audio_models ⇒ Ask::ModelCatalog
Returns new catalog containing only audio models.
343 344 345 |
# File 'lib/ask/models.rb', line 343 def audio_models self.class.new(@models.select(&:audio?)) end |
#by_family(family) ⇒ Ask::ModelCatalog
Returns new catalog filtered by family.
354 355 356 |
# File 'lib/ask/models.rb', line 354 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.
360 361 362 |
# File 'lib/ask/models.rb', line 360 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.
333 334 335 |
# File 'lib/ask/models.rb', line 333 def chat_models self.class.new(@models.select(&:chat?)) end |
#each {|Ask::ModelInfo| ... } ⇒ Enumerator
315 316 317 |
# File 'lib/ask/models.rb', line 315 def each(&block) @models.each(&block) end |
#embedding_models ⇒ Ask::ModelCatalog
Returns new catalog containing only embedding models.
338 339 340 |
# File 'lib/ask/models.rb', line 338 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.
324 325 326 327 328 329 330 |
# File 'lib/ask/models.rb', line 324 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.
348 349 350 |
# File 'lib/ask/models.rb', line 348 def image_models self.class.new(@models.select(&:image?)) end |
#length ⇒ Integer Also known as: size
Returns number of models.
365 |
# File 'lib/ask/models.rb', line 365 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.
374 375 376 377 |
# File 'lib/ask/models.rb', line 374 def refresh!(timeout: 10) @models = fetch_from_models_dev(timeout: timeout) self end |
#register(model) ⇒ self
Register a single model, skipping duplicates.
384 385 386 387 |
# File 'lib/ask/models.rb', line 384 def register(model) @models << model unless @models.any? { |m| m.id == model.id && m.provider == model.provider } self end |