Module: RubyLLM::Models::Registry

Defined in:
lib/ruby_llm/models/registry.rb

Overview

:nodoc: all

Defined Under Namespace

Classes: FileStore, PublishedSource

Constant Summary collapse

PUBLISHED_URL =
'https://rubyllm.com/models.json'

Class Method Summary collapse

Class Method Details

.cache_pathObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/ruby_llm/models/registry.rb', line 142

def cache_path
  home = Dir.home
  host_os = RbConfig::CONFIG['host_os']

  directory = if host_os.match?(/darwin/i)
                File.join(home, 'Library', 'Caches', 'RubyLLM')
              elsif host_os.match?(/mswin|mingw|cygwin/i)
                local_app_data = ENV.fetch('LOCALAPPDATA', nil)
                local_app_data = File.join(home, 'AppData', 'Local') if local_app_data.to_s.empty?
                File.join(local_app_data, 'RubyLLM', 'Cache')
              else
                xdg_cache = ENV.fetch('XDG_CACHE_HOME', nil)
                xdg_cache = File.join(home, '.cache') if xdg_cache.to_s.empty?
                File.join(xdg_cache, 'ruby_llm')
              end

  File.join(directory, 'models.json')
rescue ArgumentError
  # Dir.home raises when the home directory cannot be determined.
  nil
end

.models_from_data(data, source: nil) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/ruby_llm/models/registry.rb', line 129

def models_from_data(data, source: nil)
  unless data.is_a?(Array)
    location = source ? " in #{source}" : ''
    raise ModelRegistryError, "Model registry#{location} must be a JSON array"
  end

  data.map { |model| model.is_a?(RubyLLM::Model) ? model : RubyLLM::Model.new(model) }
end

.pretty_json(models) ⇒ Object



138
139
140
# File 'lib/ruby_llm/models/registry.rb', line 138

def pretty_json(models)
  "#{JSON.pretty_generate(Array(models).map(&:to_h))}\n"
end

.read(file) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/ruby_llm/models/registry.rb', line 118

def read(file)
  data = JSON.parse(File.read(file), symbolize_names: true)
  models_from_data(data, source: file)
rescue Errno::ENOENT
  nil
rescue JSON::ParserError => e
  raise ModelRegistryError, "Invalid model registry JSON in #{file}: #{e.message}"
rescue SystemCallError => e
  raise ModelRegistryError, "Could not read the model registry from #{file}: #{e.message}"
end