Class: RubyLLM::ActiveRecord::Model

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/ruby_llm/active_record/model.rb

Overview

A row of the ruby_llm_models table, one per registry entry. Chats point at it through belongs_to :model. The listed scope returns the models the provider still serves; unlisted returns the ones kept only because a chat references them.

Constant Summary collapse

UNLISTED_WARNING_LIMIT =

:nodoc:

5

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.descriptionObject

:nodoc:



36
37
38
# File 'lib/ruby_llm/active_record/model.rb', line 36

def description # :nodoc:
  "database:#{table_name}"
end

.from_llm(model_info) ⇒ Object

:nodoc:



55
56
57
# File 'lib/ruby_llm/active_record/model.rb', line 55

def from_llm(model_info) # :nodoc:
  new(attributes_from_llm(model_info))
end

.readObject

:nodoc:



23
24
25
26
27
28
29
30
# File 'lib/ruby_llm/active_record/model.rb', line 23

def read # :nodoc:
  return [] unless table_exists?

  all.map(&:to_llm)
rescue StandardError => e
  RubyLLM.logger.debug { "Failed to load models from database: #{e.message}, falling back to JSON" }
  []
end

.refreshObject

:nodoc:



40
41
42
# File 'lib/ruby_llm/active_record/model.rb', line 40

def refresh # :nodoc:
  RubyLLM.models.refresh
end

.save_to_database(registry = RubyLLM.models) ⇒ Object

:nodoc:



44
45
46
47
48
49
50
51
52
53
# File 'lib/ruby_llm/active_record/model.rb', line 44

def save_to_database(registry = RubyLLM.models) # :nodoc:
  transaction do
    kept = registry.all.map do |model_info|
      model = find_or_initialize_by(model_id: model_info.id, provider: model_info.provider)
      model.update!(attributes_from_llm(model_info))
      model.id
    end
    unlist(kept)
  end
end

.write(registry) ⇒ Object

:nodoc:



32
33
34
# File 'lib/ruby_llm/active_record/model.rb', line 32

def write(registry) # :nodoc:
  save_to_database(registry)
end

Instance Method Details

#to_llmObject

Returns this registry record as a RubyLLM::Model.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ruby_llm/active_record/model.rb', line 113

def to_llm
  RubyLLM::Model.new(
    id: model_id,
    name: name,
    provider: provider,
    family: family,
    created_at: model_created_at,
    context_window: context_window,
    max_output_tokens: max_output_tokens,
    knowledge_cutoff: knowledge_cutoff,
    modalities: modalities&.deep_symbolize_keys || {},
    capabilities: capabilities,
    pricing: pricing&.deep_symbolize_keys || {},
    metadata: &.deep_symbolize_keys || {},
    unlisted_at: unlisted_at
  )
end