forked from danielfriis/ruby_llm-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_output.rb
More file actions
34 lines (28 loc) · 931 Bytes
/
json_output.rb
File metadata and controls
34 lines (28 loc) · 931 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# frozen_string_literal: true
module RubyLLM
class Schema
module JsonOutput
def to_json_schema
validate! # Validate schema before generating JSON
schema_hash = {
type: "object",
properties: self.class.properties,
required: self.class.required_properties,
additionalProperties: self.class.additional_properties
}
schema_hash[:strict] = self.class.strict unless self.class.strict.nil?
# Only include $defs if there are definitions
schema_hash["$defs"] = self.class.definitions unless self.class.definitions.empty?
{
name: @name,
description: @description || self.class.description,
schema: schema_hash
}
end
def to_json(*_args)
validate! # Validate schema before generating JSON string
JSON.pretty_generate(to_json_schema)
end
end
end
end