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
-
#blob_exists?(digest:) ⇒ Boolean
Check if a blob exists on the server.
-
#copy_model(source:, destination:) ⇒ true
Copy a model.
-
#create_blob(digest:, content:) ⇒ true
Create a blob by uploading content.
-
#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.
-
#create_model_with_params(params) ⇒ Hash
Final status response.
-
#delete_model(model:) ⇒ true
Delete a model.
-
#list_model_names ⇒ Array<String>
List model names only (convenience method).
-
#list_models ⇒ Array<Hash>
(also: #tags)
List available models with full details.
-
#list_running ⇒ Array<Hash>
(also: #ps)
List currently running/loaded models.
-
#load_model(model:, keep_alive: "5m") ⇒ true
Explicitly load a model into memory.
-
#pull(model_name, insecure: false, stream: false, hooks: {}) ⇒ Hash, true
Pull a model from the registry.
-
#pull_with_params(params) ⇒ Hash, true
Final status or true if not streaming.
-
#push_model(model:, insecure: false, stream: false, hooks: {}) ⇒ Hash, true
Push a model to the registry.
-
#push_model_with_params(params) ⇒ Hash, true
Final status or true if not streaming.
-
#show_model(model:, verbose: false) ⇒ Hash
Show model details.
-
#unload_model(model:) ⇒ true
Unload a model from memory.
-
#version ⇒ String
Get Ollama server version.
Instance Method Details
#blob_exists?(digest:) ⇒ Boolean
Check if a blob exists on the server
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
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
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
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: , quantize: quantize, stream: stream ) create_model_with_params(params) end |
#create_model_with_params(params) ⇒ Hash
Returns 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.}" end |
#delete_model(model:) ⇒ true
Delete a model
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_names ⇒ Array<String>
List model names only (convenience method)
215 216 217 |
# File 'lib/ollama/client/model_management.rb', line 215 def list_model_names list_models.map { |m| m["name"] } end |
#list_models ⇒ Array<Hash> Also known as:
List available models with full details
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.}" end |
#list_running ⇒ Array<Hash> Also known as: ps
List currently running/loaded models
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.}" end |
#load_model(model:, keep_alive: "5m") ⇒ true
Explicitly load a model into memory
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
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.
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.}" end |
#push_model(model:, insecure: false, stream: false, hooks: {}) ⇒ Hash, true
Push a model to the registry
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.
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.}" end |
#show_model(model:, verbose: false) ⇒ Hash
Show model details
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.}" end |
#unload_model(model:) ⇒ true
Unload a model from memory
194 195 196 |
# File 'lib/ollama/client/model_management.rb', line 194 def unload_model(model:) load_model(model: model, keep_alive: 0) end |
#version ⇒ String
Get Ollama server version
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.}" end |