Class: Ollama::Policies::AutoPull

Inherits:
Base
  • Object
show all
Defined in:
lib/ollama/policies/auto_pull.rb

Overview

AutoPull policy - automatically pulls missing models on 404

Constant Summary collapse

MAX_RETRY_ATTEMPTS =
1
PULLABLE_ENDPOINTS =
%i[chat generate embeddings show_model create_model].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enabled: true, allowed_patterns: ["*"], hooks: {}) ⇒ AutoPull

Returns a new instance of AutoPull.

Parameters:

  • enabled (Boolean) (defaults to: true)

    Whether auto-pull is enabled (default: true)

  • allowed_patterns (Array<String>) (defaults to: ["*"])

    Glob patterns for models allowed to be auto-pulled (default: ["*"])

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

    Optional hooks: :before_pull, :after_pull



14
15
16
17
18
19
# File 'lib/ollama/policies/auto_pull.rb', line 14

def initialize(enabled: true, allowed_patterns: ["*"], hooks: {})
  super()
  @enabled = enabled
  @allowed_patterns = Array(allowed_patterns)
  @hooks = hooks
end

Instance Attribute Details

#allowed_patternsObject (readonly)

Returns the value of attribute allowed_patterns.



9
10
11
# File 'lib/ollama/policies/auto_pull.rb', line 9

def allowed_patterns
  @allowed_patterns
end

#enabledObject (readonly)

Returns the value of attribute enabled.



9
10
11
# File 'lib/ollama/policies/auto_pull.rb', line 9

def enabled
  @enabled
end

#hooksObject (readonly)

Returns the value of attribute hooks.



9
10
11
# File 'lib/ollama/policies/auto_pull.rb', line 9

def hooks
  @hooks
end

Instance Method Details

#around(request, env, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ollama/policies/auto_pull.rb', line 24

def around(request, env, &block)
  return block.call(request, env) unless @enabled && pullable?(request)

  attempt = 0

  loop do
    attempt += 1
    env[:auto_pull_attempt] = attempt

    begin
      return block.call(request, env)
    rescue NotFoundError
      raise unless attempt <= MAX_RETRY_ATTEMPTS

      model = request_model(request)
      raise unless model && allowed?(model)

      @hooks[:before_pull]&.call(model, env)

      pull_model(model, env)

      @hooks[:after_pull]&.call(model, env)
    end
  end
end