Module: Ollama::Client::ModelManagement

Defined in:
lib/ollama/client/model_management.rb

Overview

Model management endpoints: CRUD, pull/push, list, show, version

Instance Method Summary collapse

Instance Method Details

#blob_exists?(digest:) ⇒ Boolean

Check if a blob exists on the server

Parameters:

  • digest (String)

    The digest of the blob (required)

Returns:

  • (Boolean)

    True if exists, false otherwise



90
91
92
93
94
95
# File 'lib/ollama/client/model_management.rb', line 90

def blob_exists?(digest:)
  res = execute_management_request("/api/blobs/#{digest}", method: :head)
  res.is_a?(Net::HTTPSuccess)
rescue NotFoundError, Error
  false
end

#copy_model(source:, destination:) ⇒ true

Copy a model

Parameters:

  • source (String)

    Existing model name to copy from (required)

  • destination (String)

    New model name to create (required)

Returns:

  • (true)


41
42
43
44
# File 'lib/ollama/client/model_management.rb', line 41

def copy_model(source:, destination:)
  execute_management_request("/api/copy", body: { source: source, destination: destination })
  true
end

#create_blob(digest:, content:) ⇒ true

Create a blob by uploading content

Parameters:

  • digest (String)

    The digest of the blob (required)

  • content (String)

    The raw content of the blob

Returns:

  • (true)


102
103
104
105
106
107
108
109
110
# File 'lib/ollama/client/model_management.rb', line 102

def create_blob(digest:, content:) # rubocop:disable Naming/PredicateMethod
  blob_uri = URI("#{@config.base_url}/api/blobs/#{digest}")
  req = Net::HTTP::Post.new(blob_uri)
  req.body = content

  res = http_request(blob_uri, req)
  handle_http_error(res) unless res.is_a?(Net::HTTPSuccess)
  true
end

#create_model(model:, from: nil, modelfile: nil, path: nil, system: nil, template: nil, license: nil, parameters: nil, messages: nil, quantize: nil, stream: false) ⇒ Hash

Create a model from an existing model, a Modelfile, or a path

rubocop:disable Metrics/ParameterLists

Parameters:

  • model (String)

    Name for the model to create (required)

  • from (String, nil) (defaults to: nil)

    Existing model to create from

  • modelfile (String, nil) (defaults to: nil)

    Raw content of a Modelfile

  • path (String, nil) (defaults to: nil)

    Path to a Modelfile on the server

  • system (String, nil) (defaults to: nil)

    System prompt to embed

  • template (String, nil) (defaults to: nil)

    Prompt template

  • license (String, Array<String>, nil) (defaults to: nil)

    License string(s)

  • parameters (Hash, nil) (defaults to: nil)

    Key-value parameters

  • messages (Array<Hash>, nil) (defaults to: nil)

    Message history

  • quantize (String, nil) (defaults to: nil)

    Quantization level (e.g. "q4_K_M", "q8_0")

  • stream (Boolean) (defaults to: false)

    Stream status updates

Returns:

  • (Hash)

    Final status response



61
62
63
64
65
66
67
68
69
70
# File 'lib/ollama/client/model_management.rb', line 61

def create_model(model:, from: nil, modelfile: nil, path: nil, system: nil, template: nil, license: nil,
                 parameters: nil, messages: nil, quantize: nil, stream: false)
  # rubocop:enable Metrics/ParameterLists
  params = Params::CreateModel.new(
    model: model, from: from, modelfile: modelfile, path: path, system: system,
    template: template, license: license, parameters: parameters, messages: messages,
    quantize: quantize, stream: stream
  )
  create_model_with_params(params)
end

#create_model_with_params(params) ⇒ Hash

Returns Final status response.

Parameters:

Returns:

  • (Hash)

    Final status response



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ollama/client/model_management.rb', line 74

def create_model_with_params(params)
  if params.from.nil? && params.modelfile.nil? && params.path.nil?
    raise ArgumentError,
          "One of from:, modelfile:, or path: is required"
  end

  res = execute_management_request("/api/create", body: params.to_h, read_timeout: @config.timeout * 5)
  JSON.parse(res.body)
rescue JSON::ParserError => e
  raise InvalidJSONError, "Failed to parse create response: #{e.message}"
end

#delete_model(model:) ⇒ true

Delete a model

Parameters:

  • model (String)

    Model name to delete (required)

Returns:

  • (true)


31
32
33
34
# File 'lib/ollama/client/model_management.rb', line 31

def delete_model(model:)
  execute_management_request("/api/delete", method: :delete, body: { model: model }, model: model)
  true
end

#list_model_namesArray<String>

List model names only (convenience method)

Returns:

  • (Array<String>)

    Array of model name strings



215
216
217
# File 'lib/ollama/client/model_management.rb', line 215

def list_model_names
  list_models.map { |m| m["name"] }
end

#list_modelsArray<Hash> Also known as: tags

List available models with full details

Returns:

  • (Array<Hash>)

    Array of model hashes with name, model, size, details, etc.



201
202
203
204
205
206
207
208
209
# File 'lib/ollama/client/model_management.rb', line 201

def list_models
  res = execute_management_request(@provider.models_endpoint, method: :get)
  body = @provider.normalize_models_response(JSON.parse(res.body))
  models = body["models"] || []
  models.each { |m| m["capabilities"] = Capabilities.for(m) }
  models
rescue JSON::ParserError => e
  raise InvalidJSONError, "Failed to parse models response: #{e.message}"
end

#list_runningArray<Hash> Also known as: ps

List currently running/loaded models

Returns:

  • (Array<Hash>)

    Array of running model hashes with name, size, vram, context_length, etc.



222
223
224
225
226
227
228
229
# File 'lib/ollama/client/model_management.rb', line 222

def list_running
  res = execute_management_request("/api/ps", method: :get)
  models = Parsers::ListRunning.new.call(management_response(res))
  models.each { |m| m["capabilities"] = Capabilities.for(m) }
  models
rescue JSON::ParserError => e
  raise InvalidJSONError, "Failed to parse running models response: #{e.message}"
end

#load_model(model:, keep_alive: "5m") ⇒ true

Explicitly load a model into memory

Parameters:

  • model (String)

    Model name (required)

  • keep_alive (String, Integer) (defaults to: "5m")

    Keep-alive duration (default "5m")

Returns:

  • (true)


184
185
186
187
188
# File 'lib/ollama/client/model_management.rb', line 184

def load_model(model:, keep_alive: "5m") # rubocop:disable Naming/PredicateMethod
  execute_management_request("/api/generate", body: { model: model, prompt: "", keep_alive: keep_alive, stream: false },
                                              model: model)
  true
end

#pull(model_name, insecure: false, stream: false, hooks: {}) ⇒ Hash, true

Pull a model from the registry

Parameters:

  • model_name (String)

    Model name to download

  • insecure (Boolean) (defaults to: false)

    Allow insecure connections

  • stream (Boolean) (defaults to: false)

    Stream progress updates

  • hooks (Hash) (defaults to: {})

    Callbacks for streaming progress (:on_progress)

Returns:

  • (Hash, true)

    Final status or true if not streaming



152
153
154
155
# File 'lib/ollama/client/model_management.rb', line 152

def pull(model_name, insecure: false, stream: false, hooks: {})
  params = Params::ModelTransfer.new(model: model_name, insecure: insecure, stream: stream, hooks: hooks)
  pull_with_params(params)
end

#pull_with_params(params) ⇒ Hash, true

Returns Final status or true if not streaming.

Parameters:

Returns:

  • (Hash, true)

    Final status or true if not streaming



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/ollama/client/model_management.rb', line 159

def pull_with_params(params)
  body = { model: params.model, stream: params.stream }
  body[:insecure] = true if params.insecure

  pull_uri = URI("#{@config.base_url}/api/pull")
  req = Net::HTTP::Post.new(pull_uri)
  req["Content-Type"] = "application/json"
  req.body = body.to_json
  @config.apply_auth_to(req)

  if params.stream
    handle_ndjson_stream(pull_uri, req, params.hooks)
  else
    res = execute_management_request("/api/pull", body: body, model: params.model, read_timeout: @config.timeout * 10)
    JSON.parse(res.body)
  end
rescue JSON::ParserError => e
  raise InvalidJSONError, "Failed to parse pull response: #{e.message}"
end

#push_model(model:, insecure: false, stream: false, hooks: {}) ⇒ Hash, true

Push a model to the registry

Parameters:

  • model (String)

    Model name to push (required)

  • insecure (Boolean) (defaults to: false)

    Allow insecure connections

  • stream (Boolean) (defaults to: false)

    Stream progress updates

  • hooks (Hash) (defaults to: {})

    Callbacks for streaming progress (:on_progress)

Returns:

  • (Hash, true)

    Final status or true if not streaming



119
120
121
122
# File 'lib/ollama/client/model_management.rb', line 119

def push_model(model:, insecure: false, stream: false, hooks: {})
  params = Params::ModelTransfer.new(model: model, insecure: insecure, stream: stream, hooks: hooks)
  push_model_with_params(params)
end

#push_model_with_params(params) ⇒ Hash, true

Returns Final status or true if not streaming.

Parameters:

Returns:

  • (Hash, true)

    Final status or true if not streaming



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ollama/client/model_management.rb', line 126

def push_model_with_params(params)
  body = { model: params.model, stream: params.stream }
  body[:insecure] = true if params.insecure

  push_uri = URI("#{@config.base_url}/api/push")
  req = Net::HTTP::Post.new(push_uri)
  req["Content-Type"] = "application/json"
  req.body = body.to_json

  if params.stream
    handle_ndjson_stream(push_uri, req, params.hooks)
  else
    res = execute_management_request("/api/push", body: body, read_timeout: @config.timeout * 10)
    JSON.parse(res.body)
  end
rescue JSON::ParserError => e
  raise InvalidJSONError, "Failed to parse push response: #{e.message}"
end

#show_model(model:, verbose: false) ⇒ Hash

Show model details

Parameters:

  • model (String)

    Model name (required)

  • verbose (Boolean) (defaults to: false)

    Include large verbose fields

Returns:

  • (Hash)

    Model information (parameters, license, capabilities, details, model_info, template)



16
17
18
19
20
21
22
23
24
25
# File 'lib/ollama/client/model_management.rb', line 16

def show_model(model:, verbose: false)
  body = { model: model }
  body[:verbose] = true if verbose
  res = execute_management_request("/api/show", body: body, model: model)
  parsed = Parsers::ShowModel.new.call(management_response(res))
  parsed["capabilities"] = Capabilities.for(parsed)
  parsed
rescue JSON::ParserError => e
  raise InvalidJSONError, "Failed to parse show response: #{e.message}"
end

#unload_model(model:) ⇒ true

Unload a model from memory

Parameters:

  • model (String)

    Model name (required)

Returns:

  • (true)


194
195
196
# File 'lib/ollama/client/model_management.rb', line 194

def unload_model(model:)
  load_model(model: model, keep_alive: 0)
end

#versionString

Get Ollama server version

Returns:

  • (String)

    Version string (e.g. "0.12.6")



235
236
237
238
239
240
241
242
# File 'lib/ollama/client/model_management.rb', line 235

def version
  return nil if @provider.is_a?(Providers::OpenAI)

  res = execute_management_request("/api/version", method: :get)
  Parsers::Version.new.call(management_response(res))
rescue JSON::ParserError => e
  raise InvalidJSONError, "Failed to parse version response: #{e.message}"
end