Class: LLM::Schema
- Inherits:
-
Object
- Object
- LLM::Schema
- 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,
lib/llm/schema/renderer.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.
Defined Under Namespace
Modules: Parser, Renderer, 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
Class Method Summary collapse
- .defaults(defaults) ⇒ LLM::Schema::Object
-
.inherited(klass) ⇒ void
Configures a monitor for a subclass.
- .object ⇒ LLM::Schema::Object private
- .property(name, type, description, options = {}) ⇒ Object
-
.required(names) ⇒ LLM::Schema::Object
Mark existing properties as required.
- .schema ⇒ LLM::Schema private
-
.to_s ⇒ String
Render the schema as a prompt-friendly string.
Instance Method Summary collapse
-
#all_of(*values) ⇒ LLM::Schema::AllOf
Returns an allOf union.
-
#any_of(*values) ⇒ LLM::Schema::AnyOf
Returns an anyOf union.
-
#array(*items) ⇒ LLM::Schema::Array
Returns an array.
-
#boolean ⇒ LLM::Schema::Boolean
Returns a boolean.
-
#integer ⇒ LLM::Schema::Integer
Returns an integer.
-
#null ⇒ LLM::Schema::Null
Returns null.
-
#number ⇒ LLM::Schema::Number
Returns a number.
-
#object(properties) ⇒ LLM::Schema::Object
Returns an object.
-
#one_of(*values) ⇒ LLM::Schema::OneOf
Returns a oneOf union.
-
#string ⇒ LLM::Schema::String
Returns a string.
-
#to_s ⇒ String
(also: #inspect)
Render a schema leaf as a prompt-friendly string.
Methods included from Parser
Class Method Details
.defaults(defaults) ⇒ LLM::Schema::Object
128 129 130 131 132 133 134 135 136 |
# File 'lib/llm/schema.rb', line 128 def self.defaults(defaults) lock do object.tap do |schema| defaults.each do |name, val| Utils.fetch(schema.properties, name).default(val) end end end end |
.inherited(klass) ⇒ void
This method returns an undefined value.
Configures a monitor for a subclass
89 90 91 92 93 |
# File 'lib/llm/schema.rb', line 89 def self.inherited(klass) LLM.lock(:inherited) do klass.instance_eval { @__monitor = Monitor.new } end end |
.object ⇒ LLM::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.
150 151 152 153 154 |
# File 'lib/llm/schema.rb', line 150 def self.object lock do @object ||= schema.object({}) end end |
.property(name, type, description, options = {}) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/llm/schema.rb', line 104 def self.property(name, type, description, = {}) lock do prop = Utils.resolve(schema, type) = {description:}.merge() .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.
117 118 119 120 121 122 123 |
# File 'lib/llm/schema.rb', line 117 def self.required(names) lock do object.tap do |schema| [*names].each { Utils.fetch(schema.properties, _1).required } end end end |
.schema ⇒ LLM::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.
141 142 143 144 145 |
# File 'lib/llm/schema.rb', line 141 def self.schema lock do @schema ||= LLM::Schema.new end end |
Instance Method Details
#all_of(*values) ⇒ LLM::Schema::AllOf
Returns an allOf union
199 200 201 |
# File 'lib/llm/schema.rb', line 199 def all_of(*values) AllOf.new(values) end |
#any_of(*values) ⇒ LLM::Schema::AnyOf
Returns an anyOf union
191 192 193 |
# File 'lib/llm/schema.rb', line 191 def any_of(*values) AnyOf.new(values) end |
#array(*items) ⇒ LLM::Schema::Array
Returns an array
183 184 185 |
# File 'lib/llm/schema.rb', line 183 def array(*items) Array.new(*items) end |
#boolean ⇒ LLM::Schema::Boolean
Returns a boolean
235 236 237 |
# File 'lib/llm/schema.rb', line 235 def boolean Boolean.new end |
#integer ⇒ LLM::Schema::Integer
Returns an integer
228 229 230 |
# File 'lib/llm/schema.rb', line 228 def integer Integer.new end |
#null ⇒ LLM::Schema::Null
Returns null
242 243 244 |
# File 'lib/llm/schema.rb', line 242 def null Null.new end |
#number ⇒ LLM::Schema::Number
Returns a number
221 222 223 |
# File 'lib/llm/schema.rb', line 221 def number Number.new end |
#object(properties) ⇒ LLM::Schema::Object
Returns an object
175 176 177 |
# File 'lib/llm/schema.rb', line 175 def object(properties) Object.new(properties) end |
#one_of(*values) ⇒ LLM::Schema::OneOf
Returns a oneOf union
207 208 209 |
# File 'lib/llm/schema.rb', line 207 def one_of(*values) OneOf.new(values) end |
#string ⇒ LLM::Schema::String
Returns a string
214 215 216 |
# File 'lib/llm/schema.rb', line 214 def string String.new end |
#to_s ⇒ String Also known as: inspect
Render a schema leaf as a prompt-friendly string.
249 250 251 |
# File 'lib/llm/schema.rb', line 249 def to_s self.class.to_s end |