Class: RubyLLM::Contract::Step::RetryPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/contract/step/retry_policy.rb

Constant Summary collapse

DEFAULT_RETRY_ON =
%i[validation_failed parse_error adapter_error].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(models: nil, attempts: nil, retry_on: nil, &block) ⇒ RetryPolicy

Returns a new instance of RetryPolicy.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 11

def initialize(models: nil, attempts: nil, retry_on: nil, &block)
  @models = []
  @retryable_statuses = DEFAULT_RETRY_ON.dup

  if block
    @max_attempts = 1
    instance_eval(&block)
    warn_no_retry! if @max_attempts == 1 && @models.empty?
  else
    apply_keywords(models: models, attempts: attempts, retry_on: retry_on)
  end

  validate_max_attempts!
end

Instance Attribute Details

#max_attemptsObject (readonly)

Returns the value of attribute max_attempts.



7
8
9
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 7

def max_attempts
  @max_attempts
end

#retryable_statusesObject (readonly)

Returns the value of attribute retryable_statuses.



7
8
9
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 7

def retryable_statuses
  @retryable_statuses
end

Instance Method Details

#attempts(count) ⇒ Object



26
27
28
29
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 26

def attempts(count)
  @max_attempts = count
  validate_max_attempts!
end

#escalate(*model_list) ⇒ Object Also known as: models



31
32
33
34
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 31

def escalate(*model_list)
  @models = model_list.flatten
  @max_attempts = @models.length if @max_attempts < @models.length
end

#model_for_attempt(attempt, default_model) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 49

def model_for_attempt(attempt, default_model)
  if @models.any?
    @models[attempt] || @models.last
  else
    default_model
  end
end

#model_listObject



37
38
39
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 37

def model_list
  @models
end

#retry_on(*statuses) ⇒ Object



41
42
43
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 41

def retry_on(*statuses)
  @retryable_statuses = statuses.flatten
end

#retryable?(result) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 45

def retryable?(result)
  retryable_statuses.include?(result.status)
end