Class: Ace::Support::Models::Molecules::ModelSearcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/support/models/molecules/model_searcher.rb

Overview

Searches for models with fuzzy matching

Instance Method Summary collapse

Constructor Details

#initialize(cache_manager: nil) ⇒ ModelSearcher

Initialize searcher

Parameters:

  • cache_manager (CacheManager, nil) (defaults to: nil)

    Cache manager instance



11
12
13
# File 'lib/ace/support/models/molecules/model_searcher.rb', line 11

def initialize(cache_manager: nil)
  @cache_manager = cache_manager || CacheManager.new
end

Instance Method Details

#all(provider: nil) ⇒ Array<Models::ModelInfo>

List all models

Parameters:

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

    Limit to specific provider

Returns:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ace/support/models/molecules/model_searcher.rb', line 77

def all(provider: nil)
  data = load_data
  models = []

  providers_to_search = provider ? [provider] : data.keys

  providers_to_search.each do |provider_id|
    provider_data = data[provider_id]
    next unless provider_data

    (provider_data["models"] || {}).each do |_model_id, model_data|
      models << Models::ModelInfo.from_hash(model_data, provider_id: provider_id)
    end
  end

  models.sort_by(&:full_id)
end

#count(provider: nil) ⇒ Integer

Count models

Parameters:

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

    Limit to specific provider

Returns:

  • (Integer)

    Model count



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ace/support/models/molecules/model_searcher.rb', line 98

def count(provider: nil)
  data = load_data
  total = 0

  providers_to_search = provider ? [provider] : data.keys

  providers_to_search.each do |provider_id|
    provider_data = data[provider_id]
    next unless provider_data

    total += (provider_data["models"] || {}).size
  end

  total
end

#search(query = nil, provider: nil, limit: 20, filters: nil, with_total: false) ⇒ Array<Models::ModelInfo>, Hash

Search for models matching query Memory optimization: Defers ModelInfo instantiation until after pagination. This avoids materializing thousands of objects for large caches with broad queries.

Parameters:

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

    Search query (nil = match all)

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

    Limit to specific provider

  • limit (Integer) (defaults to: 20)

    Max results

  • filters (Hash, nil) (defaults to: nil)

    Additional filters (key-value pairs)

  • with_total (Boolean) (defaults to: false)

    Return hash with models and total count

Returns:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ace/support/models/molecules/model_searcher.rb', line 25

def search(query = nil, provider: nil, limit: 20, filters: nil, with_total: false)
  data = load_data
  results = []

  providers_to_search = provider ? [provider] : data.keys

  # Phase 1: Collect lightweight hashes with scores (no ModelInfo instantiation)
  providers_to_search.each do |provider_id|
    provider_data = data[provider_id]
    next unless provider_data

    (provider_data["models"] || {}).each do |model_id, model_data|
      # If no query provided, match all (score = 1)
      score = query ? match_score(query, model_id, model_data["name"]) : 1
      if score > 0
        results << {
          data: model_data,
          provider_id: provider_id,
          score: score
        }
      end
    end
  end

  # Phase 2: Sort by score (still lightweight hashes)
  sorted = results.sort_by { |r| -r[:score] }

  # Phase 3: Apply filters if provided (requires ModelInfo for capability checks)
  if filters
    # Must instantiate to filter, but filter early before limit
    models = sorted.map { |r| Models::ModelInfo.from_hash(r[:data], provider_id: r[:provider_id]) }
    models = Atoms::ModelFilter.apply(models, filters)
    total = models.size
    limited = models.first(limit)
  else
    # No filters: instantiate only the limited set
    total = sorted.size
    limited = sorted.first(limit).map do |r|
      Models::ModelInfo.from_hash(r[:data], provider_id: r[:provider_id])
    end
  end

  if with_total
    {models: limited, total: total}
  else
    limited
  end
end

#statsHash

Get stats about the data

Returns:

  • (Hash)

    Stats



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ace/support/models/molecules/model_searcher.rb', line 116

def stats
  data = load_data
  provider_count = data.size
  model_count = 0
  models_by_provider = {}

  data.each do |provider_id, provider_data|
    count = (provider_data["models"] || {}).size
    models_by_provider[provider_id] = count
    model_count += count
  end

  {
    providers: provider_count,
    models: model_count,
    models_by_provider: models_by_provider.sort_by { |_, v| -v }.to_h
  }
end