Class: Typesafe::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/typesafe/response.rb

Overview

A System One evaluation response: the model that performed the evaluation, one typed Answer per question (keyed by the question ids from the request), and the token Usage.

response = Typesafe::Response.from_json(json)
response[:refund_requested] # => #<Typesafe::NoulAnswer @noul=0.95>

The answer objects do not carry their question id: it is the Hash key under which each answer appears in #answers, mirroring how questions are sent.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:, answers:, usage:) ⇒ Response

Returns a new instance of Response.

Parameters:

  • model (String)

    the model that performed the evaluation.

  • answers (Hash)

    question ids (String or Symbol) mapped to Answer objects or answer Hashes; Hashes are parsed via Answer.from_h.

  • usage (Usage, Hash)

    a Usage object, or a usage Hash parsed via Usage.from_h.

Raises:

  • (ArgumentError)

    if any attribute is invalid or an answer is not parseable.



31
32
33
34
35
36
# File 'lib/typesafe/response.rb', line 31

def initialize(model:, answers:, usage:)
  @model = freeze_string(model, "model must be a non-empty String")
  @answers = normalize_answers(answers)
  @usage = usage.is_a?(Usage) ? usage : Usage.from_h(usage)
  freeze
end

Instance Attribute Details

#answersHash (readonly)

Returns question ids (String or Symbol) mapped to Answer objects.

Returns:

  • (Hash)

    question ids (String or Symbol) mapped to Answer objects.



19
20
21
# File 'lib/typesafe/response.rb', line 19

def answers
  @answers
end

#modelString (readonly)

Returns the model that performed the evaluation.

Returns:

  • (String)

    the model that performed the evaluation.



16
17
18
# File 'lib/typesafe/response.rb', line 16

def model
  @model
end

#usageUsage (readonly)

Returns the token usage for the request.

Returns:

  • (Usage)

    the token usage for the request.



22
23
24
# File 'lib/typesafe/response.rb', line 22

def usage
  @usage
end

Class Method Details

.from_h(hash) ⇒ Response

Parameters:

  • hash (Hash)

    the parsed response body, with String or Symbol keys.

Returns:

Raises:

  • (ArgumentError)

    if hash is not a valid response shape.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/typesafe/response.rb', line 51

def self.from_h(hash)
  unless hash.is_a?(Hash)
    raise ArgumentError, "response must be a Hash, got #{hash.inspect}"
  end

  new(
    model: hash["model"] || hash[:model],
    answers: hash["answers"] || hash[:answers],
    usage: hash["usage"] || hash[:usage]
  )
end

.from_json(json) ⇒ Response

Parses a raw JSON response body from the TypeSafe API.

Parameters:

  • json (String)

    the response body.

Returns:

Raises:

  • (ArgumentError)

    if the body is not a valid response shape.

  • (JSON::ParserError)

    if json is not valid JSON.



44
45
46
# File 'lib/typesafe/response.rb', line 44

def self.from_json(json)
  from_h(JSON.parse(json))
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Responses compare equal when they are of the same class and serialize to the same API shape.



83
84
85
# File 'lib/typesafe/response.rb', line 83

def ==(other)
  other.class == self.class && other.to_h == to_h
end

#[](id) ⇒ Answer?

The answer for a question id.

Parameters:

  • id (String, Symbol)

    the question id used in the request.

Returns:

  • (Answer, nil)

    the matching answer, or nil if the id is unknown.



67
68
69
# File 'lib/typesafe/response.rb', line 67

def [](id)
  answers[id] || (id.is_a?(Symbol) ? answers[id.to_s] : nil)
end

#hashObject



88
89
90
# File 'lib/typesafe/response.rb', line 88

def hash
  [self.class, to_h].hash
end

#to_hHash

Returns the response shape returned by the TypeSafe API.

Returns:

  • (Hash)

    the response shape returned by the TypeSafe API.



72
73
74
# File 'lib/typesafe/response.rb', line 72

def to_h
  { model: model, answers: answers.transform_values(&:to_h), usage: usage.to_h }
end

#to_json(state = nil) ⇒ String

Returns the response serialized as JSON.

Returns:

  • (String)

    the response serialized as JSON.



77
78
79
# File 'lib/typesafe/response.rb', line 77

def to_json(state = nil)
  JSON.generate(to_h, state)
end