Class: OpenAI::Helpers::StructuredOutput::EnumOf

Inherits:
Object
  • Object
show all
Includes:
JsonSchemaConverter, Internal::Type::Enum
Defined in:
lib/openai/helpers/structured_output/enum_of.rb

Overview

Examples:

example = OpenAI::EnumOf[:foo, :bar, :zoo]
example = OpenAI::EnumOf[1, 2, 3]

Constant Summary

Constants included from JsonSchemaConverter

JsonSchemaConverter::COUNTER, JsonSchemaConverter::POINTER

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JsonSchemaConverter

cache_def!, to_json_schema, to_json_schema_inner, to_nilable

Methods included from Internal::Type::Enum

#==, #===, #coerce, #dump, #hash, #inspect, #to_sorbet_type

Methods included from Internal::Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, #to_sorbet_type, to_sorbet_type

Methods included from Internal::Type::Converter

#coerce, coerce, #dump, dump, #inspect, inspect, type_info

Constructor Details

#initialize(*values) ⇒ EnumOf

Returns a new instance of EnumOf.

Parameters:

  • values (Array<generic<Value>>)


59
# File 'lib/openai/helpers/structured_output/enum_of.rb', line 59

def initialize(*values) = (@values = values.map { _1.is_a?(String) ? _1.to_sym : _1 })

Instance Attribute Details

#valuesArray<generic<Value>> (readonly)

Returns:

  • (Array<generic<Value>>)


56
57
58
# File 'lib/openai/helpers/structured_output/enum_of.rb', line 56

def values
  @values
end

Class Method Details

.[]Object



53
# File 'lib/openai/helpers/structured_output/enum_of.rb', line 53

def self.[](...) = new(...)

Instance Method Details

#to_json_schema_inner(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.

Parameters:

  • state (Hash{Symbol=>Object})

    @option state [HashObject=>String] :defs

    @option state [Array<String>] :path

Returns:

  • (Hash{Symbol=>Object})


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/openai/helpers/structured_output/enum_of.rb', line 26

def to_json_schema_inner(state:)
  OpenAI::Helpers::StructuredOutput::JsonSchemaConverter.cache_def!(state, type: self) do
    types = values.map do
      case _1
      in NilClass
        "null"
      in true | false
        "boolean"
      in Integer
        "integer"
      in Float
        "number"
      in Symbol
        "string"
      end
    end
      .uniq

    {
      type: types.length == 1 ? types.first : types,
      enum: values.map { _1.is_a?(Symbol) ? _1.to_s : _1 }
    }
  end
end