ruby_llm-schema 0.1.7 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +11 -2
- data/lib/ruby_llm/schema/dsl.rb +22 -11
- data/lib/ruby_llm/schema/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5965c9d64040358651a74d313d1bd50010378ca33b20ec46a26db8fb006e4e25
|
4
|
+
data.tar.gz: a4d9ac3040a6ce33fe0bbc4294166c00e8cafb5e87dcb336d47e1541b504dc12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6ce13ba9d6164a794004fe1138f92ab8d141348d86b33163ad23c119425f252f57b585853ab73a21cff102de9cc403f8936f7b63d4b52d4a572795d8a340aad
|
7
|
+
data.tar.gz: 4edbb7d38e7fa1414a812d0fa87f6f20b24e19d2ceb1a151e476c1b330d2e6ac84cfc7faf4aa388858d322bbdaeccffcaa5002db4d19e5a68d5e263876b3db0f
|
data/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# RubyLLM::Schema
|
2
2
|
|
3
|
+
[](https://rubygems.org/gems/ruby_llm-schema)
|
4
|
+
[](https://github.com/danielfriis/ruby_llm-schema/blob/main/LICENSE.txt)
|
5
|
+
[](https://github.com/danielfriis/ruby_llm-schema/actions/workflows/ci.yml)
|
6
|
+
|
3
7
|
A Ruby DSL for creating JSON schemas with a clean, Rails-inspired API. Perfect for defining structured data schemas for LLM function calling or structured outputs.
|
4
8
|
|
5
9
|
## Use Cases
|
@@ -267,9 +271,14 @@ class MySchema < RubyLLM::Schema
|
|
267
271
|
string :longitude
|
268
272
|
end
|
269
273
|
|
274
|
+
# Using a reference in an array
|
270
275
|
array :coordinates, of: :location
|
271
|
-
|
272
|
-
object
|
276
|
+
|
277
|
+
# Using a reference in an object via the `reference` option
|
278
|
+
object :home_location, reference: :location
|
279
|
+
|
280
|
+
# Using a reference in an object via block
|
281
|
+
object :user do
|
273
282
|
reference :location
|
274
283
|
end
|
275
284
|
end
|
data/lib/ruby_llm/schema/dsl.rb
CHANGED
@@ -41,8 +41,8 @@ module RubyLLM
|
|
41
41
|
end
|
42
42
|
|
43
43
|
# Complex type methods
|
44
|
-
def object(name = nil, description: nil, required: true, &block)
|
45
|
-
add_property(name, build_property_schema(:object, description: description, &block), required: required)
|
44
|
+
def object(name = nil, reference: nil, description: nil, required: true, &block)
|
45
|
+
add_property(name, build_property_schema(:object, description: description, reference: reference, &block), required: required)
|
46
46
|
end
|
47
47
|
|
48
48
|
def array(name, of: nil, description: nil, required: true, min_items: nil, max_items: nil, &block)
|
@@ -81,7 +81,8 @@ module RubyLLM
|
|
81
81
|
definitions[name] = {
|
82
82
|
type: "object",
|
83
83
|
properties: sub_schema.properties,
|
84
|
-
required: sub_schema.required_properties
|
84
|
+
required: sub_schema.required_properties,
|
85
|
+
additionalProperties: sub_schema.additional_properties
|
85
86
|
}
|
86
87
|
end
|
87
88
|
|
@@ -123,16 +124,26 @@ module RubyLLM
|
|
123
124
|
when :null
|
124
125
|
{type: "null", description: options[:description]}.compact
|
125
126
|
when :object
|
127
|
+
# If the reference option is provided, return the reference
|
128
|
+
return reference(options[:reference]) if options[:reference]
|
129
|
+
|
126
130
|
sub_schema = Class.new(Schema)
|
127
|
-
sub_schema.class_eval(&)
|
128
131
|
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
description: options[:description]
|
135
|
-
|
132
|
+
# Evaluate the block and capture the result
|
133
|
+
result = sub_schema.class_eval(&)
|
134
|
+
|
135
|
+
# If the block returned a reference and no properties were added, use the reference
|
136
|
+
if result.is_a?(Hash) && result["$ref"] && sub_schema.properties.empty?
|
137
|
+
result.merge(options[:description] ? {description: options[:description]} : {})
|
138
|
+
else
|
139
|
+
{
|
140
|
+
type: "object",
|
141
|
+
properties: sub_schema.properties,
|
142
|
+
required: sub_schema.required_properties,
|
143
|
+
additionalProperties: sub_schema.additional_properties,
|
144
|
+
description: options[:description]
|
145
|
+
}.compact
|
146
|
+
end
|
136
147
|
when :any_of
|
137
148
|
schemas = collect_property_schemas_from_block(&)
|
138
149
|
{
|