Class: Terret::LLM::AdapterBase

Inherits:
Object
  • Object
show all
Defined in:
lib/terret/llm.rb

Overview

Shared behaviour for real adapters: jittered exponential backoff around anything raising RetryableError. Sleeper is injectable for tests.

Instance Method Summary collapse

Constructor Details

#initialize(max_attempts: 4, base_delay: 0.5, sleeper: ->(s) { sleep(s) }) ⇒ AdapterBase

Returns a new instance of AdapterBase.



63
64
65
66
67
# File 'lib/terret/llm.rb', line 63

def initialize(max_attempts: 4, base_delay: 0.5, sleeper: ->(s) { sleep(s) })
  @max_attempts = max_attempts
  @base_delay = base_delay
  @sleeper = sleeper
end

Instance Method Details

#with_retriesObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/terret/llm.rb', line 69

def with_retries
  attempt = 0
  begin
    yield
  rescue RetryableError
    attempt += 1
    raise if attempt >= @max_attempts

    # equal jitter: uniform over (half, full] of base * 2^n
    cap = @base_delay * (2**(attempt - 1))
    @sleeper.call(cap * (0.5 + rand * 0.5))
    retry
  end
end