Class: Rails::Llm::Structured::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/llm/structured/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_response, schema:) ⇒ Response

Returns a new instance of Response.



11
12
13
14
15
16
# File 'lib/rails/llm/structured/response.rb', line 11

def initialize(raw_response, schema:)
  @raw = raw_response
  @schema = schema
  @data = parse_response(raw_response)
  @metadata = (raw_response)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/rails/llm/structured/response.rb', line 18

def method_missing(method, *args)
  if @data.respond_to?(method)
    @data.public_send(method, *args)
  else
    super
  end
end

Instance Attribute Details

#data ⇒ Object (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/rails/llm/structured/response.rb', line 9

def data
  @data
end

#metadata ⇒ Object (readonly)

Returns the value of attribute metadata.



9
10
11
# File 'lib/rails/llm/structured/response.rb', line 9

def 
  @metadata
end

#raw ⇒ Object (readonly)

Returns the value of attribute raw.



9
10
11
# File 'lib/rails/llm/structured/response.rb', line 9

def raw
  @raw
end

Instance Method Details

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rails/llm/structured/response.rb', line 26

def respond_to_missing?(method, include_private = false)
  @data.respond_to?(method) || super
end

#to_h ⇒ Object



30
31
32
# File 'lib/rails/llm/structured/response.rb', line 30

def to_h
  @data.to_h
end

#valid? ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'lib/rails/llm/structured/response.rb', line 34

def valid?
  @schema.validate!(@data.to_h)
  true
rescue
  false
end