Class: LLM::Schema

Inherits:
Object
  • Object
show all
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/number.rb,
lib/llm/schema/object.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: true
end

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

See Also:

Defined Under Namespace

Classes: Array, Boolean, Enum, Integer, Leaf, Null, Number, Object, String

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/llm/schema.rb', line 62

def self.property(name, type, description, options = {})
  lock do
    if LLM::Schema::Leaf === type
      prop = type
    elsif Class === type && type.respond_to?(:object)
      prop = type.object
    else
      target = type.name.split("::").last.downcase
      prop = schema.public_send(target)
    end
    options = {description:}.merge(options)
    options.each { (_2 == true) ? prop.public_send(_1) : prop.public_send(_1, *_2) }
    object[name] = prop
  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:



81
82
83
84
85
# File 'lib/llm/schema.rb', line 81

def self.schema
  lock do
    @schema ||= LLM::Schema.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:



90
91
92
93
94
# File 'lib/llm/schema.rb', line 90

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

.inherited(klass) ⇒ void

This method returns an undefined value.

Configures a monitor for a subclass



47
48
49
50
51
# File 'lib/llm/schema.rb', line 47

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

Instance Method Details

#nullLLM::Schema::Null

Returns null

Returns:



150
151
152
# File 'lib/llm/schema.rb', line 150

def null
  Null.new
end

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

Returns an object

Parameters:

  • properties (Hash)

    A hash of properties

Returns:



107
108
109
# File 'lib/llm/schema.rb', line 107

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

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

Returns an array

Parameters:

  • items (Array)

    An array of items

Returns:



115
116
117
# File 'lib/llm/schema.rb', line 115

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

#stringLLM::Schema::String

Returns a string

Returns:



122
123
124
# File 'lib/llm/schema.rb', line 122

def string
  String.new
end

#numberLLM::Schema::Number

Returns a number

Returns:



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

def number
  Number.new
end

#integerLLM::Schema::Integer

Returns an integer



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

def integer
  Integer.new
end

#booleanLLM::Schema::Boolean

Returns a boolean



143
144
145
# File 'lib/llm/schema.rb', line 143

def boolean
  Boolean.new
end