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



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/ruby_llm/contract/step/dsl.rb', line 158

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_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) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ruby_llm/contract/step/dsl.rb', line 114

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

    return @max_cost = amount
  end

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

#max_input(tokens = nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ruby_llm/contract/step/dsl.rb', line 98

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ruby_llm/contract/step/dsl.rb', line 82

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



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ruby_llm/contract/step/dsl.rb', line 130

def model(name = nil)
  if name
    return @model = name
  end

  if defined?(@model)
    @model
  elsif superclass.respond_to?(:model)
    superclass.model
  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



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ruby_llm/contract/step/dsl.rb', line 170

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ruby_llm/contract/step/dsl.rb', line 142

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

    return @temperature = value
  end

  if defined?(@temperature)
    @temperature
  elsif superclass.respond_to?(:temperature)
    superclass.temperature
  end
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