Module: RubyLLM::Contract::Step::Dsl

Included in:
Base
Defined in:
lib/ruby_llm/contract/step/dsl.rb

Overview

Extracted from Base to reduce class length. DSL accessor methods for step definition (input_type, output_type, prompt, etc.).

Instance Method Summary collapse

Instance Method Details

#around_call(&block) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/ruby_llm/contract/step/dsl.rb', line 203

def around_call(&block)
  if block
    return @around_call = block
  end

  if defined?(@around_call) && @around_call
    @around_call
  elsif superclass.respond_to?(:around_call)
    superclass.around_call
  end
end

#class_observersObject



86
87
88
89
90
# File 'lib/ruby_llm/contract/step/dsl.rb', line 86

def class_observers
  own = defined?(@class_observers) ? @class_observers : []
  inherited = superclass.respond_to?(:class_observers) ? superclass.class_observers : []
  inherited + own
end

#class_validatesObject



76
77
78
79
80
# File 'lib/ruby_llm/contract/step/dsl.rb', line 76

def class_validates
  own = defined?(@class_validates) ? @class_validates : []
  inherited = superclass.respond_to?(:class_validates) ? superclass.class_validates : []
  inherited + own
end

#contract(&block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ruby_llm/contract/step/dsl.rb', line 60

def contract(&block)
  return @contract_definition = Definition.new(&block) if block

  if defined?(@contract_definition) && @contract_definition
    @contract_definition
  elsif superclass.respond_to?(:contract)
    superclass.contract
  else
    Definition.new
  end
end

#input_type(type = nil) ⇒ Object

rubocop:disable Metrics/ModuleLength



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ruby_llm/contract/step/dsl.rb', line 9

def input_type(type = nil)
  return @input_type = type if type

  if defined?(@input_type)
    @input_type
  elsif superclass.respond_to?(:input_type)
    superclass.input_type
  else
    String
  end
end

#max_cost(amount = nil, on_unknown_pricing: nil) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ruby_llm/contract/step/dsl.rb', line 124

def max_cost(amount = nil, on_unknown_pricing: nil)
  if amount == :default
    @max_cost = nil
    @max_cost_explicitly_unset = true
    @on_unknown_pricing = nil
    return nil
  end

  if amount
    unless amount.is_a?(Numeric) && amount.positive?
      raise ArgumentError, "max_cost must be positive, got #{amount}"
    end

    if on_unknown_pricing && !%i[refuse warn].include?(on_unknown_pricing)
      raise ArgumentError, "on_unknown_pricing must be :refuse or :warn, got #{on_unknown_pricing.inspect}"
    end

    @max_cost_explicitly_unset = false
    @max_cost = amount
    @on_unknown_pricing = on_unknown_pricing || :refuse
    return @max_cost
  end

  return @max_cost if defined?(@max_cost) && !@max_cost_explicitly_unset
  return nil if @max_cost_explicitly_unset

  superclass.max_cost if superclass.respond_to?(:max_cost)
end

#max_input(tokens = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ruby_llm/contract/step/dsl.rb', line 108

def max_input(tokens = nil)
  if tokens
    unless tokens.is_a?(Numeric) && tokens.positive?
      raise ArgumentError, "max_input must be positive, got #{tokens}"
    end

    return @max_input = tokens
  end

  if defined?(@max_input)
    @max_input
  elsif superclass.respond_to?(:max_input)
    superclass.max_input
  end
end

#max_output(tokens = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ruby_llm/contract/step/dsl.rb', line 92

def max_output(tokens = nil)
  if tokens
    unless tokens.is_a?(Numeric) && tokens.positive?
      raise ArgumentError, "max_output must be positive, got #{tokens}"
    end

    return @max_output = tokens
  end

  if defined?(@max_output)
    @max_output
  elsif superclass.respond_to?(:max_output)
    superclass.max_output
  end
end

#model(name = nil) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/ruby_llm/contract/step/dsl.rb', line 163

def model(name = nil)
  if name == :default
    @model = nil
    @model_explicitly_unset = true
    return nil
  end

  if name
    @model_explicitly_unset = false
    return @model = name
  end

  return @model if defined?(@model) && !@model_explicitly_unset
  return nil if @model_explicitly_unset

  superclass.model if superclass.respond_to?(:model)
end

#observe(description, &block) ⇒ Object



82
83
84
# File 'lib/ruby_llm/contract/step/dsl.rb', line 82

def observe(description, &block)
  (@class_observers ||= []) << Invariant.new(description, block)
end

#on_unknown_pricingObject



153
154
155
156
157
158
159
160
161
# File 'lib/ruby_llm/contract/step/dsl.rb', line 153

def on_unknown_pricing
  if defined?(@on_unknown_pricing)
    @on_unknown_pricing
  elsif superclass.respond_to?(:on_unknown_pricing)
    superclass.on_unknown_pricing
  else
    :refuse
  end
end

#output_schema(&block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby_llm/contract/step/dsl.rb', line 35

def output_schema(&block)
  if block
    require "ruby_llm/schema"
    @output_schema = ::RubyLLM::Schema.create(&block)
  elsif defined?(@output_schema)
    @output_schema
  elsif superclass.respond_to?(:output_schema)
    superclass.output_schema
  end
end

#output_type(type = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby_llm/contract/step/dsl.rb', line 21

def output_type(type = nil)
  return @output_type = type if type

  if defined?(@output_type)
    @output_type
  elsif defined?(@output_schema) && @output_schema
    RubyLLM::Contract::Types::Hash
  elsif superclass.respond_to?(:output_type)
    superclass.output_type
  else
    Hash
  end
end

#prompt(text = nil, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby_llm/contract/step/dsl.rb', line 46

def prompt(text = nil, &block)
  if text
    @prompt_block = proc { user text }
  elsif block
    @prompt_block = block
  elsif defined?(@prompt_block) && @prompt_block
    @prompt_block
  elsif superclass.respond_to?(:prompt)
    superclass.prompt
  else
    raise(ArgumentError, "prompt has not been set")
  end
end

#retry_policy(models: nil, attempts: nil, retry_on: nil, &block) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/ruby_llm/contract/step/dsl.rb', line 215

def retry_policy(models: nil, attempts: nil, retry_on: nil, &block)
  if block || models || attempts || retry_on
    return @retry_policy = RetryPolicy.new(models: models, attempts: attempts, retry_on: retry_on, &block)
  end

  if defined?(@retry_policy) && @retry_policy
    @retry_policy
  elsif superclass.respond_to?(:retry_policy)
    superclass.retry_policy
  end
end

#temperature(value = nil) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/ruby_llm/contract/step/dsl.rb', line 181

def temperature(value = nil)
  if value == :default
    @temperature = nil
    @temperature_explicitly_unset = true
    return nil
  end

  if value
    unless value.is_a?(Numeric) && value >= 0 && value <= 2
      raise ArgumentError, "temperature must be 0.0-2.0, got #{value}"
    end

    @temperature_explicitly_unset = false
    return @temperature = value
  end

  return @temperature if defined?(@temperature) && !@temperature_explicitly_unset
  return nil if @temperature_explicitly_unset

  superclass.temperature if superclass.respond_to?(:temperature)
end

#validate(description, &block) ⇒ Object



72
73
74
# File 'lib/ruby_llm/contract/step/dsl.rb', line 72

def validate(description, &block)
  (@class_validates ||= []) << Invariant.new(description, block)
end