Class: Rails::Llm::Structured::Field

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, options = {}) ⇒ Field

Returns a new instance of Field.



60
61
62
63
64
# File 'lib/rails/llm/structured/schema.rb', line 60

def initialize(name, type, options = {})
  @name = name
  @type = type
  @options = options
end

Instance Attribute Details

#name ⇒ Object (readonly)

Returns the value of attribute name.



58
59
60
# File 'lib/rails/llm/structured/schema.rb', line 58

def name
  @name
end

#options ⇒ Object (readonly)

Returns the value of attribute options.



58
59
60
# File 'lib/rails/llm/structured/schema.rb', line 58

def options
  @options
end

#type ⇒ Object (readonly)

Returns the value of attribute type.



58
59
60
# File 'lib/rails/llm/structured/schema.rb', line 58

def type
  @type
end

Instance Method Details

#required? ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/rails/llm/structured/schema.rb', line 66

def required?
  !@options[:optional]
end

#to_json_schema ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rails/llm/structured/schema.rb', line 70

def to_json_schema
  case @type
  when :string
    { type: "string", description: @options[:description] }.compact
  when :integer
    schema = { type: "integer" }
    schema[:minimum] = @options[:min] if @options[:min]
    schema[:maximum] = @options[:max] if @options[:max]
    schema
  when :number
    schema = { type: "number" }
    schema[:minimum] = @options[:min] if @options[:min]
    schema[:maximum] = @options[:max] if @options[:max]
    schema
  when :boolean
    { type: "boolean" }
  when :enum
    { type: "string", enum: @options[:values].map(&:to_s) }
  when :array
    {
      type: "array",
      items: item_schema,
      minItems: @options[:min_items],
      maxItems: @options[:max_items]
    }.compact
  else
    raise Error, "Unknown field type: #{@type}"
  end
end

#validate!(value) ⇒ Object

Raises:



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rails/llm/structured/schema.rb', line 100

def validate!(value)
  return if @options[:optional] && value.nil?
  
  raise Error, "Field #{@name} is required" if value.nil? && !@options[:optional]
  
  case @type
  when :string
    raise Error, "Field #{@name} must be a string" unless value.is_a?(String)
  when :integer
    raise Error, "Field #{@name} must be an integer" unless value.is_a?(Integer)
    raise Error, "Field #{@name} must be >= #{@options[:min]}" if @options[:min] && value < @options[:min]
    raise Error, "Field #{@name} must be <= #{@options[:max]}" if @options[:max] && value > @options[:max]
  when :number
    raise Error, "Field #{@name} must be a number" unless value.is_a?(Numeric)
  when :boolean
    raise Error, "Field #{@name} must be a boolean" unless [true, false].include?(value)
  when :enum
    unless @options[:values].map(&:to_s).include?(value.to_s)
      raise Error, "Field #{@name} must be one of #{@options[:values].join(', ')}"
    end
  when :array
    raise Error, "Field #{@name} must be an array" unless value.is_a?(Array)
  end
end