Module: RubyLLM::Sequel::ModelMethods::ClassMethods

Defined in:
lib/ruby_llm/sequel/model_methods.rb

Instance Method Summary collapse

Instance Method Details

#from_llm(model_info) ⇒ Object



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
73
74
75
# File 'lib/ruby_llm/sequel/model_methods.rb', line 37

def from_llm(model_info)
  modalities_hash =
    if model_info.modalities.respond_to?(:to_h)
      model_info.modalities.to_h
    else
      model_info.modalities || {}
    end
  capabilities_array = model_info.capabilities || []
  pricing_hash = model_info.pricing.respond_to?(:to_h) ? model_info.pricing.to_h : (model_info.pricing || {})
   =
    if model_info..respond_to?(:to_h)
      model_info..to_h
    else
      model_info. || {}
    end

  model_data = {
    model_id: model_info.id, # RubyLLM::Model::Info uses 'id' not 'model_id'
    name: model_info.name,
    provider: model_info.provider,
    family: model_info.family,
    model_created_at: model_info.created_at,
    context_window: model_info.context_window,
    max_output_tokens: model_info.max_output_tokens,
    knowledge_cutoff: model_info.knowledge_cutoff,
    modalities: jsonb_or_string(modalities_hash),
    capabilities: jsonb_or_string(capabilities_array),
    pricing: jsonb_or_string(pricing_hash),
    metadata: jsonb_or_string()
  }

  existing = first(provider: model_info.provider, model_id: model_info.id)
  if existing
    existing.update(model_data)
    existing
  else
    create(model_data)
  end
end

#jsonb_or_string(value) ⇒ Object

Helper to handle JSON data for both PostgreSQL (jsonb) and other databases (text)



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ruby_llm/sequel/model_methods.rb', line 12

def jsonb_or_string(value)
  return nil if value.nil?

  # If the database supports pg_jsonb (PostgreSQL), use it
  if defined?(::Sequel::Postgres) && db.database_type == :postgres
    ::Sequel.pg_jsonb(value)
  else
    # For other databases (like SQLite), store as JSON string
    value.is_a?(String) ? value : JSON.generate(value)
  end
end

#refresh!Object



24
25
26
27
# File 'lib/ruby_llm/sequel/model_methods.rb', line 24

def refresh!
  ::RubyLLM.models.refresh!
  save_to_database
end

#save_to_databaseObject



29
30
31
32
33
34
35
# File 'lib/ruby_llm/sequel/model_methods.rb', line 29

def save_to_database
  ::Sequel::Model.db.transaction do
    ::RubyLLM.models.all.each do |model_info|
      from_llm(model_info)
    end
  end
end