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/version.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
VERSION =
'0.3.1'

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.



74
75
76
77
# File 'lib/ruby_llm/schema.rb', line 74

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



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

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
53
# 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(*args) ⇒ Object



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

def strict(*args)
  if args.empty?
    instance_variable_defined?(:@strict) ? @strict : true
  else
    @strict = args.first
  end
end

.valid?Boolean

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/ruby_llm/schema.rb', line 68

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

.validate!Object



63
64
65
66
# File 'lib/ruby_llm/schema.rb', line 63

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

Instance Method Details

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

Returns:

  • (Boolean)


95
96
97
# File 'lib/ruby_llm/schema.rb', line 95

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)


83
84
85
# File 'lib/ruby_llm/schema.rb', line 83

def valid?
  self.class.valid?
end

#validate!Object



79
80
81
# File 'lib/ruby_llm/schema.rb', line 79

def validate!
  self.class.validate!
end