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

Instance Method Details

#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)


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

Parameters:

  • model (String)

    Name for the model to create (required)

  • from (String)

    Existing model to create from (required)

  • 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



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] = messages if messages
  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.message}"
end

#delete_model(model:) ⇒ true

Delete a model

Parameters:

  • model (String)

    Model name to delete (required)

Returns:

  • (true)


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_namesArray<String>

List model names only (convenience method)

Returns:

  • (Array<String>)

    Array of model name strings



166
167
168
# File 'lib/ollama/client/model_management.rb', line 166

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.



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
  tags_uri = URI("#{@config.base_url}/api/tags")
  req = Net::HTTP::Get.new(tags_uri)

  res = http_request(tags_uri, 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.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.



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.message}"
end

#pull(model_name) ⇒ true

Pull a model explicitly

Parameters:

  • model_name (String)

    Model name to download

Returns:

  • (true)


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.http_connection_options(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.message}"
end

#push_model(model:, insecure: false, stream: false) ⇒ Hash

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

Returns:

  • (Hash)

    Final status response



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.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)



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.message}"
end

#versionString

Get Ollama server version

Returns:

  • (String)

    Version string (e.g. “0.12.6”)



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.message}"
end