Module: OllamaChat::ModelHandling
- Included in:
- Chat
- Defined in:
- lib/ollama_chat/model_handling.rb,
lib/ollama_chat/model_handling/model_cache.rb
Overview
A module that provides functionality for managing Ollama models, including checking model availability, pulling models from remote servers, and handling model presence verification.
This module encapsulates the logic for interacting with Ollama models, ensuring that required models are available locally before attempting to use them in chat sessions. It handles both local model verification and remote model retrieval when necessary.
Defined Under Namespace
Classes: ModelCache, ModelMetadata
Instance Method Summary collapse
-
#all_models ⇒ Array<SearchUI::Wrapper>
private
Retrieves a sorted list of all available Ollama models, enriched with size information and marked as favorites where applicable.
-
#choose_model(cli_model, current_model) ⇒ String
private
The choose_model method selects a model from the available list based on CLI input or user interaction.
-
#choose_profile_for_model(model_name, allow_new: false, suggest: nil) ⇒ String?
private
Presents an interactive list of stored configuration profiles for the specified model and prompts the user to select one.
-
#copy_model_options_from_session(model_name = nil, profile: nil) ⇒ Object
private
This method retrieves the options stored for the current session and updates the active model options to match, ensuring the model behavior aligns with the session's specific configuration.
-
#copy_model_options_profile(model) ⇒ Object
private
Interactively copies model options from a source model/profile to the current model/profile.
-
#copy_model_options_to_session(model_name = nil, profile: nil) ⇒ Object
private
Resets the session's model options to match the stored defaults for the specified model.
-
#delete_model_options_profile(model) ⇒ Object
private
Interactively deletes a stored model options profile.
-
#edit_model_options(model_name = nil, profile: nil) ⇒ Object
private
The edit_model_options method retrieves the current options for the specified model, presents them to the user for editing, and returns a new Ollama::Options instance based on the edited configuration.
-
#edit_session_model_options ⇒ self
private
Presents the current session's model options to the user for editing.
-
#export_model_options ⇒ Pathname?
private
Exports all stored model options for every model to a JSON file.
-
#fill_up_model_options(model_options) ⇒ Ollama::Options
private
Fills in missing keys in a model options hash using the attributes of
Ollama::Options. -
#get_default_model_options ⇒ Hash
private
Retrieves the default model options from the application configuration.
-
#get_session_model_options ⇒ Hash
private
Retrieves the model options currently associated with the active session.
-
#get_stored_model_options(model_name, profile: nil) ⇒ Hash
Retrieves the stored model options from the database for a given model name.
-
#import_model_options(filename) ⇒ Boolean?
private
Imports model options from a JSON file into the database.
-
#model_options ⇒ Ollama::Options
private
Computes the current model options as an
Ollama::Optionsobject. -
#model_present?(model, ollama:) ⇒ ModelMetadata, NilClass
private
The model_present? method checks if the specified Ollama model is available.
-
#model_with_size(model, size, favourited: false) ⇒ SearchUI::Wrapper
private
The model_with_size method formats a model's size for display by creating a formatted string that includes the model name and its size in a human-readable format with appropriate units.
-
#prepare_model(model) ⇒ OllamaChat::ModelHandling::ModelMetadata
private
Ensures the specified model is available locally and synchronizes the session's capability settings with the model's actual supported features.
-
#pull_model_from_remote(model, ollama:) ⇒ Object
private
The pull_model_from_remote method attempts to retrieve a model from the remote server if it is not found locally.
-
#pull_model_unless_present(model, ollama:) ⇒ ModelMetadata
private
The pull_model_unless_present method ensures that a specified model is available on the Ollama server.
-
#reconfigure_model_options(profile:, keep_options:) ⇒ Object
private
Reconfigures model options for the current model and profile.
-
#store_model_options(model_name, model_options, profile: nil) ⇒ Hash
private
Stores or updates model options in the database for a specific model.
-
#stored_model_options_exist?(model_name, profile: nil) ⇒ OllamaChat::Database::Models::ModelOptions?
private
Checks if model options exist in the database for the given model name.
-
#use_model(model = nil, keep_options: false, profile: nil) ⇒ ModelMetadata
private
The use_model method selects and sets the model to be used for the chat session.
Instance Method Details
#all_models ⇒ Array<SearchUI::Wrapper> (private)
Retrieves a sorted list of all available Ollama models, enriched with size information and marked as favorites where applicable.
This method fetches the list of models from the Ollama server, sorts them alphabetically by name, and wraps each in a SearchUI::Wrapper for consistent display in the user interface.
639 640 641 642 643 644 645 646 647 648 |
# File 'lib/ollama_chat/model_handling.rb', line 639 def all_models favs = all_favourited('model') @model_cache.models(ollama).sort.map { |name| model_with_size( name, @model_cache.model_size(ollama, name), favourited: favs[name] ) } end |
#choose_model(cli_model, current_model) ⇒ String (private)
The choose_model method selects a model from the available list based on CLI input or user interaction. It processes the provided CLI model parameter to determine if a regex selector is used, filters the models accordingly, and prompts the user to choose from the filtered list if needed. The method ensures that a model is selected and displays a connection message with the chosen model and base URL.
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 |
# File 'lib/ollama_chat/model_handling.rb', line 661 def choose_model(cli_model, current_model) if model_present?(cli_model, ollama:) return cli_model end selector = if cli_model =~ /\A\?+(.*)\z/ cli_model = '' Regexp.new($1) end models = all_models selector and models = models.select { _1.value =~ selector } if models.size == 1 models.first.value elsif cli_model == '' choose_entry( models, prompt: "Which digital oracle shall we consult? %s" )&.value || current_model else cli_model || current_model end end |
#choose_profile_for_model(model_name, allow_new: false, suggest: nil) ⇒ String? (private)
Presents an interactive list of stored configuration profiles for the specified model and prompts the user to select one.
If only one profile exists for the model, it is returned immediately without
prompting. If the user cancels the selection or chooses [EXIT], nil is
returned.
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/ollama_chat/model_handling.rb', line 259 def choose_profile_for_model(model_name, allow_new: false, suggest: nil) profiles = models::ModelOptions.where(model_name:).order(:profile).map(&:profile) if allow_new profiles.unshift(suggest) if suggest && !profiles.member?(suggest) profiles = [ '[EXIT]', '[NEW]' ] + profiles else profiles.unshift(suggest) if suggest && !profiles.member?(suggest) profiles.size < 2 and return profiles.first profiles = [ '[EXIT]' ] + profiles end case chosen = choose_entry(profiles, prompt: "Choose profile for #{bold{model_name}}: %s") when '[EXIT]', nil feedback("Cancelled.", type: :cancel) return when '[NEW]' name = switch_history(:profile_name) do ask?(prompt: 'Enter new profile name: ') end or return if models::ModelOptions.where(model_name:, profile: name).present? feedback("Profile #{name.inspect} already exists!", type: :warn) return end name else chosen end end |
#copy_model_options_from_session(model_name = nil, profile: nil) ⇒ Object (private)
This method retrieves the options stored for the current session and updates the active model options to match, ensuring the model behavior aligns with the session's specific configuration.
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/ollama_chat/model_handling.rb', line 161 def (model_name = nil, profile: nil) profile ||= 'default' model_name ||= @model = (model_name, , profile:) feedback( "Model options #{italic{profile}} for #{bold{model_name}} "\ "were copied from session model options.", type: :info ) end |
#copy_model_options_profile(model) ⇒ Object (private)
Interactively copies model options from a source model/profile to the current model/profile. Displays a side-by-side comparison if the destination profile already exists and prompts for override confirmation.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/ollama_chat/model_handling.rb', line 195 def (model) src_model = choose_model('', model) src_profile = choose_profile_for_model(src_model) || 'default' dst_model = model dst_profile = choose_profile_for_model(dst_model, allow_new: true, suggest: src_profile) src_opts = (src_model, profile: src_profile).full? or return dst_opts = (dst_model, profile: dst_profile) if dst_opts.present? feedback(<<~EOT, type: :info) Profile #{italic{dst_profile}} already exists for #{bold{dst_model}}. 📥 Source (#{src_model}/#{src_profile}): #{JSON.pretty_generate(src_opts)} 📤 Destination (#{dst_model}/#{dst_profile}): #{JSON.pretty_generate(dst_opts)} EOT unless confirm?(prompt: "Override existing profile? (y/n) ", yes: /\Ay/i) feedback("Denied.", type: :denied) return end end (dst_model, src_opts, profile: dst_profile) feedback("Copied options from #{italic{src_model}}/#{italic{src_profile}} " \ "to #{bold{dst_model}}/#{italic{dst_profile}}.", type: :success) end |
#copy_model_options_to_session(model_name = nil, profile: nil) ⇒ Object (private)
Resets the session's model options to match the stored defaults for the specified model.
178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/ollama_chat/model_handling.rb', line 178 def (model_name = nil, profile: nil) profile ||= 'default' model_name ||= @model = (model_name, profile:) session.update(model_options: ) feedback( "Model options #{italic{profile}} of #{bold{model_name}} "\ "were copied to session model options.", type: :info ) end |
#delete_model_options_profile(model) ⇒ Object (private)
Interactively deletes a stored model options profile.
If the deleted profile is the default one, it is replaced with an empty
options hash {} to ensure the model always has a valid default profile.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/ollama_chat/model_handling.rb', line 233 def (model) profile = choose_profile_for_model(model) or return if confirm?(prompt: "🔔 Really delete profile #{bold{profile}} for #{bold{model}}? (y/n) ", yes: /\Ay/i) if profile == 'default' (model, {}, profile:) feedback("Default profile #{italic{profile}} for #{bold{model}} has been reset to empty options.", type: :info) else models::ModelOptions.where(model_name: model, profile:).destroy feedback("Profile #{italic{profile}} for #{bold{model}} deleted.", type: :info) end log(:info, "Model options profile deleted", data: { model:, profile: }) else feedback("Denied.", type: :denied) end end |
#edit_model_options(model_name = nil, profile: nil) ⇒ Object (private)
The edit_model_options method retrieves the current options for the specified model, presents them to the user for editing, and returns a new Ollama::Options instance based on the edited configuration.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/ollama_chat/model_handling.rb', line 119 def (model_name = nil, profile: nil) model_name ||= @model profile ||= 'default' = (model_name, profile:) = () = edit_text(JSON.pretty_generate()) = JSON.load() (model_name, , profile:) rescue JSON::ParserError => e log( :error, "Model handling error", data: { method: __method__, error_class: e.class, error_message: e. }, warn: true ) end |
#edit_session_model_options ⇒ self (private)
Presents the current session's model options to the user for editing.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/ollama_chat/model_handling.rb', line 139 def = = () = edit_text(JSON.pretty_generate()) = (JSON.load().full? || {}).compact session.update(model_options:) self rescue JSON::ParserError => e log( :error, "Model handling error", data: { method: __method__, error_class: e.class, error_message: e. }, warn: true ) end |
#export_model_options ⇒ Pathname? (private)
Exports all stored model options for every model to a JSON file.
The resulting JSON structure is:
[
{ "model_name": "…", "profiles": [ { "profile": "…", "options": {…} } ] },
…
]
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/ollama_chat/model_handling.rb', line 298 def model_names = models::ModelOptions.distinct.map(&:model_name).sort unless model_names.any? feedback("No model options stored yet!", type: :warn) return end payload = model_names.map do |name| { model_name: name, profiles: models::ModelOptions.where(model_name: name) .order(:profile).map { |m| { profile: m.profile, options: m..to_h } } } end filename = determine_valid_output_filename('to export model options to') or return filename.write(JSON.pretty_generate(payload)) total = model_names.sum { |n| models::ModelOptions.where(model_name: n).count } log(:info, "Model options exported", data: { models: model_names, dest: filename.to_s }) feedback("#{model_names.size} model(s), #{total} profile(s) " \ "exported to #{filename.to_path.inspect}.", type: :success) filename end |
#fill_up_model_options(model_options) ⇒ Ollama::Options (private)
Fills in missing keys in a model options hash using the attributes of Ollama::Options.
89 90 91 92 93 94 |
# File 'lib/ollama_chat/model_handling.rb', line 89 def () Ollama::Options.attributes.each_with_object() do |name, mo| mo[name] = [name] end end |
#get_default_model_options ⇒ Hash (private)
Retrieves the default model options from the application configuration.
74 75 76 |
# File 'lib/ollama_chat/model_handling.rb', line 74 def config.model..to_h end |
#get_session_model_options ⇒ Hash (private)
Retrieves the model options currently associated with the active session.
67 68 69 |
# File 'lib/ollama_chat/model_handling.rb', line 67 def session..to_h.symbolize_keys_recursive end |
#get_stored_model_options(model_name, profile: nil) ⇒ Hash
Retrieves the stored model options from the database for a given model name.
47 48 49 50 51 |
# File 'lib/ollama_chat/model_handling.rb', line 47 def (model_name, profile: nil) profile ||= 'default' models::ModelOptions.where(model_name:, profile:).first&.. to_h.symbolize_keys_recursive end |
#import_model_options(filename) ⇒ Boolean? (private)
Imports model options from a JSON file into the database.
The JSON file must be an array of entries:
[ { "model_name": "…", "profiles": [ … ] }, … ]
If multiple models are present, the user selects which to import. For each profile, if an existing record has different options, both are displayed side-by-side and the user decides per-profile.
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/ollama_chat/model_handling.rb', line 336 def (filename) filename = Pathname.new(filename) data = JSON.parse(filename.read) unless data.is_a?(Array) && data.all? { |d| d['model_name'] } feedback("Invalid format: expected array of { 'model_name', 'profiles' }!", type: :warn) return end # Let user pick models if more than one if data.size > 1 = (['[ALL]'] + data.map { |d| d['model_name'] } + ['[EXIT]']) chosen = choose_entry(, prompt: 'Which model(s) to import? %s') case chosen when '[EXIT]', nil feedback("Cancelled.", type: :cancel) return when '[ALL]' selected = data else selected = data.select { |d| d['model_name'] == chosen } end else selected = data end imported = 0 selected.each do |entry| model_name = entry['model_name'] profiles = Array(entry['profiles']) feedback "\n📦 #{bold{model_name}} (#{profiles.size} profile(s))" profiles.each do |profile_data| profile = profile_data['profile'] || 'default' = profile_data['options'] || {} existing = (model_name, profile:) if existing current = existing..to_h.symbolize_keys_recursive incoming = .to_h.symbolize_keys_recursive if current == incoming feedback " • #{italic{profile}}: identical, skipping." next end cur = JSON.pretty_generate(current).gsub(/^/m, ' ') inc = JSON.pretty_generate(incoming).gsub(/^/m, ' ') feedback <<~EOT • #{italic{profile}}: differs! 📤 Current: #{cur} 📥 Incoming: #{inc} EOT unless confirm?(prompt: "\u26A0 Overwrite? (y/n) ", yes: /\Ay/i) feedback " Skipped." next end end (model_name, , profile:) imported += 1 feedback " • #{italic{profile}}: ✅" end end log(:info, "Model options imported", data: { models: selected.map { |e| e['model_name'] }, imported:, source: filename.to_s }) feedback("\nImported #{imported} profile(s) " \ "from #{filename.to_path.inspect}.", type: :success) true end |
#model_options ⇒ Ollama::Options (private)
Computes the current model options as an Ollama::Options object.
81 82 83 |
# File 'lib/ollama_chat/model_handling.rb', line 81 def Ollama::Options[session..to_h.symbolize_keys_recursive] end |
#model_present?(model, ollama:) ⇒ ModelMetadata, NilClass (private)
The model_present? method checks if the specified Ollama model is available.
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/ollama_chat/model_handling.rb', line 420 def model_present?(model, ollama:) name = model.full? or return = @model_cache.cached?(ollama, name) and return ollama.show(model:) do |md| = ModelMetadata.new( name: , system: md.system, capabilities: md.capabilities, families: md.details.families, thinking: md.thinking, ) end or return @model_cache.cache(ollama, name, ) rescue Ollama::Errors::NotFoundError, Ollama::Errors::BadRequestError nil end |
#model_with_size(model, size, favourited: false) ⇒ SearchUI::Wrapper (private)
The model_with_size method formats a model's size for display by creating a formatted string that includes the model name and its size in a human-readable format with appropriate units.
481 482 483 484 485 |
# File 'lib/ollama_chat/model_handling.rb', line 481 def model_with_size(model, size, favourited: false) formatted_size = Term::ANSIColor.bold { format_bytes(size) } display = prefix_favourite("#{model} #{formatted_size}", favourited) SearchUI::Wrapper.new(model, display:) end |
#prepare_model(model) ⇒ OllamaChat::ModelHandling::ModelMetadata (private)
Ensures the specified model is available locally and synchronizes the session's capability settings with the model's actual supported features.
This method performs a lazy-load check: it pulls the model if it's missing and then immediately validates and updates 'thinking' and 'tools' support to prevent invalid API requests.
497 498 499 500 501 502 503 504 505 506 |
# File 'lib/ollama_chat/model_handling.rb', line 497 def prepare_model(model) @model_metadata = pull_model_unless_present(model, ollama:) if think? && !@model_metadata.can?('thinking') think_mode.selected = 'disabled' end if tools_support.on? && !@model_metadata.can?('tools') tools_support.set false end end |
#pull_model_from_remote(model, ollama:) ⇒ Object (private)
The pull_model_from_remote method attempts to retrieve a model from the remote server if it is not found locally.
443 444 445 446 |
# File 'lib/ollama_chat/model_handling.rb', line 443 def pull_model_from_remote(model, ollama:) feedback("Model #{bold{model}} not found locally, attempting to pull it from remote now…", type: :info) ollama.pull(model:) end |
#pull_model_unless_present(model, ollama:) ⇒ ModelMetadata (private)
The pull_model_unless_present method ensures that a specified model is available on the Ollama server. It first checks if the model metadata exists locally; if not, it pulls the model from a remote source and verifies its presence again. If the model still cannot be found, it raises an UnknownModelError indicating the missing model name.
460 461 462 463 464 465 466 467 468 469 470 |
# File 'lib/ollama_chat/model_handling.rb', line 460 def pull_model_unless_present(model, ollama:) if = model_present?(model, ollama:) return else pull_model_from_remote(model, ollama:) if = model_present?(model, ollama:) return end raise OllamaChat::UnknownModelError, "unknown model named #{model.inspect}" end end |
#reconfigure_model_options(profile:, keep_options:) ⇒ Object (private)
Reconfigures model options for the current model and profile.
Ensures that the session has the appropriate model options by:
- Storing default options if none exist for the model/profile combination
- Populating blank session options with stored or default values
- Prompting to overwrite session options if they diverge from stored defaults
(only when
keep_optionsis false)
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
# File 'lib/ollama_chat/model_handling.rb', line 517 def (profile:, keep_options:) = = unless (@model, profile:) (@model, , profile:) end = (@model, profile:) if .blank? if .present? session.update(model_options: ) else (@model, ) session.update(model_options: ) end elsif ! && != all_profiles = models::ModelOptions.where(model_name: @model). order(:profile).all matching = all_profiles.any? do |mo| mo..ask_and_send(:symbolize_keys_recursive) == end unless matching use_pager do |output| dump = all_profiles.map do |mo| <<~EOT 📄 #{italic{mo.profile}}: #{JSON.pretty_generate(mo..ask_and_send(:symbolize_keys_recursive))} EOT end.join feedback <<~EOT, output:, type: :warn Session model options differ from current profile (#{italic{profile}}) and don't match any saved profile! 📋 Available profiles for #{bold{@model}}: #{dump} EOT end if confirm?(prompt: "\n❓ Switch to an existing profile? (y/n) ", yes: /\Ay/i) chosen = choose_profile_for_model(@model) if chosen new_opts = (@model, profile: chosen) session.update(model_options: new_opts) feedback("Switched to profile #{italic{chosen}}.", type: :info) end end end end end |
#store_model_options(model_name, model_options, profile: nil) ⇒ Hash (private)
Stores or updates model options in the database for a specific model.
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ollama_chat/model_handling.rb', line 101 def (model_name, , profile: nil) profile ||= 'default' = .to_h.symbolize_keys_recursive.compact mo = nil if mo = (model_name, profile:) mo.update(options:) else mo = models::ModelOptions.create(model_name:, options:, profile:) end mo. end |
#stored_model_options_exist?(model_name, profile: nil) ⇒ OllamaChat::Database::Models::ModelOptions? (private)
Checks if model options exist in the database for the given model name.
59 60 61 62 |
# File 'lib/ollama_chat/model_handling.rb', line 59 def (model_name, profile: nil) profile ||= 'default' models::ModelOptions.where(model_name:, profile:).first end |
#use_model(model = nil, keep_options: false, profile: nil) ⇒ ModelMetadata (private)
The use_model method selects and sets the model to be used for the chat session.
It allows specifying a particular model or defaults to the current model. After selecting, it pulls the model metadata if necessary. If think? is true and the chosen model does not support thinking, the think mode selector is set to 'disabled'. If tools_support.on? is true and the chosen model does not support tools, tool support is disabled. Returns the metadata for the selected model.
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 |
# File 'lib/ollama_chat/model_handling.rb', line 585 def use_model(model = nil, keep_options: false, profile: nil) profile ||= 'default' old_model = @model if model.blank? new_model = choose_model('', old_model) else new_model = choose_model(model, config.model.name) end unless = model_present?(new_model, ollama:) feedback("Model #{bold{new_model}} not found locally.", type: :warn) unless confirm?(prompt: "Pull it from remote? (y/n) ", yes: /\Ay/i) feedback("Cancelled.", type: :cancel) old_model.full? && model_present?(old_model, ollama:) or raise OllamaChat::UnknownModelError, "no usable model after cancel" return @model_metadata end = pull_model_unless_present(new_model, ollama:) end @model, @model_metadata = new_model, session.update(current_model: @model) if old_model != @model (profile:, keep_options:) if @model_metadata&.can?('thinking') unless think_mode_states.include?(session.think_mode) default_think_mode = @model_metadata&.thinking&.default if default_think_mode default_think_mode = normalize_think_state(default_think_mode) else default_think_mode = 'disabled' end think_mode.selected = default_think_mode end else think_mode.selected = 'disabled' end end log(:info, "Model switched", data: { old_model:, new_model: @model, profile: }) @model_metadata end |