Class: Inquirex::LLM::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/inquirex/llm/adapter.rb

Overview

Abstract interface for LLM adapters. Adapters bridge the gap between LLM::Node definitions and actual LLM API calls.

Implementations must:

1. Accept an LLM::Node and current answers
2. Construct the appropriate prompt (using node.prompt, node.from_steps, etc.)
3. Call the LLM API
4. Parse and validate the response against node.schema (if present)
5. Return a Hash or String result

The adapter is invoked server-side when the engine reaches an LLM step. It is never called on the frontend.

Examples:

Implementing a custom adapter

class MyLlmAdapter < Inquirex::LLM::Adapter
  def call(node, answers)
    prompt_text = build_prompt(node, answers)
    response = my_llm_client.complete(prompt_text, model: node.model)
    parse_response(response, node.schema)
  end
end

Direct Known Subclasses

NullAdapter

Instance Method Summary collapse

Instance Method Details

#call(node, answers) ⇒ Hash, String

Processes an LLM step and returns the result.

Parameters:

  • node (LLM::Node)

    the LLM step to process

  • answers (Hash)

    current collected answers

Returns:

  • (Hash, String)

    structured output (for clarify/detour) or text (for describe/summarize)

Raises:



34
35
36
# File 'lib/inquirex/llm/adapter.rb', line 34

def call(node, answers)
  raise NotImplementedError, "#{self.class}#call must be implemented"
end

#source_answers(node, answers) ⇒ Hash

Gathers the source answer data that feeds the LLM prompt.

Parameters:

Returns:

  • (Hash)

    relevant subset of answers



43
44
45
46
47
48
49
50
51
# File 'lib/inquirex/llm/adapter.rb', line 43

def source_answers(node, answers)
  if node.from_all
    answers.dup
  else
    node.from_steps.each_with_object({}) do |step_id, acc|
      acc[step_id] = answers[step_id] if answers.key?(step_id)
    end
  end
end

#validate_output!(node, output) ⇒ Object

Validates adapter output against the node’s schema.

Parameters:

Raises:



58
59
60
61
62
63
64
65
66
# File 'lib/inquirex/llm/adapter.rb', line 58

def validate_output!(node, output)
  return unless node.schema

  missing = node.schema.missing_fields(output)
  return if missing.empty?

  raise Errors::SchemaViolationError,
    "LLM output for #{node.id.inspect} missing fields: #{missing.join(", ")}"
end