Class: Ask::ModelCatalog

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ask/models.rb

Overview

Catalog of available AI models. Provides model resolution by name/ID, filtering by capability, and refresh from the models.dev API.

Ask::ModelCatalog.find("gpt-4o")
Ask::ModelCatalog.chat_models
Ask::ModelCatalog.refresh!

Constant Summary collapse

MODELS_DEV_URL =

Returns URL for the models.dev API.

Returns:

  • (String)

    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 Method Summary collapse

Constructor Details

#initialize(models = nil) ⇒ ModelCatalog

Returns a new instance of ModelCatalog.

Parameters:

  • models (Array<Ask::ModelInfo>, nil) (defaults to: nil)

    initial model list



303
304
305
# File 'lib/ask/models.rb', line 303

def initialize(models = nil)
  @models = models || []
end

Class Method Details

.instanceAsk::ModelCatalog

Returns the process-wide singleton instance.

Returns:



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

#allArray<Ask::ModelInfo>

Returns all models in the catalog.

Returns:



310
311
312
# File 'lib/ask/models.rb', line 310

def all
  @models
end

#audio_modelsAsk::ModelCatalog

Returns new catalog containing only audio models.

Returns:



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.

Parameters:

  • family (String)

    family name

Returns:



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.

Parameters:

  • provider (String)

    provider slug

Returns:



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_modelsAsk::ModelCatalog

Returns new catalog containing only chat models.

Returns:



334
335
336
# File 'lib/ask/models.rb', line 334

def chat_models
  self.class.new(@models.select(&:chat?))
end

#each {|Ask::ModelInfo| ... } ⇒ Enumerator

Yields:

Returns:

  • (Enumerator)


316
317
318
# File 'lib/ask/models.rb', line 316

def each(&block)
  @models.each(&block)
end

#embedding_modelsAsk::ModelCatalog

Returns new catalog containing only embedding models.

Returns:



339
340
341
# File 'lib/ask/models.rb', line 339

def embedding_models
  self.class.new(@models.select(&:embedding?))
end

#find(model_id, provider = nil) ⇒ Ask::ModelInfo

Find a model by ID, optionally scoped to a provider.

Parameters:

  • model_id (String)

    model identifier

  • provider (String, nil) (defaults to: nil)

    provider slug

Returns:

Raises:



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_modelsAsk::ModelCatalog

Returns new catalog containing only image models.

Returns:



349
350
351
# File 'lib/ask/models.rb', line 349

def image_models
  self.class.new(@models.select(&:image?))
end

#lengthInteger Also known as: size

Returns number of models.

Returns:

  • (Integer)

    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.

Parameters:

  • timeout (Integer) (defaults to: 10)

    HTTP timeout in seconds

Returns:

  • (self)


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.

Parameters:

Returns:

  • (self)


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