Skip to content

Commit 45040db

Browse files
committed
Enhance model registry documentation with application and gem development guidelines, including in-memory refresh and database persistence examples.
1 parent 477d71d commit 45040db

File tree

1 file changed

+80
-6
lines changed

1 file changed

+80
-6
lines changed

docs/guides/models.md

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,93 @@ You can see the full list of currently registered models in the [Available Model
5353

5454
### Refreshing the Registry
5555

56-
The `rake models:update` task updates the `models.json` file based on the currently available models from providers for which you have configured API keys.
56+
**For Application Developers:**
57+
58+
The recommended way to refresh models in your application is to call `RubyLLM.models.refresh!` directly:
59+
60+
```ruby
61+
# In your application code (console, background job, etc.)
62+
RubyLLM.models.refresh!
63+
puts "Refreshed in-memory model list."
64+
```
65+
66+
This refreshes the in-memory model registry and is what you want 99% of the time. This method is safe to call from Rails applications, background jobs, or any running Ruby process.
67+
68+
**For Gem Development:**
69+
70+
The `rake models:update` task is designed for gem maintainers and updates the `models.json` file shipped with the gem:
5771

5872
```bash
59-
# Ensure API keys are configured (e.g., via ENV vars)
73+
# Only for gem development - requires API keys and gem directory structure
6074
bundle exec rake models:update
6175
```
6276

63-
Additionally, you can refresh the *in-memory* model list within a running application using `RubyLLM.models.refresh!`. This is useful for long-running processes that might need to pick up newly available models without restarting. Note that this does *not* update the `models.json` file itself, only the currently loaded list.
77+
This task is not intended for Rails applications as it writes to gem directories and requires the full gem development environment.
78+
79+
**Persisting Models to Your Database:**
80+
81+
If you want to store model information in your application's database for persistence, querying, or caching, create your own migration and sync logic. Here's an example schema and production-ready sync job:
6482

6583
```ruby
66-
# In your application code (e.g., a background job scheduler)
67-
RubyLLM.models.refresh!
68-
puts "Refreshed in-memory model list."
84+
# db/migrate/xxx_create_llm_models.rb
85+
create_table "llm_models", force: :cascade do |t|
86+
t.string "model_id", null: false
87+
t.string "name", null: false
88+
t.string "provider", null: false
89+
t.boolean "available", default: false
90+
t.boolean "is_default", default: false
91+
t.datetime "last_synced_at"
92+
t.integer "context_window"
93+
t.integer "max_output_tokens"
94+
t.jsonb "metadata", default: {}
95+
t.datetime "created_at", null: false
96+
t.datetime "updated_at", null: false
97+
t.string "slug"
98+
t.string "model_type"
99+
t.string "family"
100+
t.datetime "model_created_at"
101+
t.date "knowledge_cutoff"
102+
t.jsonb "modalities", default: {}, null: false
103+
t.jsonb "capabilities", default: [], null: false
104+
t.jsonb "pricing", default: {}, null: false
105+
106+
t.index ["model_id"], unique: true
107+
t.index ["provider", "available", "context_window"]
108+
t.index ["capabilities"], using: :gin
109+
t.index ["modalities"], using: :gin
110+
t.index ["pricing"], using: :gin
111+
end
112+
113+
# app/jobs/sync_llm_models_job.rb
114+
class SyncLLMModelsJob < ApplicationJob
115+
queue_as :default
116+
retry_on StandardError, wait: 1.seconds, attempts: 5
117+
118+
def perform
119+
RubyLLM.models.refresh!
120+
121+
found_model_ids = RubyLLM.models.chat_models.filter_map do |model_data|
122+
attributes = model_data.to_h
123+
attributes[:model_id] = attributes.delete(:id)
124+
attributes[:model_type] = attributes.delete(:type)
125+
attributes[:model_created_at] = attributes.delete(:created_at)
126+
attributes[:last_synced_at] = Time.now
127+
128+
model = LLMModel.find_or_initialize_by(model_id: attributes[:model_id])
129+
model.assign_attributes(**attributes)
130+
model.save ? model.id : nil
131+
end
132+
133+
# Mark missing models as unavailable instead of deleting them
134+
LLMModel.where.not(id: found_model_ids).update_all(available: false)
135+
end
136+
end
137+
138+
# Schedule it to run periodically
139+
# config/schedule.rb (with whenever gem)
140+
every 6.hours do
141+
runner "SyncLLMModelsJob.perform_later"
142+
end
69143
```
70144

71145
## Exploring and Finding Models

0 commit comments

Comments
 (0)