Class: LLM::Schema

Inherits:
Object
  • Object
show all
Extended by:
Parser
Defined in:
lib/llm/schema.rb,
lib/llm/schema/enum.rb,
lib/llm/schema/leaf.rb,
lib/llm/schema/null.rb,
lib/llm/schema/array.rb,
lib/llm/schema/all_of.rb,
lib/llm/schema/any_of.rb,
lib/llm/schema/number.rb,
lib/llm/schema/object.rb,
lib/llm/schema/one_of.rb,
lib/llm/schema/parser.rb,
lib/llm/schema/string.rb,
lib/llm/schema/boolean.rb,
lib/llm/schema/integer.rb,
lib/llm/schema/version.rb

Overview

The LLM::Schema class represents a JSON schema, and provides methods that let you describe and produce a schema that can be used in various contexts that include the validation and generation of JSON data.

Examples:

JavaScript-style

schema = LLM::Schema.new
schema.object({
  name: schema.string.enum("John", "Jane").required,
  age: schema.integer.required,
  hobbies: schema.array(schema.string).required,
  address: schema.object({street: schema.string}).required,
})

Ruby-style

class Address < LLM::Schema
  property :street, String, "Street address"
  required %i[street]
end

class Person < LLM::Schema
  property :name, String, "Person's name"
  property :age, Integer, "Person's age"
  property :hobbies, Array[String], "Person's hobbies"
  property :address, Address, "Person's address"
  required %i[name age hobbies address]
end

See Also:

Defined Under Namespace

Modules: Parser, Utils Classes: AllOf, AnyOf, Array, Boolean, Enum, Integer, Leaf, Null, Number, Object, OneOf, String

Constant Summary collapse

VERSION =
"0.1.0"

Constants included from Parser

Parser::METADATA_KEYS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Parser

parse

Class Method Details

.inherited(klass) ⇒ void

This method returns an undefined value.

Configures a monitor for a subclass



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

def self.inherited(klass)
  LLM.lock(:inherited) do
    klass.instance_eval { @__monitor = Monitor.new }
  end
end

.objectLLM::Schema::Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



136
137
138
139
140
# File 'lib/llm/schema.rb', line 136

def self.object
  lock do
    @object ||= schema.object({})
  end
end

.property(name, type, description, options = {}) ⇒ Object

Parameters:

  • name (String)

    The property name

  • type (Class)

    The property type

  • description (String)

    The property description

  • options (Hash) (defaults to: {})

    A hash of options



103
104
105
106
107
108
109
110
# File 'lib/llm/schema.rb', line 103

def self.property(name, type, description, options = {})
  lock do
    prop = Utils.resolve(schema, type)
    options = {description:}.merge(options)
    options.each { (_2 == true) ? prop.public_send(_1) : prop.public_send(_1, *_2) }
    object[name] = prop
  end
end

.required(names) ⇒ LLM::Schema::Object

Mark existing properties as required.

Parameters:

Returns:



116
117
118
119
120
121
122
# File 'lib/llm/schema.rb', line 116

def self.required(names)
  lock do
    object.tap do |schema|
      [*names].each { Utils.fetch(schema.properties, _1).required }
    end
  end
end

.schemaLLM::Schema

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



127
128
129
130
131
# File 'lib/llm/schema.rb', line 127

def self.schema
  lock do
    @schema ||= LLM::Schema.new
  end
end

Instance Method Details

#all_of(*values) ⇒ LLM::Schema::AllOf

Returns an allOf union

Parameters:

Returns:



177
178
179
# File 'lib/llm/schema.rb', line 177

def all_of(*values)
  AllOf.new(values)
end

#any_of(*values) ⇒ LLM::Schema::AnyOf

Returns an anyOf union

Parameters:

Returns:



169
170
171
# File 'lib/llm/schema.rb', line 169

def any_of(*values)
  AnyOf.new(values)
end

#array(*items) ⇒ LLM::Schema::Array

Returns an array

Parameters:

  • items (Array)

    An array of items

Returns:



161
162
163
# File 'lib/llm/schema.rb', line 161

def array(*items)
  Array.new(*items)
end

#booleanLLM::Schema::Boolean

Returns a boolean



213
214
215
# File 'lib/llm/schema.rb', line 213

def boolean
  Boolean.new
end

#integerLLM::Schema::Integer

Returns an integer



206
207
208
# File 'lib/llm/schema.rb', line 206

def integer
  Integer.new
end

#nullLLM::Schema::Null

Returns null

Returns:



220
221
222
# File 'lib/llm/schema.rb', line 220

def null
  Null.new
end

#numberLLM::Schema::Number

Returns a number

Returns:



199
200
201
# File 'lib/llm/schema.rb', line 199

def number
  Number.new
end

#object(properties) ⇒ LLM::Schema::Object

Returns an object

Parameters:

  • properties (Hash)

    A hash of properties

Returns:



153
154
155
# File 'lib/llm/schema.rb', line 153

def object(properties)
  Object.new(properties)
end

#one_of(*values) ⇒ LLM::Schema::OneOf

Returns a oneOf union

Parameters:

Returns:



185
186
187
# File 'lib/llm/schema.rb', line 185

def one_of(*values)
  OneOf.new(values)
end

#stringLLM::Schema::String

Returns a string

Returns:



192
193
194
# File 'lib/llm/schema.rb', line 192

def string
  String.new
end