Class: RubyLLM::Schema

Inherits:
Object
  • Object
show all
Extended by:
DSL
Includes:
JsonOutput
Defined in:
lib/ruby_llm/schema.rb,
lib/ruby_llm/schema/dsl.rb,
lib/ruby_llm/schema/errors.rb,
lib/ruby_llm/schema/validator.rb,
lib/ruby_llm/schema/json_output.rb,
lib/ruby_llm/schema/dsl/utilities.rb,
lib/ruby_llm/schema/dsl/complex_types.rb,
lib/ruby_llm/schema/dsl/primitive_types.rb,
lib/ruby_llm/schema/dsl/schema_builders.rb

Defined Under Namespace

Modules: DSL, JsonOutput Classes: Error, InvalidArrayTypeError, InvalidObjectTypeError, InvalidSchemaError, InvalidSchemaTypeError, LimitExceededError, ValidationError, Validator

Constant Summary collapse

PRIMITIVE_TYPES =
i[string number integer boolean null].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL::Utilities

#define, #reference

Methods included from DSL::ComplexTypes

#any_of, #array, #object, #one_of, #optional

Methods included from DSL::PrimitiveTypes

#boolean, #integer, #null, #number, #string

Methods included from DSL::SchemaBuilders

#any_of_schema, #array_schema, #boolean_schema, #integer_schema, #null_schema, #number_schema, #object_schema, #one_of_schema, #string_schema

Methods included from JsonOutput

#to_json, #to_json_schema

Constructor Details

#initialize(name = nil, description: nil) ⇒ Schema

Returns a new instance of Schema.



72
73
74
75
# File 'lib/ruby_llm/schema.rb', line 72

def initialize(name = nil, description: nil)
  @name = name || self.class.name || "Schema"
  @description = description
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/ruby_llm/schema.rb', line 85

def method_missing(method_name, ...)
  if respond_to_missing?(method_name)
    self.class.send(method_name, ...)
  else
    super
  end
end

Class Method Details

.additional_properties(value = nil) ⇒ Object



49
50
51
52
# File 'lib/ruby_llm/schema.rb', line 49

def additional_properties(value = nil)
  return @additional_properties ||= false if value.nil?
  @additional_properties = value
end

.create(&block) ⇒ Object



19
20
21
22
23
# File 'lib/ruby_llm/schema.rb', line 19

def create(&block)
  schema_class = Class.new(Schema)
  schema_class.class_eval(&block)
  schema_class
end

.definitionsObject



33
34
35
# File 'lib/ruby_llm/schema.rb', line 33

def definitions
  @definitions ||= {}
end

.description(description = nil) ⇒ Object



44
45
46
47
# File 'lib/ruby_llm/schema.rb', line 44

def description(description = nil)
  @description = description if description
  @description
end

.name(name = nil) ⇒ Object



37
38
39
40
41
42
# File 'lib/ruby_llm/schema.rb', line 37

def name(name = nil)
  @schema_name = name if name
  return @schema_name if defined?(@schema_name)

  super()
end

.propertiesObject



25
26
27
# File 'lib/ruby_llm/schema.rb', line 25

def properties
  @properties ||= {}
end

.required_propertiesObject



29
30
31
# File 'lib/ruby_llm/schema.rb', line 29

def required_properties
  @required_properties ||= []
end

.strict(value = nil) ⇒ Object



54
55
56
57
58
59
# File 'lib/ruby_llm/schema.rb', line 54

def strict(value = nil)
  if value.nil?
    return @strict.nil? ? (@strict = true) : @strict
  end
  @strict = value
end

.valid?Boolean

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/ruby_llm/schema.rb', line 66

def valid?
  validator = Validator.new(self)
  validator.valid?
end

.validate!Object



61
62
63
64
# File 'lib/ruby_llm/schema.rb', line 61

def validate!
  validator = Validator.new(self)
  validator.validate!
end

Instance Method Details

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to_missing?(method_name, include_private = false)
  i[string number integer boolean array object any_of one_of null].include?(method_name) || super
end

#valid?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/ruby_llm/schema.rb', line 81

def valid?
  self.class.valid?
end

#validate!Object



77
78
79
# File 'lib/ruby_llm/schema.rb', line 77

def validate!
  self.class.validate!
end