Class: Inquirex::LLM::Schema

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

Overview

Immutable definition of expected LLM output structure. Each field maps a name to an Inquirex data type, forming the contract between the LLM prompt and the structured data it must return.

Examples:

schema = Schema.new(
  industry:          :string,
  entity_type:       :enum,
  employee_count:    :integer,
  estimated_revenue: :currency
)
schema.fields          # => { industry: :string, ... }
schema.field_names     # => [:industry, :entity_type, ...]
schema.valid_output?({ industry: "Tech", ... })  # => true

Constant Summary collapse

VALID_TYPES =
%i[
  string text integer decimal currency boolean
  enum multi_enum date email phone
  array hash
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**field_map) ⇒ Schema

Returns a new instance of Schema.

Parameters:

  • field_map (Hash{Symbol => Symbol})

    field_name => type

Raises:



32
33
34
35
36
37
38
39
40
# File 'lib/inquirex/llm/schema.rb', line 32

def initialize(**field_map)
  raise Errors::DefinitionError, "Schema must have at least one field" if field_map.empty?

  validate_types!(field_map)
  @fields = field_map.transform_keys(&:to_sym)
                     .transform_values(&:to_sym)
                     .freeze
  freeze
end

Instance Attribute Details

#fieldsHash{Symbol => Symbol} (readonly)

field_name => type mapping

Returns:

  • (Hash{Symbol => Symbol})

    the current value of fields



21
22
23
# File 'lib/inquirex/llm/schema.rb', line 21

def fields
  @fields
end

Class Method Details

.from_h(hash) ⇒ Schema

Parameters:

  • hash (Hash)

    string or symbol keys, values are type names

Returns:



82
83
84
85
86
87
# File 'lib/inquirex/llm/schema.rb', line 82

def self.from_h(hash)
  field_map = hash.each_with_object({}) do |(k, v), acc|
    acc[k.to_sym] = v.to_sym
  end
  new(**field_map)
end

Instance Method Details

#==(other) ⇒ Object



89
90
91
# File 'lib/inquirex/llm/schema.rb', line 89

def ==(other)
  other.is_a?(Schema) && @fields == other.fields
end

#field_namesArray<Symbol>

Returns ordered list of field names.

Returns:

  • (Array<Symbol>)

    ordered list of field names



43
# File 'lib/inquirex/llm/schema.rb', line 43

def field_names = @fields.keys

#inspectObject



93
94
95
# File 'lib/inquirex/llm/schema.rb', line 93

def inspect
  "#<Inquirex::LLM::Schema #{@fields.map { |k, v| "#{k}:#{v}" }.join(", ")}>"
end

#missing_fields(output) ⇒ Array<Symbol>

Returns the list of fields missing from the given output.

Parameters:

  • output (Hash)

Returns:

  • (Array<Symbol>)


63
64
65
66
67
68
# File 'lib/inquirex/llm/schema.rb', line 63

def missing_fields(output)
  return field_names unless output.is_a?(Hash)

  symbolized = output.transform_keys(&:to_sym)
  @fields.keys.reject { |key| symbolized.key?(key) }
end

#sizeInteger

Returns number of fields.

Returns:

  • (Integer)

    number of fields



46
# File 'lib/inquirex/llm/schema.rb', line 46

def size = @fields.size

#to_hHash

Returns:

  • (Hash)


71
72
73
# File 'lib/inquirex/llm/schema.rb', line 71

def to_h
  @fields.transform_keys(&:to_s).transform_values(&:to_s)
end

#to_jsonString

Returns JSON representation.

Returns:

  • (String)

    JSON representation



76
77
78
# File 'lib/inquirex/llm/schema.rb', line 76

def to_json(*)
  JSON.generate(to_h)
end

#valid_output?(output) ⇒ Boolean

Checks whether a Hash output conforms to the schema (all declared fields present).

Parameters:

  • output (Hash)

    LLM output to validate

Returns:

  • (Boolean)


52
53
54
55
56
57
# File 'lib/inquirex/llm/schema.rb', line 52

def valid_output?(output)
  return false unless output.is_a?(Hash)

  symbolized = output.transform_keys(&:to_sym)
  @fields.keys.all? { |key| symbolized.key?(key) }
end