Module: OmniAI::Schema

Defined in:
lib/omniai/schema.rb,
lib/omniai/schema/base.rb,
lib/omniai/schema/array.rb,
lib/omniai/schema/format.rb,
lib/omniai/schema/object.rb,
lib/omniai/schema/scalar.rb

Overview

Examples:

format = OmniAI::Schema.format(name: "Contact", schema: OmniAI::Schema.object(
  description: "A contact with a name, relationship, and addresses.",
  properties: {
    name: OmniAI::Schema.string,
    relationship: OmniAI::Schema.string(enum: %w[friend family]),
    addresses: OmniAI::Schema.array(
      items: OmniAI::Schema.object(
        title: "Address",
        description: "An address with street, city, state, and zip code.",
        properties: {
          street: OmniAI::Schema.string,
          city: OmniAI::Schema.string,
          state: OmniAI::Schema.string,
          zip: OmniAI::Schema.string,
        },
        required: %i[street city state zip]
      )
    ),
  },
  required: %i[name]
))

Defined Under Namespace

Classes: Array, Base, Format, Object, Scalar

Class Method Summary collapse

Class Method Details

.array(items:, min_items: nil, max_items: nil, description: nil, nullable: nil) ⇒ OmniAI::Schema::Array

Examples:

property = OmniAI::Schema.array(
  items: OmniAI::Schema.string(description: 'The name of the person.'),
  description: 'A list of names.'
  min_items: 1,
  max_items: 5,
)

Parameters:

  • items (OmniAI::Schema::Scalar)

    required - the items in the array

  • min_items (Integer) (defaults to: nil)

    optional - the minimum number of items

  • max_items (Integer) (defaults to: nil)

    optional - the maximum number of items

  • description (String) (defaults to: nil)

    optional - a description of the array

  • nullable (Boolean) (defaults to: nil)

    optional - if the array may be null

Returns:



88
89
90
# File 'lib/omniai/schema.rb', line 88

def self.array(items:, min_items: nil, max_items: nil, description: nil, nullable: nil)
  OmniAI::Schema::Array.new(items:, description:, min_items:, max_items:, nullable:)
end

.boolean(title: nil, description: nil, enum: nil, nullable: nil) ⇒ OmniAI::Schema::Scalar

Examples:

OmniAI::Schema.boolean(description: "Is the person employed?") #=> OmniAI::Schema::Scalar

Parameters:

  • title (String) (defaults to: nil)

    optional - the title of the property

  • description (String) (defaults to: nil)

    optional - a description of the property

  • enum (Array<Boolean>) (defaults to: nil)

    optional - the possible values of the property

  • nullable (Boolean) (defaults to: nil)

    optional - if the property may be null

Returns:



123
124
125
# File 'lib/omniai/schema.rb', line 123

def self.boolean(title: nil, description: nil, enum: nil, nullable: nil)
  OmniAI::Schema::Scalar.new(type: OmniAI::Schema::Scalar::Type::BOOLEAN, title:, description:, enum:, nullable:)
end

.build(kind, **args) ⇒ OmniAI::Schema::Object, ...



62
63
64
65
66
67
68
69
70
71
# File 'lib/omniai/schema.rb', line 62

def self.build(kind, **args)
  case kind
  when :array then array(**args)
  when :object then object(**args)
  when :boolean then boolean(**args)
  when :integer then integer(**args)
  when :number then number(**args)
  when :string then string(**args)
  end
end

.deserialize(data) ⇒ Object

Examples:

OmniAI::Schema.deserialize({
  type: 'object',
  title: 'Person',
  properties: { name: { type: 'string' } },
  required: %i[name],
}) # => OmniAI::Schema::Object
OmniAI::Schema.deserialize({
  type: 'array',
  items: { type: 'string' },
}) # => OmniAI::Schema::Array
OmniAI::Schema.deserialize({
  type: 'string',
  description: '...',
  enum: ['...', ],
}) # => OmniAI::Schema::Scalar

Parameters:



49
50
51
52
53
54
55
56
57
# File 'lib/omniai/schema.rb', line 49

def self.deserialize(data)
  type = Array(data["type"] || data[:type]).find { |type| !type.eql?("null") }

  case type
  when OmniAI::Schema::Array::TYPE then OmniAI::Schema::Array.deserialize(data)
  when OmniAI::Schema::Object::TYPE then OmniAI::Schema::Object.deserialize(data)
  else OmniAI::Schema::Scalar.deserialize(data)
  end
end

.format(name:, schema:) ⇒ OmniAI::Schema::Format

Examples:

OmniAI::Schema.format(name: "Contact", schema: OmniAI::Schema.object(...)) #=> OmniAI::Schema::Format

Parameters:

Returns:



174
175
176
# File 'lib/omniai/schema.rb', line 174

def self.format(name:, schema:)
  OmniAI::Schema::Format.new(name:, schema:)
end

.integer(title: nil, description: nil, enum: nil, nullable: nil) ⇒ OmniAI::Schema::Scalar

Examples:

OmniAI::Schema.integer(description: "The age of the person") #=> OmniAI::Schema::Scalar

Parameters:

  • title (String) (defaults to: nil)

    optional - the title of the property

  • description (String) (defaults to: nil)

    optional - a description of the property

  • enum (Array<Integer>) (defaults to: nil)

    optional - the possible values of the property

  • nullable (Boolean) (defaults to: nil)

    optional - if the property may be null

Returns:



136
137
138
# File 'lib/omniai/schema.rb', line 136

def self.integer(title: nil, description: nil, enum: nil, nullable: nil)
  OmniAI::Schema::Scalar.new(type: OmniAI::Schema::Scalar::Type::INTEGER, title:, description:, enum:, nullable:)
end

.number(title: nil, description: nil, enum: nil, nullable: nil) ⇒ OmniAI::Schema::Scalar

Examples:

OmniAI::Schema.number(description: "The current temperature.") #=> OmniAI::Schema::Scalar

Parameters:

  • title (String) (defaults to: nil)

    optional - the title of the property

  • description (String) (defaults to: nil)

    optional - a description of the property

  • enum (Array<Number>) (defaults to: nil)

    optional - the possible values of the property

  • nullable (Boolean) (defaults to: nil)

    optional - if the property may be null

Returns:



162
163
164
# File 'lib/omniai/schema.rb', line 162

def self.number(title: nil, description: nil, enum: nil, nullable: nil)
  OmniAI::Schema::Scalar.new(type: OmniAI::Schema::Scalar::Type::NUMBER, title:, description:, enum:, nullable:)
end

.object(title: nil, properties: {}, required: [], description: nil, nullable: nil) ⇒ OmniAI::Schema::Array

Examples:

property = OmniAI::Schema.object(
  properties: {
    name: OmniAI::Schema.string(description: 'The name of the person.'),
    age: OmniAI::Schema.integer(description: 'The age of the person.'),
    employed: OmniAI::Schema.boolean(description: 'Is the person employed?'),
  },
  description: 'A person.'
  required: %i[name]
)

Parameters:

  • title (String) (defaults to: nil)

    optional - the title of the object

  • properties (Hash<String, OmniAI::Schema::Scalar>) (defaults to: {})

    required - the properties of the object

  • required (Array<Symbol>) (defaults to: [])

    optional - the required properties

  • description (String) (defaults to: nil)

    optional - a description of the object

  • nullable (Boolean) (defaults to: nil)

    optional - if the object may be null

Returns:



110
111
112
# File 'lib/omniai/schema.rb', line 110

def self.object(title: nil, properties: {}, required: [], description: nil, nullable: nil)
  OmniAI::Schema::Object.new(title:, properties:, required:, description:, nullable:)
end

.string(title: nil, description: nil, enum: nil, nullable: nil) ⇒ OmniAI::Schema::Scalar

Examples:

OmniAI::Schema.string(description: "The name of the person.") #=> OmniAI::Schema::Scalar

Parameters:

  • title (String) (defaults to: nil)

    optional - the title of the property

  • description (String) (defaults to: nil)

    optional - a description of the property

  • enum (Array<String>) (defaults to: nil)

    optional - the possible values of the property

  • nullable (Boolean) (defaults to: nil)

    optional - if the property may be null

Returns:



149
150
151
# File 'lib/omniai/schema.rb', line 149

def self.string(title: nil, description: nil, enum: nil, nullable: nil)
  OmniAI::Schema::Scalar.new(type: OmniAI::Schema::Scalar::Type::STRING, title:, description:, enum:, nullable:)
end