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/deferred.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, Deferred, Enum, Integer, Leaf, Null, Number, Object, OneOf, String
Constant Summary collapse
- VERSION =
"0.1.0"
Constants included from Parser
Class Method Summary collapse
- .property(name, type, description, options = {}) ⇒ Object
-
.required(names) ⇒ LLM::Schema::Object
Mark existing properties as required.
- .defaults(defaults) ⇒ LLM::Schema::Object
- .schema ⇒ LLM::Schema private
- .object ⇒ LLM::Schema::Object private
-
.to_s ⇒ String
Render the schema as a prompt-friendly string.
-
.inherited(klass)
Configures a monitor for a subclass.
Instance Method Summary collapse
-
#to_s ⇒ String
(also: #inspect)
Render a schema leaf as a prompt-friendly string.
-
#object(properties) ⇒ LLM::Schema::Object
Returns an object.
-
#array(*items) ⇒ LLM::Schema::Array
Returns an array.
-
#any_of(*values) ⇒ LLM::Schema::AnyOf
Returns an anyOf union.
-
#all_of(*values) ⇒ LLM::Schema::AllOf
Returns an allOf union.
-
#one_of(*values) ⇒ LLM::Schema::OneOf
Returns a oneOf union.
-
#string ⇒ LLM::Schema::String
Returns a string.
-
#number ⇒ LLM::Schema::Number
Returns a number.
-
#integer ⇒ LLM::Schema::Integer
Returns an integer.
-
#boolean ⇒ LLM::Schema::Boolean
Returns a boolean.
-
#null ⇒ LLM::Schema::Null
Returns null.
Methods included from Parser
Class Method Details
.property(name, type, description, options = {}) ⇒ Object
141 142 143 144 145 146 147 148 |
# File 'lib/llm/schema.rb', line 141 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.
154 155 156 157 158 159 160 |
# File 'lib/llm/schema.rb', line 154 def self.required(names) lock do object.tap do |schema| [*names].each { Utils.fetch(schema.properties, _1).required } end end end |
.defaults(defaults) ⇒ LLM::Schema::Object
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/llm/schema.rb', line 165 def self.defaults(defaults) lock do object.tap do |schema| defaults.each do |name, value| leaf = Utils.fetch(schema.properties, name) leaf.owner = self leaf.default(value) end 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.
180 181 182 183 184 |
# File 'lib/llm/schema.rb', line 180 def self.schema lock do @schema ||= LLM::Schema.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.
189 190 191 192 193 |
# File 'lib/llm/schema.rb', line 189 def self.object lock do @object ||= schema.object({}) end end |
Instance Method Details
#to_s ⇒ String Also known as: inspect
Render a schema leaf as a prompt-friendly string.
288 289 290 |
# File 'lib/llm/schema.rb', line 288 def to_s self.class.to_s end |
#object(properties) ⇒ LLM::Schema::Object
Returns an object
214 215 216 |
# File 'lib/llm/schema.rb', line 214 def object(properties) Object.new(properties) end |
#array(*items) ⇒ LLM::Schema::Array
Returns an array
222 223 224 |
# File 'lib/llm/schema.rb', line 222 def array(*items) Array.new(*items) end |
#any_of(*values) ⇒ LLM::Schema::AnyOf
Returns an anyOf union
230 231 232 |
# File 'lib/llm/schema.rb', line 230 def any_of(*values) AnyOf.new(values) end |
#all_of(*values) ⇒ LLM::Schema::AllOf
Returns an allOf union
238 239 240 |
# File 'lib/llm/schema.rb', line 238 def all_of(*values) AllOf.new(values) end |
#one_of(*values) ⇒ LLM::Schema::OneOf
Returns a oneOf union
246 247 248 |
# File 'lib/llm/schema.rb', line 246 def one_of(*values) OneOf.new(values) end |
#string ⇒ LLM::Schema::String
Returns a string
253 254 255 |
# File 'lib/llm/schema.rb', line 253 def string String.new end |
#number ⇒ LLM::Schema::Number
Returns a number
260 261 262 |
# File 'lib/llm/schema.rb', line 260 def number Number.new end |
#integer ⇒ LLM::Schema::Integer
Returns an integer
267 268 269 |
# File 'lib/llm/schema.rb', line 267 def integer Integer.new end |
#boolean ⇒ LLM::Schema::Boolean
Returns a boolean
274 275 276 |
# File 'lib/llm/schema.rb', line 274 def boolean Boolean.new end |
#null ⇒ LLM::Schema::Null
Returns null
281 282 283 |
# File 'lib/llm/schema.rb', line 281 def null Null.new end |