Class: Ollama::Policies::Fallback

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

Overview

Fallback policy - tries alternative models on failure

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(models: [], enabled: true, fallback_on: [NotFoundError, TimeoutError, HTTPError, UnsupportedCapabilityError], hooks: {}) ⇒ Fallback

Returns a new instance of Fallback.

Parameters:

  • models (Array<String>) (defaults to: [])

    Ordered list of models to try

  • enabled (Boolean) (defaults to: true)

    Whether fallback is enabled (default: true)

  • fallback_on (Array<Class>) (defaults to: [NotFoundError, TimeoutError, HTTPError, UnsupportedCapabilityError])

    Exception classes to trigger fallback

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

    Optional hooks: :fallback_triggered, :fallback_succeeded



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ollama/policies/fallback.rb', line 15

def initialize(
  models: [],
  enabled: true,
  fallback_on: [NotFoundError, TimeoutError, HTTPError, UnsupportedCapabilityError],
  hooks: {}
)
  super()
  @models = Array(models)
  @enabled = enabled
  @fallback_on = fallback_on
  @hooks = hooks
end

Instance Attribute Details

#enabledObject (readonly)

Returns the value of attribute enabled.



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

def enabled
  @enabled
end

#fallback_onObject (readonly)

Returns the value of attribute fallback_on.



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

def fallback_on
  @fallback_on
end

#hooksObject (readonly)

Returns the value of attribute hooks.



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

def hooks
  @hooks
end

#modelsObject (readonly)

Returns the value of attribute models.



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

def models
  @models
end

Instance Method Details

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ollama/policies/fallback.rb', line 28

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

  current_request = request
  last_error = nil

  @models.each_with_index do |model, index|
    current_request = request_with_model(current_request, model)
    env[:fallback_model] = model
    env[:fallback_index] = index

    begin
      response = block.call(current_request, env)
      @hooks[:fallback_succeeded]&.call(model, index, env) if index.positive?
      return response
    rescue StandardError => e
      last_error = e
      next unless @fallback_on.any? { |cls| e.is_a?(cls) }
      next unless index < @models.size - 1

      @hooks[:fallback_triggered]&.call(e, model, index, env)
    end
  end

  # All fallbacks exhausted
  raise last_error
end