Class: Inquirex::LLM::Schema
- Inherits:
-
Object
- Object
- Inquirex::LLM::Schema
- 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.
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
-
#fields ⇒ Hash{Symbol => Symbol}
readonly
field_name => type mapping.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#field_names ⇒ Array<Symbol>
Ordered list of field names.
-
#initialize(**field_map) ⇒ Schema
constructor
A new instance of Schema.
- #inspect ⇒ Object
-
#missing_fields(output) ⇒ Array<Symbol>
Returns the list of fields missing from the given output.
-
#size ⇒ Integer
Number of fields.
- #to_h ⇒ Hash
-
#to_json ⇒ String
JSON representation.
-
#valid_output?(output) ⇒ Boolean
Checks whether a Hash output conforms to the schema (all declared fields present).
Constructor Details
#initialize(**field_map) ⇒ Schema
Returns a new instance of Schema.
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
#fields ⇒ Hash{Symbol => Symbol} (readonly)
field_name => type mapping
21 22 23 |
# File 'lib/inquirex/llm/schema.rb', line 21 def fields @fields end |
Class Method Details
.from_h(hash) ⇒ Schema
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_names ⇒ Array<Symbol>
Returns ordered list of field names.
43 |
# File 'lib/inquirex/llm/schema.rb', line 43 def field_names = @fields.keys |
#inspect ⇒ Object
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.
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 |
#size ⇒ Integer
Returns number of fields.
46 |
# File 'lib/inquirex/llm/schema.rb', line 46 def size = @fields.size |
#to_h ⇒ 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_json ⇒ String
Returns 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).
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 |