Class: ActiveAgent::SchemaGenerator::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/active_agent/schema_generator.rb

Class Method Summary collapse

Class Method Details

.json_schema_from_model(model_class, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/active_agent/schema_generator.rb', line 31

def self.json_schema_from_model(model_class, options = {})
  schema = {
    type: "object",
    properties: {},
    required: [],
    additionalProperties: options.fetch(:additional_properties, false)
  }

  if defined?(ActiveRecord::Base) && model_class < ActiveRecord::Base
    build_activerecord_schema(model_class, schema, options)
  elsif defined?(ActiveModel::Model) && model_class.include?(ActiveModel::Model)
    build_activemodel_schema(model_class, schema, options)
  else
    raise ArgumentError, "Model must be an ActiveRecord or ActiveModel class"
  end

  if options[:strict]
    # OpenAI strict mode requires all properties to be in the required array
    # So we add all properties to required if strict mode is enabled
    schema[:required] = schema[:properties].keys.map(&:to_s).sort

    {
      name: options[:name] || model_class.name.underscore,
      schema: schema,
      strict: true
    }
  else
    schema
  end
end