Class: Typesafe::Response
- Inherits:
-
Object
- Object
- Typesafe::Response
- 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
-
#answers ⇒ Hash
readonly
Question ids (String or Symbol) mapped to Answer objects.
-
#model ⇒ String
readonly
The model that performed the evaluation.
-
#usage ⇒ Usage
readonly
The token usage for the request.
Class Method Summary collapse
- .from_h(hash) ⇒ Response
-
.from_json(json) ⇒ Response
Parses a raw JSON response body from the TypeSafe API.
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Responses compare equal when they are of the same class and serialize to the same API shape.
-
#[](id) ⇒ Answer?
The answer for a question id.
- #hash ⇒ Object
-
#initialize(model:, answers:, usage:) ⇒ Response
constructor
A new instance of Response.
-
#to_h ⇒ Hash
The response shape returned by the TypeSafe API.
-
#to_json(state = nil) ⇒ String
The response serialized as JSON.
Constructor Details
#initialize(model:, answers:, usage:) ⇒ Response
Returns a new instance of Response.
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
#answers ⇒ Hash (readonly)
Returns question ids (String or Symbol) mapped to Answer objects.
19 20 21 |
# File 'lib/typesafe/response.rb', line 19 def answers @answers end |
#model ⇒ String (readonly)
Returns the model that performed the evaluation.
16 17 18 |
# File 'lib/typesafe/response.rb', line 16 def model @model end |
#usage ⇒ Usage (readonly)
Returns 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
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.
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.
67 68 69 |
# File 'lib/typesafe/response.rb', line 67 def [](id) answers[id] || (id.is_a?(Symbol) ? answers[id.to_s] : nil) end |
#hash ⇒ Object
88 89 90 |
# File 'lib/typesafe/response.rb', line 88 def hash [self.class, to_h].hash end |
#to_h ⇒ Hash
Returns 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.
77 78 79 |
# File 'lib/typesafe/response.rb', line 77 def to_json(state = nil) JSON.generate(to_h, state) end |