Class: Ollama::Policies::Fallback
- Defined in:
- lib/ollama/policies/fallback.rb
Overview
Fallback policy - tries alternative models on failure
Instance Attribute Summary collapse
-
#enabled ⇒ Object
readonly
Returns the value of attribute enabled.
-
#fallback_on ⇒ Object
readonly
Returns the value of attribute fallback_on.
-
#hooks ⇒ Object
readonly
Returns the value of attribute hooks.
-
#models ⇒ Object
readonly
Returns the value of attribute models.
Instance Method Summary collapse
- #around(request, env, &block) ⇒ Object
-
#initialize(models: [], enabled: true, fallback_on: [NotFoundError, TimeoutError, HTTPError, UnsupportedCapabilityError], hooks: {}) ⇒ Fallback
constructor
A new instance of Fallback.
Constructor Details
#initialize(models: [], enabled: true, fallback_on: [NotFoundError, TimeoutError, HTTPError, UnsupportedCapabilityError], hooks: {}) ⇒ Fallback
Returns a new instance of Fallback.
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
#enabled ⇒ Object (readonly)
Returns the value of attribute enabled.
9 10 11 |
# File 'lib/ollama/policies/fallback.rb', line 9 def enabled @enabled end |
#fallback_on ⇒ Object (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 |
#hooks ⇒ Object (readonly)
Returns the value of attribute hooks.
9 10 11 |
# File 'lib/ollama/policies/fallback.rb', line 9 def hooks @hooks end |
#models ⇒ Object (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 |