Class: Inquirex::LLM::DSL::LlmStepBuilder

Inherits:
Object
  • Object
show all
Includes:
DSL::RuleHelpers
Defined in:
lib/inquirex/llm/dsl/llm_step_builder.rb

Overview

Builds an LLM::Node from a step DSL block. Handles the LLM-specific methods (from, prompt, schema, model, temperature, max_tokens, fallback) while inheriting transition and skip_if from the core StepBuilder.

Examples:

Referencing downstream questions (preferred)

extract :business_extracted do
  from :business_description
  prompt "Extract structured business info."
  schema :entity_type, :employee_band     # resolved from the ask steps
  model :claude_sonnet
  temperature 0.2
  transition to: :next_step
end

Explicit field types (for fields with no matching question)

extract :business_extracted do
  from :business_description
  prompt "Extract structured business info."
  schema industry: :string, employee_count: :integer
  transition to: :next_step
end

Instance Method Summary collapse

Constructor Details

#initialize(verb) ⇒ LlmStepBuilder

Returns a new instance of LlmStepBuilder.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/inquirex/llm/dsl/llm_step_builder.rb', line 30

def initialize(verb)
  @verb = verb.to_sym
  @prompt = nil
  @schema_refs = []
  @schema_fields = {}
  @from_steps = []
  @from_all = false
  @model = nil
  @temperature = nil
  @max_tokens = nil
  @fallback = nil
  @transitions = []
  @skip_if = nil
  @question = nil
  @text = nil
end

Instance Method Details

#build(id, nodes: nil) ⇒ LLM::Node

Builds the LLM::Node. Question references in the schema are resolved against the full node map, so the flow builder defers this call until every step — including ones defined after this one — is known.

Parameters:

  • id (Symbol)
  • nodes (Hash{Symbol => Inquirex::Node}, nil) (defaults to: nil)

    all flow nodes, required when the schema references question ids

Returns:

Raises:

  • (Errors::DefinitionError)

    if prompt is missing or a schema reference does not resolve to an ask/confirm question



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/inquirex/llm/dsl/llm_step_builder.rb', line 165

def build(id, nodes: nil)
  validate!(id)

  field_map = resolve_schema_refs(id, nodes).merge(@schema_fields)
  schema_obj = field_map.empty? ? nil : Schema.new(**field_map)
  prompt_text = @prompt == :auto ? auto_prompt(nodes) : @prompt

  LLM::Node.new(
    id:,
    verb:        @verb,
    prompt:      prompt_text,
    schema:      schema_obj,
    from_steps:  @from_steps,
    from_all:    @from_all,
    model:       @model,
    temperature: @temperature,
    max_tokens:  @max_tokens,
    fallback:    @fallback,
    question:    @question,
    text:        @text,
    transitions: @transitions,
    skip_if:     @skip_if
  )
end

#fallback {|Hash| ... } ⇒ Object

Server-side fallback block, invoked when the LLM call fails. Stripped from JSON serialization.

Yields:

  • (Hash)

    answers collected so far

Returns:

  • (Object)

    fallback value to store as the answer



120
121
122
# File 'lib/inquirex/llm/dsl/llm_step_builder.rb', line 120

def fallback(&block)
  @fallback = block
end

#from(*step_ids) ⇒ Object

Adds source step id(s) whose answers feed the LLM prompt.

Parameters:

  • step_ids (Symbol, Array<Symbol>)

    one or more step ids



83
84
85
# File 'lib/inquirex/llm/dsl/llm_step_builder.rb', line 83

def from(*step_ids)
  @from_steps.concat(step_ids.flatten)
end

#from_all(value = true) ⇒ Object

Passes all collected answers to the LLM prompt.

Parameters:

  • value (Boolean) (defaults to: true)


90
91
92
# File 'lib/inquirex/llm/dsl/llm_step_builder.rb', line 90

def from_all(value = true)
  @from_all = !!value
end

#max_tokens(value) ⇒ Object

Optional maximum output tokens.

Parameters:

  • value (Integer)


111
112
113
# File 'lib/inquirex/llm/dsl/llm_step_builder.rb', line 111

def max_tokens(value)
  @max_tokens = value.to_i
end

#model(name) ⇒ Object

Optional model hint for the adapter.

Parameters:

  • name (Symbol)

    e.g. :claude_sonnet, :claude_haiku



97
98
99
# File 'lib/inquirex/llm/dsl/llm_step_builder.rb', line 97

def model(name)
  @model = name.to_sym
end

#prompt(text) ⇒ Object

Sets the LLM prompt template. Use {field_name} for interpolation placeholders that the adapter resolves at runtime.

Pass :auto to generate the prompt from the schema's question references at build time: each referenced question's own wording is enumerated, so the LLM sees what was actually asked. Requires at least one question reference in the schema.

Parameters:

  • text (String, :auto)


56
57
58
# File 'lib/inquirex/llm/dsl/llm_step_builder.rb', line 56

def prompt(text)
  @prompt = text
end

#question(content) ⇒ Object

Optional display text (user-visible label for the step).

Parameters:

  • content (String)


144
145
146
# File 'lib/inquirex/llm/dsl/llm_step_builder.rb', line 144

def question(content)
  @question = content
end

#schema(*question_ids, **fields) ⇒ Object

Declares the expected output schema.

Positional symbols name questions defined elsewhere in the flow; each is resolved against that step's declared type, and for :enum / :multi_enum questions the allowed option values are folded into the schema sent to the LLM. A symbol that matches no ask/confirm step in the flow fails validation as invalid DSL.

Keyword pairs declare explicit field => type mappings, for output fields that have no corresponding question. Both forms compose:

schema :filing_status, :income_types, confidence: :decimal

Parameters:

  • question_ids (Array<Symbol>)

    ids of questions to fill

  • fields (Hash{Symbol => Symbol})

    explicit field => type pairs



75
76
77
78
# File 'lib/inquirex/llm/dsl/llm_step_builder.rb', line 75

def schema(*question_ids, **fields)
  @schema_refs.concat(question_ids.flatten.map(&:to_sym))
  @schema_fields.merge!(fields)
end

#skip_if(rule) ⇒ Object

Sets a rule that skips this step entirely when true.

Parameters:

  • rule (Rules::Base)


137
138
139
# File 'lib/inquirex/llm/dsl/llm_step_builder.rb', line 137

def skip_if(rule)
  @skip_if = rule
end

#temperature(value) ⇒ Object

Optional sampling temperature.

Parameters:

  • value (Float)


104
105
106
# File 'lib/inquirex/llm/dsl/llm_step_builder.rb', line 104

def temperature(value)
  @temperature = value.to_f
end

#text(content) ⇒ Object

Optional display text for context.

Parameters:

  • content (String)


151
152
153
# File 'lib/inquirex/llm/dsl/llm_step_builder.rb', line 151

def text(content)
  @text = content
end

#transition(to:, if_rule: nil, requires_server: true) ⇒ Object

Adds a conditional transition. Inherited concept from core DSL. All LLM transitions are implicitly requires_server: true.

Parameters:

  • to (Symbol)

    target step id

  • if_rule (Rules::Base, nil) (defaults to: nil)
  • requires_server (Boolean) (defaults to: true)


130
131
132
# File 'lib/inquirex/llm/dsl/llm_step_builder.rb', line 130

def transition(to:, if_rule: nil, requires_server: true)
  @transitions << Inquirex::Transition.new(target: to, rule: if_rule, requires_server:)
end