Module: OllamaChat::ModelHandling

Included in:
Chat
Defined in:
lib/ollama_chat/model_handling.rb

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,



9
10
11
12
13
# File 'lib/ollama_chat/model_handling.rb', line 9

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)


21
22
23
24
# File 'lib/ollama_chat/model_handling.rb', line 21

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ollama_chat/model_handling.rb', line 42

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