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)
  @configs = []
  @retryable_statuses = DEFAULT_RETRY_ON.dup

  if block
    @max_attempts = 1
    instance_eval(&block)
    warn_no_retry! if @max_attempts == 1 && @configs.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

#config_for_attempt(attempt, default_config) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 53

def config_for_attempt(attempt, default_config)
  if @configs.any?
    @configs[attempt] || @configs.last
  else
    default_config
  end
end

#config_listObject



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

def config_list
  @configs
end

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



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

def escalate(*config_list)
  @configs = config_list.flatten.map { |c| normalize_config(c).freeze }.freeze
  @max_attempts = @configs.length if @max_attempts < @configs.length
end

#model_for_attempt(attempt, default_model) ⇒ Object



61
62
63
# File 'lib/ruby_llm/contract/step/retry_policy.rb', line 61

def model_for_attempt(attempt, default_model)
  config_for_attempt(attempt, { model: default_model })[:model]
end

#model_listObject



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

def model_list
  @configs.map { |c| c[:model] }.freeze
end

#retry_on(*statuses) ⇒ Object



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

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

#retryable?(result) ⇒ Boolean

Returns:

  • (Boolean)


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

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