Module: OpenAI::Helpers::StructuredOutput::JsonSchemaConverter
- Defined in:
- lib/openai/helpers/structured_output/json_schema_converter.rb
Overview
To customize the JSON schema conversion for a type, implement the ‘JsonSchemaConverter` interface.
Constant Summary collapse
- POINTER =
Object.new.freeze
- COUNTER =
Object.new.freeze
Class Method Summary collapse
- .cache_def!(state, type:, &blk) ⇒ Object private
- .to_json_schema(type) ⇒ Hash{Symbol=>Object} private
- .to_json_schema_inner(type, state:) ⇒ Hash{Symbol=>Object} private
- .to_nilable(schema) ⇒ Hash{Symbol=>Object} private
Instance Method Summary collapse
-
#to_json_schema_inner(state:) ⇒ Hash{Symbol=>Object}
The exact JSON schema produced is subject to improvement between minor release versions.
Class Method Details
.cache_def!(state, type:, &blk) ⇒ 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.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/openai/helpers/structured_output/json_schema_converter.rb', line 60 def cache_def!(state, type:, &blk) defs, path = state.fetch_values(:defs, :path) if (stored = defs[type]) stored[OpenAI::Helpers::StructuredOutput::JsonSchemaConverter::COUNTER] += 1 stored.fetch(OpenAI::Helpers::StructuredOutput::JsonSchemaConverter::POINTER) else ref_path = String.new ref = {"$ref": ref_path} stored = { OpenAI::Helpers::StructuredOutput::JsonSchemaConverter::POINTER => ref, OpenAI::Helpers::StructuredOutput::JsonSchemaConverter::COUNTER => 1 } defs.store(type, stored) schema = blk.call ref_path.replace("#/definitions/#{path.join('/')}") stored.update(schema) ref end end |
.to_json_schema(type) ⇒ Hash{Symbol=>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.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/openai/helpers/structured_output/json_schema_converter.rb', line 85 def to_json_schema(type) defs = {} state = {defs: defs, path: []} schema = OpenAI::Helpers::StructuredOutput::JsonSchemaConverter.to_json_schema_inner( type, state: state ) reused_defs = {} defs.each_value do |acc| ref = acc.fetch(OpenAI::Helpers::StructuredOutput::JsonSchemaConverter::POINTER) sch = acc.except( OpenAI::Helpers::StructuredOutput::JsonSchemaConverter::POINTER, OpenAI::Helpers::StructuredOutput::JsonSchemaConverter::COUNTER ) if acc.fetch(OpenAI::Helpers::StructuredOutput::JsonSchemaConverter::COUNTER) > 1 reused_defs.store(ref.fetch(:$ref), sch) else ref.replace(sch) end end reused_defs.empty? ? schema : {"$defs": reused_defs}.update(schema) end |
.to_json_schema_inner(type, state:) ⇒ Hash{Symbol=>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.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/openai/helpers/structured_output/json_schema_converter.rb', line 119 def to_json_schema_inner(type, state:) case type in {"$ref": String} return type in OpenAI::Helpers::StructuredOutput::JsonSchemaConverter return type.to_json_schema_inner(state: state) in Class case type in -> { _1 <= NilClass } return {type: "null"} in -> { _1 <= Integer } return {type: "integer"} in -> { _1 <= Float } return {type: "number"} in -> { _1 <= Symbol || _1 <= String } return {type: "string"} else end in _ if OpenAI::Internal::Util.primitive?(type) return {const: type.is_a?(Symbol) ? type.to_s : type} else end models = %w[ NilClass String Symbol Integer Float OpenAI::Boolean OpenAI::ArrayOf OpenAI::EnumOf OpenAI::UnionOf OpenAI::BaseModel ] # rubocop:disable Layout/LineLength = "#{type} does not implement the #{OpenAI::Helpers::StructuredOutput::JsonSchemaConverter} interface. Please use one of the supported types: #{models}" # rubocop:enable Layout/LineLength raise ArgumentError.new() end |
.to_nilable(schema) ⇒ Hash{Symbol=>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.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/openai/helpers/structured_output/json_schema_converter.rb', line 33 def to_nilable(schema) null = "null" case schema in {"$ref": String} {anyOf: [schema, {type: null}]} in {anyOf: schemas} null = {type: null} schemas.any? { _1 == null || _1 == {type: ["null"]} } ? schema : {anyOf: [*schemas, null]} in {type: String => type} type == null ? schema : schema.update(type: [type, null]) in {type: Array => types} types.include?(null) ? schema : schema.update(type: [*types, null]) end end |
Instance Method Details
#to_json_schema_inner(state:) ⇒ Hash{Symbol=>Object}
The exact JSON schema produced is subject to improvement between minor release versions.
22 |
# File 'lib/openai/helpers/structured_output/json_schema_converter.rb', line 22 def to_json_schema_inner(state:) = (raise NotImplementedError) |