Module: Ollama::Client::ModelManagement
- Included in:
- Ollama::Client
- Defined in:
- lib/ollama/client/model_management.rb
Overview
Model management endpoints: CRUD, pull/push, list, show, version
Instance Method Summary collapse
-
#copy_model(source:, destination:) ⇒ true
Copy a model.
-
#create_model(model:, from:, system: nil, template: nil, license: nil, parameters: nil, messages: nil, quantize: nil, stream: false) ⇒ Hash
Create a model from an existing model.
-
#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.
-
#pull(model_name) ⇒ true
Pull a model explicitly.
-
#push_model(model:, insecure: false, stream: false) ⇒ Hash
Push a model to the registry.
-
#show_model(model:, verbose: false) ⇒ Hash
Show model details.
-
#version ⇒ String
Get Ollama server version.
Instance Method Details
#copy_model(source:, destination:) ⇒ true
Copy a model
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ollama/client/model_management.rb', line 50 def copy_model(source:, destination:) copy_uri = URI("#{@config.base_url}/api/copy") req = Net::HTTP::Post.new(copy_uri) req["Content-Type"] = "application/json" req.body = { source: source, destination: destination }.to_json res = http_request(copy_uri, req) handle_http_error(res) unless res.is_a?(Net::HTTPSuccess) true end |
#create_model(model:, from:, system: nil, template: nil, license: nil, parameters: nil, messages: nil, quantize: nil, stream: false) ⇒ Hash
Create a model from an existing model
rubocop:disable Metrics/ParameterLists
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ollama/client/model_management.rb', line 74 def create_model(model:, from:, system: nil, template: nil, license: nil, parameters: nil, messages: nil, quantize: nil, stream: false) create_uri = URI("#{@config.base_url}/api/create") req = Net::HTTP::Post.new(create_uri) req["Content-Type"] = "application/json" body = { model: model, from: from, stream: stream } body[:system] = system if system body[:template] = template if template body[:license] = license if license body[:parameters] = parameters if parameters body[:messages] = if body[:quantize] = quantize if quantize req.body = body.to_json res = http_request(create_uri, req, read_timeout: @config.timeout * 5) handle_http_error(res) unless res.is_a?(Net::HTTPSuccess) JSON.parse(res.body) rescue JSON::ParserError => e raise InvalidJSONError, "Failed to parse create response: #{e.}" end |
#delete_model(model:) ⇒ true
Delete a model
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ollama/client/model_management.rb', line 34 def delete_model(model:) delete_uri = URI("#{@config.base_url}/api/delete") req = Net::HTTP::Delete.new(delete_uri) req["Content-Type"] = "application/json" req.body = { model: model }.to_json res = http_request(delete_uri, req) handle_http_error(res, requested_model: model) unless res.is_a?(Net::HTTPSuccess) true end |
#list_model_names ⇒ Array<String>
List model names only (convenience method)
166 167 168 |
# File 'lib/ollama/client/model_management.rb', line 166 def list_model_names list_models.map { |m| m["name"] } end |
#list_models ⇒ Array<Hash> Also known as:
List available models with full details
147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/ollama/client/model_management.rb', line 147 def list_models = URI("#{@config.base_url}/api/tags") req = Net::HTTP::Get.new() res = http_request(, req) raise Error, "Failed to fetch models: HTTP #{res.code}" unless res.is_a?(Net::HTTPSuccess) body = 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
173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/ollama/client/model_management.rb', line 173 def list_running ps_uri = URI("#{@config.base_url}/api/ps") req = Net::HTTP::Get.new(ps_uri) res = http_request(ps_uri, req) raise Error, "Failed to fetch running models: HTTP #{res.code}" unless res.is_a?(Net::HTTPSuccess) body = 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 running models response: #{e.}" end |
#pull(model_name) ⇒ true
Pull a model explicitly
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/ollama/client/model_management.rb', line 123 def pull(model_name) pull_uri = URI("#{@config.base_url}/api/pull") req = Net::HTTP::Post.new(pull_uri) req["Content-Type"] = "application/json" req.body = { model: model_name, stream: false }.to_json @config.apply_auth_to(req) res = Net::HTTP.start( pull_uri.hostname, pull_uri.port, **@config.(pull_uri, read_timeout: @config.timeout * 10) ) { |http| http.request(req) } handle_http_error(res, requested_model: model_name) unless res.is_a?(Net::HTTPSuccess) true rescue Net::ReadTimeout, Net::OpenTimeout raise TimeoutError, "Pull request timed out" rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError => e raise Error, "Connection failed during pull: #{e.}" end |
#push_model(model:, insecure: false, stream: false) ⇒ Hash
Push a model to the registry
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/ollama/client/model_management.rb', line 103 def push_model(model:, insecure: false, stream: false) push_uri = URI("#{@config.base_url}/api/push") req = Net::HTTP::Post.new(push_uri) req["Content-Type"] = "application/json" body = { model: model, stream: stream } body[:insecure] = true if insecure req.body = body.to_json res = http_request(push_uri, req, read_timeout: @config.timeout * 10) handle_http_error(res) unless res.is_a?(Net::HTTPSuccess) JSON.parse(res.body) rescue JSON::ParserError => e raise InvalidJSONError, "Failed to parse push response: #{e.}" end |
#show_model(model:, verbose: false) ⇒ Hash
Show model details
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/ollama/client/model_management.rb', line 12 def show_model(model:, verbose: false) show_uri = URI("#{@config.base_url}/api/show") req = Net::HTTP::Post.new(show_uri) req["Content-Type"] = "application/json" body = { model: model } body[:verbose] = true if verbose req.body = body.to_json res = http_request(show_uri, req) handle_http_error(res, requested_model: model) unless res.is_a?(Net::HTTPSuccess) parsed = JSON.parse(res.body) parsed["capabilities"] = Capabilities.for(parsed) parsed rescue JSON::ParserError => e raise InvalidJSONError, "Failed to parse show response: #{e.}" end |
#version ⇒ String
Get Ollama server version
192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/ollama/client/model_management.rb', line 192 def version version_uri = URI("#{@config.base_url}/api/version") req = Net::HTTP::Get.new(version_uri) res = http_request(version_uri, req) raise Error, "Failed to fetch version: HTTP #{res.code}" unless res.is_a?(Net::HTTPSuccess) body = JSON.parse(res.body) body["version"] rescue JSON::ParserError => e raise InvalidJSONError, "Failed to parse version response: #{e.}" end |