fix: refresh and persist RubyLLM model registry on initialization

RubyLLM uses a static bundled models.json 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, causing ModelNotFoundError for self-hosted
users who configure newer models.

Refresh the model registry synchronously during Llm::Config.initialize!
to fetch the latest model list from configured providers and models.dev,
then persist with save_to_json. This runs once per process on the first
LLM request, adding ~1-2s to that request. Falls back gracefully to the
bundled registry on failure.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
aakashb95
2026-02-18 13:35:02 +05:30
co-authored by Claude Opus 4.6
parent d11e058db2
commit 73cecdbc26
+7 -14
View File
@@ -2,7 +2,6 @@ require 'ruby_llm'
module Llm::Config
DEFAULT_MODEL = 'gpt-4.1-mini'.freeze
INIT_MUTEX = Mutex.new
class << self
def initialized?
@@ -12,13 +11,9 @@ module Llm::Config
def initialize!
return if @initialized
INIT_MUTEX.synchronize do
return if @initialized
configure_ruby_llm
refresh_model_registry
@initialized = true
end
configure_ruby_llm
refresh_model_registry
@initialized = true
end
def reset!
@@ -37,12 +32,10 @@ module Llm::Config
private
def refresh_model_registry
Thread.new do
RubyLLM.models.refresh!
RubyLLM.models.save_to_json
rescue StandardError => e
Rails.logger.warn "Failed to refresh RubyLLM model registry: #{e.message}"
end
RubyLLM.models.refresh!
RubyLLM.models.save_to_json
rescue StandardError => e
Rails.logger.warn "Failed to refresh RubyLLM model registry: #{e.message}"
end
def configure_ruby_llm