Module: OllamaChat::ModelHandling

Included in:
Chat
Defined in:
lib/ollama_chat/model_handling.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.

Examples:

Checking if a model is present

chat.model_present?('llama3.1')

Pulling a model from a remote server

chat.pull_model_from_remote('mistral')

Ensuring a model is available locally

chat.pull_model_unless_present('phi3', {})

Instance Method Summary collapse

Instance Method Details

#model_present?(model) ⇒ String, FalseClass

The model_present? method checks if the specified Ollama model is available.

false otherwise

Parameters:

  • model (String)

    the name of the Ollama model

Returns:

  • (String, FalseClass)

    the system prompt if the model is present,



25
26
27
28
29
# File 'lib/ollama_chat/model_handling.rb', line 25

def model_present?(model)
  ollama.show(model:) { return _1.system.to_s }
rescue Ollama::Errors::NotFoundError
  false
end

#pull_model_from_remote(model) ⇒ nil

The pull_model_from_remote method attempts to retrieve a model from the remote server if it is not found locally.

Parameters:

  • model (String)

    the name of the model to be pulled

Returns:

  • (nil)


37
38
39
40
# File 'lib/ollama_chat/model_handling.rb', line 37

def pull_model_from_remote(model)
  STDOUT.puts "Model #{bold{model}} not found locally, attempting to pull it from remote now…"
  ollama.pull(model:)
end

#pull_model_unless_present(model, options) ⇒ String, FalseClass

The pull_model_unless_present method checks if the specified model is present on the system.

If the model is already present, it returns the system prompt if it is present.

Otherwise, it attempts to pull the model from the remote server using the pull_model_from_remote method. If the model is still not found after pulling, it exits the program with a message indicating that the model was not found remotely.

present, false otherwise.

Parameters:

  • model (String)

    The name of the model to check for presence.

  • options (Hash)

    Options for the pull_model_from_remote method.

Returns:

  • (String, FalseClass)

    the system prompt if the model and it are



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ollama_chat/model_handling.rb', line 58

def pull_model_unless_present(model, options)
  if system = model_present?(model)
    return system.full?
  else
    pull_model_from_remote(model)
    if system = model_present?(model)
      return system.full?
    else
      STDOUT.puts "Model #{bold{model}} not found remotely. => Exiting."
      exit 1
    end
  end
rescue Ollama::Errors::Error => e
  warn "Caught #{e.class} while pulling model: #{e} => Exiting."
  exit 1
end