Class: Raix::ResponseFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/raix/response_format.rb

Overview

Handles the formatting of responses for AI interactions.

This class is responsible for converting input data into a JSON schema that can be used to structure and validate AI responses. It supports nested structures and arrays, ensuring that the output conforms to the expected format for AI model interactions.

Examples:

input = { name: { type: "string" }, age: { type: "integer" } }
format = ResponseFormat.new("PersonInfo", input)
schema = format.to_schema

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, input) ⇒ ResponseFormat

Returns a new instance of ResponseFormat.



22
23
24
25
# File 'lib/raix/response_format.rb', line 22

def initialize(name, input)
  @name = name
  @input = input
end

Instance Attribute Details

#inputHash (readonly)

The input data to be formatted

Returns:

  • (Hash)

    the current value of input



21
22
23
# File 'lib/raix/response_format.rb', line 21

def input
  @input
end

#nameString (readonly)

The name of the response format

Returns:

  • (String)

    the current value of name



21
22
23
# File 'lib/raix/response_format.rb', line 21

def name
  @name
end

Instance Method Details

#to_jsonObject



27
28
29
# File 'lib/raix/response_format.rb', line 27

def to_json(*)
  JSON.pretty_generate(to_schema)
end

#to_schemaObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/raix/response_format.rb', line 31

def to_schema
  {
    type: "json_schema",
    json_schema: {
      name: @name,
      schema: {
        type: "object",
        properties: decode(@input.deep_dup),
        required: @input.keys,
        additionalProperties: false
      },
      strict: true
    }
  }
end