Class: RubyPi::LLM::Fallback

Inherits:
BaseProvider show all
Defined in:
lib/ruby_pi/llm/fallback.rb

Overview

A resilient provider wrapper that tries a primary provider first and automatically falls back to an alternative provider on failure. Both providers must conform to the BaseProvider interface.

Authentication errors are NOT retried with the fallback since they indicate a configuration problem rather than a transient failure.

Examples:

Setting up a fallback chain

primary  = RubyPi::LLM.model(:gemini, "gemini-2.0-flash")
backup   = RubyPi::LLM.model(:openai, "gpt-4o")
provider = RubyPi::LLM::Fallback.new(primary: primary, fallback: backup)

# If Gemini fails, automatically retries with OpenAI
response = provider.complete(messages: messages)

Instance Attribute Summary collapse

Attributes inherited from BaseProvider

#max_retries, #retry_base_delay, #retry_max_delay

Instance Method Summary collapse

Methods inherited from BaseProvider

#complete

Constructor Details

#initialize(primary:, fallback:, **options) ⇒ Fallback

Creates a new Fallback wrapper with a primary and fallback provider.

Parameters:



38
39
40
41
42
# File 'lib/ruby_pi/llm/fallback.rb', line 38

def initialize(primary:, fallback:, **options)
  super(**options)
  @primary = primary
  @fallback = fallback
end

Instance Attribute Details

#fallbackRubyPi::LLM::BaseProvider (readonly)

Returns the fallback provider.

Returns:



31
32
33
# File 'lib/ruby_pi/llm/fallback.rb', line 31

def fallback
  @fallback
end

#primaryRubyPi::LLM::BaseProvider (readonly)

Returns the primary provider.

Returns:



28
29
30
# File 'lib/ruby_pi/llm/fallback.rb', line 28

def primary
  @primary
end

Instance Method Details

#model_nameString

Returns the model name of the primary provider.

Returns:

  • (String)


47
48
49
# File 'lib/ruby_pi/llm/fallback.rb', line 47

def model_name
  @primary.model_name
end

#provider_nameSymbol

Returns :fallback as the provider identifier.

Returns:

  • (Symbol)


54
55
56
# File 'lib/ruby_pi/llm/fallback.rb', line 54

def provider_name
  :fallback
end