fix: refresh and persist RubyLLM model registry on initialization
RubyLLM uses a static bundled models.json file as its model registry. New models released by providers (e.g., gpt-5.1, gpt-5.2) are not available until the gem is updated. This causes ModelNotFoundError for self-hosted users who configure newer models. Call RubyLLM.models.refresh! during Llm::Config.initialize! to fetch the latest model list from configured providers and models.dev. Persist the result with save_to_json to tmp/ruby_llm_models.json so subsequent processes load from disk instead of making HTTP calls. The file has a 1-hour TTL to balance freshness with performance. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
dae4f3ee13
commit
f4900f45d5
@@ -2,6 +2,8 @@ require 'ruby_llm'
|
||||
|
||||
module Llm::Config
|
||||
DEFAULT_MODEL = 'gpt-4.1-mini'.freeze
|
||||
REGISTRY_TTL = 1.hour
|
||||
REGISTRY_PATH = Rails.root.join('tmp/ruby_llm_models.json').to_s
|
||||
|
||||
class << self
|
||||
def initialized?
|
||||
@@ -12,6 +14,7 @@ module Llm::Config
|
||||
return if @initialized
|
||||
|
||||
configure_ruby_llm
|
||||
refresh_model_registry
|
||||
@initialized = true
|
||||
end
|
||||
|
||||
@@ -30,6 +33,21 @@ module Llm::Config
|
||||
|
||||
private
|
||||
|
||||
def refresh_model_registry
|
||||
if fresh_registry_file?
|
||||
RubyLLM.models.load_from_json!(REGISTRY_PATH)
|
||||
else
|
||||
RubyLLM.models.refresh!
|
||||
RubyLLM.models.save_to_json(REGISTRY_PATH)
|
||||
end
|
||||
rescue StandardError => e
|
||||
Rails.logger.warn "Failed to refresh RubyLLM model registry: #{e.message}"
|
||||
end
|
||||
|
||||
def fresh_registry_file?
|
||||
File.exist?(REGISTRY_PATH) && File.mtime(REGISTRY_PATH) > REGISTRY_TTL.ago
|
||||
end
|
||||
|
||||
def configure_ruby_llm
|
||||
RubyLLM.configure do |config|
|
||||
config.openai_api_key = system_api_key if system_api_key.present?
|
||||
|
||||
Reference in New Issue
Block a user